fox/mapstruct/mapstruct.go

40 lines
1.0 KiB
Go
Raw Normal View History

// Package mapstruct provides bidirectional conversion between Go structs and maps.
// It supports:
// - Struct <-> map[string]any
// - Struct <-> map[string]string
// - Custom type conversion via hooks
// - Nested structs and pointers
// - Field name mapping via struct tags
package mapstruct
import (
"reflect"
)
// HookFunc defines a function type for custom field conversion.
type HookFunc func(reflect.Value, reflect.StructField) (any, error)
// StringHookFunc defines a function type for custom string-to-value conversion.
type StringHookFunc func(string, reflect.StructField) (any, error)
// Direction indicates the conversion direction.
//type Direction int
//
//const (
// StructToMap Direction = iota
// MapToStruct
//)
// commonOptions contains options shared by both conversions.
type commonOptions struct {
TagName string
FieldHooks map[string]HookFunc
}
// newCommonOptions creates a new commonOptions with defaults.
func newCommonOptions() *commonOptions {
return &commonOptions{
TagName: "json",
}
}