replacer

package
v2.1.93 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NULL = "__null__"
)

NULL is used to force resolve to nil

Variables

View Source
var Replacers = []Replacer{
	replaceInRequest,
	replaceInResponse,
	replaceInHeaders,
	replaceInPath,
	replaceFromContext,
	replaceFromSchemaExample,
	replaceFromSchemaFormat,
	replaceFromSchemaPrimitive,
	replaceFromSchemaFallback,
}

Replacers is a list of replacers that are used to replace values in schemas and contents in the specified order.

Functions

func IsCorrectlyReplacedType

func IsCorrectlyReplacedType(value any, neededType string) bool

IsCorrectlyReplacedType checks if the given value is of the correct schema type.

func IsMatchSchemaReadWriteToState

func IsMatchSchemaReadWriteToState(schema *schema.Schema, state *ReplaceState) bool

IsMatchSchemaReadWriteToState checks if the given schema is read-write match. ReadOnly - A property that is only available in a response. WriteOnly - A property that is only available in a request.

Types

type ReplaceContext

type ReplaceContext struct {
	// contains filtered or unexported fields
}

ReplaceContext is a context that is used to replace values in schemas and contents.

schema is a schema that is used to replace values. Currently only OpenAPI schema is supported. It does not depend on schema provider as this is already converted to internal schema type.

state is a state of the current replace operation. It is used to store information about the current element, including its name, index, content type etc.

areaPrefix is a prefix that is used to identify the correct section in the context config for specific replacement area. e.g. in- then in the contexts we should have: in-header:

X-GeneratedRequest-ID: 123

in-path:

user_id: 123

data is a list of contexts that are used to replace values. faker is a faker instance that is used to generate fake data. functions is a map of all fake functions that are used to replace values.

type ReplaceState

type ReplaceState struct {
	NamePath           []string
	ElementIndex       int
	IsHeader           bool
	IsPathParam        bool
	ContentType        string
	IsContentReadOnly  bool
	IsContentWriteOnly bool
	SchemaStack        map[*schema.Schema]bool
	RecursionHit       bool
	// contains filtered or unexported fields
}

ReplaceState is a struct that holds information about the current state of the replace operation.

NamePath is a slice of names of the current element. It is used to build a path to the current element. For example, "users", "name", "first".

ElementIndex is an index of the current element if required structure to generate is an array. IsHeader is a flag that indicates that the current element we're replacing is a header. IsPathParam is a flag that indicates that the current element we're replacing is a path parameter. ContentType is a content type of the current element. IsContentReadOnly is a flag that indicates that the current element we're replacing is a read-only content. This value is used only when Scheme has ReadOnly set to true.

IsContentWriteOnly is a flag that indicates that the current element we're replacing is a write-only content. This value is used only when Scheme has WriteOnly set to true.

SchemaStack tracks schemas being processed to detect circular references.

RecursionHit is set to true when a circular reference is detected during generation. This allows parent objects to know that a child returned nil due to recursion, not because it was legitimately optional.

func NewReplaceState

func NewReplaceState(opts ...ReplaceStateOption) *ReplaceState

func NewReplaceStateWithName

func NewReplaceStateWithName(name string) *ReplaceState

func (*ReplaceState) NewFrom

func (s *ReplaceState) NewFrom(src *ReplaceState) *ReplaceState

NewFrom creates a new ReplaceState instance from the given one.

func (*ReplaceState) WithOptions

func (s *ReplaceState) WithOptions(options ...ReplaceStateOption) *ReplaceState

type ReplaceStateOption

type ReplaceStateOption func(*ReplaceState)

func WithContentType

func WithContentType(value string) ReplaceStateOption

func WithElementIndex

func WithElementIndex(value int) ReplaceStateOption

func WithHeader

func WithHeader() ReplaceStateOption

func WithName

func WithName(name string) ReplaceStateOption

func WithPath

func WithPath() ReplaceStateOption

func WithReadOnly

func WithReadOnly() ReplaceStateOption

func WithWriteOnly

func WithWriteOnly() ReplaceStateOption

type Replacer

type Replacer func(ctx *ReplaceContext) any

Replacer is a function that returns a value to replace the original value with. Replacer functions are predefined and set in the correct order to be executed.

type UnsignedInt

type UnsignedInt interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

UnsignedInt is a constraint for unsigned integer types.

type ValueReplacer

type ValueReplacer func(schemaOrContent any, state *ReplaceState) any

ValueReplacer is a function that replaces value in schema or content. This function should encapsulate all the logic, data, contexts etc. of replacing values.

func CreateValueReplacer

func CreateValueReplacer(replacers []Replacer, contexts []map[string]any) ValueReplacer

CreateValueReplacer is a factory that creates a new ValueReplacer instance from the given config and contexts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL