gotag

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 3 Imported by: 0

README

gotag

This package defines the framework and the interfaces.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAStruct = fmt.Errorf("type is not a struct")

An error that says that the type where the tags should be read is not a struct.

Functions

This section is empty.

Types

type ReadValueError

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

An error that wraps the underlying value write error.

func NewReadValueError

func NewReadValueError[T any](valueType reflect.Type, cause error) *ReadValueError

Creates a new gotag.ReadValueError.

func (*ReadValueError) Error

func (e *ReadValueError) Error() string

Error implements error.

func (*ReadValueError) Unwrap

func (e *ReadValueError) Unwrap() error

Returns the cause of the the read error.

type StructDecoder

type StructDecoder[T any, S any] interface {
	// Create a new instance based on the source.
	Decode(s S) (T, error)
}

Decoder that creates new instaces of structs that have struct tags.

func NewDecoder

func NewDecoder[T any, S any](writers ...StructTagValueWriter[S]) StructDecoder[T, S]

Creates a default gotag.StructDecoder.

type StructEncoder

type StructEncoder[T any, R any] interface {
	// Create a new instance based on the source.
	Encode(t T, defaultValue R) (R, error)
}

Decoder that creates new instaces of structs that have struct tags.

func NewEncoder

func NewEncoder[T any, R any](readers ...StructTagValueReader[R]) StructEncoder[T, R]

Creates a default gotag.StructEncoder.

type StructEncoderCombiner

type StructEncoderCombiner[S any, R any] func(S, R) (R, error)

type StructTagCommand

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

Allows accessing the value without exposing the reflect.Value.

func (*StructTagCommand) Tag

func (c *StructTagCommand) Tag() string

Tag implements StructTagHandler.

func (*StructTagCommand) TagValues

func (c *StructTagCommand) TagValues() []string

type StructTagHandler

type StructTagHandler interface {
	Tag() string
}

Interface for struct tag handlers.

type StructTagHandlerExecutionError

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

An error that wraps the underlying value tag processing error.

func NewStructTagHandlerExecutionError

func NewStructTagHandlerExecutionError(command *StructTagCommand, cause error) *StructTagHandlerExecutionError

Creates a new gotag.StructTagHandlerExecutionError.

func (*StructTagHandlerExecutionError) Error

Error implements error.

func (*StructTagHandlerExecutionError) Unwrap

Returns the cause of the the write error.

type StructTagHandlerUnsupportedFieldError

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

An error for a not supported field.

func (*StructTagHandlerUnsupportedFieldError) Error

Error implements error.

type StructTagValueReader

type StructTagValueReader[R any] interface {
	StructTagHandler
	// Processes the struct tag and sets the value.
	Read(cmd *StructTagCommand, result R) (R, error)
}

Interface for struct tag based value writers.

type StructTagValueWriter

type StructTagValueWriter[S any] interface {
	StructTagHandler
	// Processes the struct tag and sets the value.
	Write(cmd *StructTagCommand, source S) error
}

Interface for struct tag based value writers.

type TagValueHandlerConfigError

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

An error for a invalid handler configuration.

func NewTagValueHandlerConfigError

func NewTagValueHandlerConfigError(command *StructTagCommand, message string) *TagValueHandlerConfigError

Creates a new gotag.TagValueHandlerConfigError.

func (*TagValueHandlerConfigError) Error

Error implements error.

type ValueReaderFunc

type ValueReaderFunc[R any] func(value reflect.Value) (R, bool, error)

Function that transforms the reflect.Value. Returns value, true and nil error, if the execution has succeeded. Returns default value, true and non-nil error, if the execution has failed. Returns default value, false and nil error, if the value is not supported.

func NewFirstSucceedValueReader added in v1.1.0

func NewFirstSucceedValueReader[R any](readers ...ValueReaderFunc[R]) ValueReaderFunc[R]

Combines multiple gotag.ValueReaderFunc into a single one. Each function is executed in natural order. Returns the result from the first supported function. Return default value, false and nil error, if no function supports the value or all supported functions fail.

func NewFirstSupportedValueReader added in v1.1.0

func NewFirstSupportedValueReader[R any](readers ...ValueReaderFunc[R]) ValueReaderFunc[R]

Combines multiple gotag.ValueReaderFunc into a single one. Each function is executed in natural order. Returns the result from the first supported function. Return default value, false and nil error, if no function supports the value.

func (ValueReaderFunc[R]) ReadValue

func (f ValueReaderFunc[R]) ReadValue(c *StructTagCommand) (R, error)

Reads the value without exposing the reflect.Value.

type ValueWriterFunc

type ValueWriterFunc[S any] func(value reflect.Value, source S) (bool, error)

Function that transforms the source value, and writes it to the reflect.Value. Returns true and nil error, if the execution has succeeded. Returns true and non-nil error, if the execution has failed. Returns false and nil error, if the value is not supported.

func NewFirstSucceedValueWriter added in v1.1.0

func NewFirstSucceedValueWriter[S any](writers ...ValueWriterFunc[S]) ValueWriterFunc[S]

Combines multiple gotag.ValueWriterFunc into a single one. Each function is executed in natural order. Returns the result from the first supported function which executes without errors. Return false and nil error, if no function supports the value or all supported functions fail.

func NewFirstSupportedValueWriter added in v1.1.0

func NewFirstSupportedValueWriter[S any](writers ...ValueWriterFunc[S]) ValueWriterFunc[S]

Combines multiple gotag.ValueWriterFunc into a single one. Each function is executed in natural order. Returns the result from the first supported function. Return false and nil error, if no function supports the value.

func (ValueWriterFunc[S]) WriteValue

func (f ValueWriterFunc[S]) WriteValue(c *StructTagCommand, source S) error

Writes the value without exposing the reflect.Value.

type WriteValueError

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

An error that wraps the underlying value write error.

func NewWriteValueError

func NewWriteValueError(newValue any, valueType reflect.Type, cause error) *WriteValueError

Creates a new gotag.WriteValueError.

func (*WriteValueError) Error

func (e *WriteValueError) Error() string

Error implements error.

func (*WriteValueError) Unwrap

func (e *WriteValueError) Unwrap() error

Returns the cause of the the write error.

Jump to

Keyboard shortcuts

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