Documentation
¶
Overview ¶
Package mapping converts between untyped action parameter maps and tagged Go structs.
Index ¶
- func AssignStruct(pa map[string]any, st any) error
- func EnvToStringValue(data map[string]any) map[string]any
- func FieldTags(v any) []string
- func MapToStructByTags(params map[string]any, dest any) error
- func ParseTimeout(v any) (time.Duration, error)
- func StringMap(m map[string]any) (map[string]string, error)
- func StructToMapByTags(src any) (map[string]any, error)
- func TitleCase(st string, char string) string
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssignStruct ¶
AssignStruct assigns values from an action's parameter map to a struct using struct tags. Supports string and int fields with validation. Used for legacy action parameter assignment.
Example:
type Config struct {
Name string `map:"name" validate:"required"`
Timeout int `map:"timeout"`
}
params := map[string]any{"name": "test", "timeout": "30"}
var config Config
err := AssignStruct(params, &config)
// config.Name = "test", config.Timeout = 30
func EnvToStringValue ¶
EnvToStringValue returns a copy of data whose env entry is a map of strings, ready for a map[string]string field. Flat env__NAME keys are folded into it and removed, and every value is turned into a string, so that a number such as PORT: 8080 is passed on instead of dropped. When a name is given both ways, the nested env map wins. data itself is not modified.
Example:
data := map[string]any{
"cmd": "run",
"env": map[string]any{"PORT": 8080},
"env__MODE": "test",
}
result := EnvToStringValue(data)
// result = map[string]any{
// "cmd": "run",
// "env": map[string]any{"PORT": "8080", "MODE": "test"},
// }
func FieldTags ¶
FieldTags returns the names the "map" tags of v's fields give, v being a struct or a pointer to one: the keys MapToStructByTags fills it from, in the order of the fields. A tag is taken as it is written, as MapToStructByTags takes it, and a field without one has no key. A struct with no tagged field gives an empty list, and anything else nil.
func MapToStructByTags ¶
MapToStructByTags converts a map[string]any to a struct using struct tags. Fields are mapped using the "map" tag, and validation is performed using the "validate" tag. Supports nested structs, []byte fields, and map[string]string fields.
Example:
type User struct {
Name string `map:"name" validate:"required"`
Age int `map:"age"`
Metadata map[string]string `map:"metadata"`
}
params := map[string]any{
"name": "John",
"age": 30,
"metadata": map[string]any{"role": "admin", "dept": "IT"},
}
var user User
err := MapToStructByTags(params, &user)
// user.Name = "John", user.Age = 30, user.Metadata = {"role": "admin", "dept": "IT"}
func ParseTimeout ¶
ParseTimeout reads an action's timeout parameter as duration.Parse reads a duration: a duration string such as "30s", a number of seconds, or a numeric string. It refuses anything else, and zero too, rather than falling back to a default, so a mistyped timeout is reported instead of silently ignored. The error says why, as duration.Parse does.
func StringMap ¶
StringMap returns m with each value written as text: a string as it is, a bool or a number as scalar.String writes it, so a header such as X-Retry: 3 is passed on as "3", and nil, as YAML reads X-Empty: with nothing after it, as an empty string. Any other value, such as a list or a map, has no text to stand for it, and is an error that names its key. m itself is not modified.
Example:
StringMap(map[string]any{"a": "x", "b": 1024, "c": 0.5, "d": true, "e": nil})
// map[string]string{"a": "x", "b": "1024", "c": "0.5", "d": "true", "e": ""}
func StructToMapByTags ¶
StructToMapByTags converts a struct to a map[string]any using struct tags. Fields are mapped using the "map" tag. Supports nested structs, []byte fields, and map[string]string fields. This is the inverse operation of MapToStructByTags.
Example:
type User struct {
Name string `map:"name"`
Age int `map:"age"`
Metadata map[string]string `map:"metadata"`
}
user := User{
Name: "John",
Age: 30,
Metadata: map[string]string{"role": "admin", "dept": "IT"},
}
result, err := StructToMapByTags(user)
// result: map[string]any{
// "name": "John",
// "age": 30,
// "metadata": map[string]string{"role": "admin", "dept": "IT"},
// }
func TitleCase ¶
TitleCase converts a string to title case using a specified separator character. Each part separated by the character has its first letter capitalized.
Example:
TitleCase("content-type", "-") // "Content-Type"
TitleCase("user_name", "_") // "User_Name"
TitleCase("hello-world-test", "-") // "Hello-World-Test"
Types ¶
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
ValidationError collects validation messages produced while assigning action parameters to a struct.
func (*ValidationError) AddMessage ¶
func (e *ValidationError) AddMessage(s string)
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) HasError ¶
func (e *ValidationError) HasError() bool