replacers

package
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NULL = "__null__"
)

NULL is used to force resolve to nil

Variables

View Source
var (
	ErrUnexpectedFormDataType       = errors.New("expected map[string]any for multipart/form-data")
	ErrUnexpectedFormURLEncodedType = errors.New("expected map[string]any for x-www-form-urlencoded")
	ErrGettingFileFromURL           = errors.New("error getting file from url")
)

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

Functions

func ApplySchemaConstraints

func ApplySchemaConstraints(openAPISchema any, res any) any

ApplySchemaConstraints applies schema constraints to the value. It converts the input value to match the corresponding OpenAPI type specified in the schema.

func CastToSchemaFormat

func CastToSchemaFormat(ctx *ReplaceContext, value any) any

CastToSchemaFormat casts the value to the schema format if possible. If the schema format is not specified, the value is returned as-is.

func HasCorrectSchemaValue

func HasCorrectSchemaValue(ctx *ReplaceContext, value any) bool

HasCorrectSchemaValue checks if the value is of the correct type and format.

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 *openapi.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.

func ReplaceFromContext

func ReplaceFromContext(ctx *ReplaceContext) any

ReplaceFromContext is a replacer that replaces values from the context.

func ReplaceFromSchemaExample

func ReplaceFromSchemaExample(ctx *ReplaceContext) any

ReplaceFromSchemaExample is a replacer that replaces values from the schema example.

func ReplaceFromSchemaFallback

func ReplaceFromSchemaFallback(ctx *ReplaceContext) any

ReplaceFromSchemaFallback is the last resort to get a value from the schema.

func ReplaceFromSchemaFormat

func ReplaceFromSchemaFormat(ctx *ReplaceContext) any

ReplaceFromSchemaFormat is a replacer that replaces values from the schema format.

func ReplaceFromSchemaPrimitive

func ReplaceFromSchemaPrimitive(ctx *ReplaceContext) any

ReplaceFromSchemaPrimitive is a replacer that replaces values from the schema primitive.

func ReplaceInHeaders

func ReplaceInHeaders(ctx *ReplaceContext) any

ReplaceInHeaders is a replacer that replaces values only in headers.

func ReplaceInPath

func ReplaceInPath(ctx *ReplaceContext) any

ReplaceInPath is a replacer that replaces values only in path parameters.

func ReplaceValueWithContext

func ReplaceValueWithContext(path []string, contextData any) interface{}

ReplaceValueWithContext is a replacer that replaces values from the context.

Types

type Any

type Any interface {
	string | int | bool | float64 | any
}

Any is a type that can be used to represent any type in generics.

type ReplaceContext

type ReplaceContext struct {
	// 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.
	Schema any

	// 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.
	State *ReplaceState

	// 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
	AreaPrefix string

	// Data is a list of contexts that are used to replace values.
	Data []map[string]any

	// Faker is a faker instance that is used to generate fake data.
	Faker faker.Faker
}

type ReplaceState

type ReplaceState struct {
	// 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".
	NamePath []string

	// ElementIndex is an index of the current element if required structure to generate is an array.
	ElementIndex int

	// IsHeader is a flag that indicates that the current element we're replacing is a header.
	IsHeader bool

	// IsPathParam is a flag that indicates that the current element we're replacing is a path parameter.
	IsPathParam bool

	// ContentType is a content type of the current element.
	ContentType string

	// IsContentReadOnly is a flag that indicates that the current element we're replacing is a read-only content.
	// This value is used only when Schema has ReadOnly set to true.
	IsContentReadOnly bool

	// IsContentWriteOnly is a flag that indicates that the current element we're replacing is a write-only content.
	// This value is used only when Schema has WriteOnly set to true.
	IsContentWriteOnly bool
	// contains filtered or unexported fields
}

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

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 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(cfg *config.Config, 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