mapping

package
v1.39.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mapping converts between untyped action parameter maps and tagged Go structs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssignStruct

func AssignStruct(pa map[string]any, st any) error

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

func EnvToStringValue(data map[string]any) map[string]any

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

func FieldTags(v any) []string

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

func MapToStructByTags(params map[string]any, dest any) error

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

func ParseTimeout(v any) (time.Duration, error)

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

func StringMap(m map[string]any) (map[string]string, error)

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

func StructToMapByTags(src any) (map[string]any, error)

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

func TitleCase(st string, char string) string

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

Jump to

Keyboard shortcuts

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