structs

package
v10.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTagEmpty is returned when an empty tag is passed to ToMap.
	ErrTagEmpty = errors.New("tag cannot be empty")
	// ErrInvalidInputType is returned when ToMap receives an input that is not a struct or a pointer to a struct.
	ErrInvalidInputType = errors.New("invalid input type")
)
View Source
var (
	// ErrFieldNotFound is returned when an operation is unable to find a target field.
	ErrFieldNotFound = errors.New("struct field not found")
)

Functions

func GetFieldTagValue

func GetFieldTagValue(targetStruct interface{}, fieldName string, tagName string) (string, error)

GetFieldTagValue gets a tag value from a struct field. The tag is always returned as a string, independent of field type.

Example ```

s := Struct{
    Value int `default:"100" validate:"required"`
}{}

// Get the Value field default value tag := GetFieldTagValue(s, "Value", "default")

// Prints 100 fmt.Println(tag) ```

func ToMap

func ToMap(s any) (map[string]any, error)

ToMap converts the given struct to a map[string]any. It uses the "structs" tag to identify the key for each map field.

The default key string is the struct field name but can be changed in the struct field's tag value.

// Field appears in map as key "Name".
Name string

The "structs" key in the struct's field tag value is the key name. Example:

// Field appears in map as key "myName".
Name string `structs:"myName"`

A tag value with the content of "-" ignores that particular field. Example:

// Field is ignored by this function.
Field bool `structs:"-"`

A tag value with the content of "string" uses Go's Stringer interface to get the value. Example:

// The value will be the output of Animal's String() func.
// Map will panic if Animal does not implement String().
Field *Animal `structs:"field,string"`

A tag value with the option of "flatten" used in a struct field is to flatten its fields in the output map. Example:

// The FieldStruct's fields will be flattened into the output map.
FieldStruct time.Time `structs:",flatten"`

A tag value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

A tag value with the option of "omitempty" ignores that particular field if the field value is empty. Example:

// Field appears in map as key "myName", but the field is
// skipped if empty.
Field string `structs:"myName,omitempty"`

// Field appears in map as key "Field" (the default), but
// the field is skipped if empty.
Field string `structs:",omitempty"`

Types

This section is empty.

Jump to

Keyboard shortcuts

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