Documentation
¶
Overview ¶
Package mapping converts between untyped action parameter maps and tagged Go structs.
Index ¶
- func AnyToString(value any) (string, bool)
- func AssignStruct(pa map[string]any, st any) error
- func EnvToStringValue(data map[string]any) map[string]any
- func HeaderToStringValue(data map[string]any) map[string]any
- func MapToStructByTags(params map[string]any, dest any) 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 AnyToString ¶
AnyToString attempts to convert any type to a string. Returns the string representation and a boolean indicating success.
Example:
str, ok := AnyToString(42) // "42", true
str, ok := AnyToString(3.14) // "3.14", true
str, ok := AnyToString("hello") // "hello", true
str, ok := AnyToString(nil) // "nil", true
str, ok := AnyToString([]int{1}) // "", false
func AssignStruct ¶
AssignStruct assigns values from an ActionsParams 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 ¶ added in v1.12.0
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 HeaderToStringValue ¶
HeaderToStringValue converts header values to strings for HTTP processing. This function ensures all header values are strings, converting numbers and other types as needed.
Example:
data := map[string]any{
"headers": map[string]any{
"Content-Length": 1024,
"X-Rate-Limit": 100.5,
"Authorization": "Bearer token",
},
}
result := HeaderToStringValue(data)
// result["headers"] = map[string]any{
// "Content-Length": "1024",
// "X-Rate-Limit": "100.5",
// "Authorization": "Bearer token",
// }
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 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