mapping

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 7 Imported by: 0

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 AnyToString

func AnyToString(value any) (string, bool)

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

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 added in v1.12.0

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 HeaderToStringValue

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

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

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 added in v1.13.0

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

ParseTimeout reads an action's timeout parameter: a duration string such as "30s", a number of seconds, or a numeric string. It refuses anything else, including zero, negative values and values too large for a time.Duration, rather than falling back to a default, so a mistyped timeout is reported instead of silently ignored.

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