mapping

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 5 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 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 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 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