feat(go-config): multi-format codecs, envc CLI, fixtures, ASRs
- Module github.com/eslider/go-config with env/yaml/json/ini packages - internal: source, keymap, merge, structconv, bytesutil - cmd/envc: convert, get, merge - docs/asr ASR-0001..0008, README/CHANGELOG/AGENTS refresh - golangci-lint v2 config; tests + fixtures Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// Package structconv wraps mapstructure for map<->struct conversion.
|
||||
package structconv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
)
|
||||
|
||||
// Options configures Decode and Encode.
|
||||
type Options struct {
|
||||
TagName string
|
||||
WeaklyTyped bool
|
||||
Trim bool
|
||||
ExtraHooks []mapstructure.DecodeHookFunc
|
||||
}
|
||||
|
||||
func trimStringHook(from, to reflect.Type, data any) (any, error) {
|
||||
if from.Kind() == reflect.String && to.Kind() == reflect.String {
|
||||
if s, ok := data.(string); ok {
|
||||
return strings.TrimSpace(s), nil
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func composeHooks(o Options) mapstructure.DecodeHookFunc {
|
||||
var hooks []mapstructure.DecodeHookFunc
|
||||
if o.Trim {
|
||||
hooks = append(hooks, trimStringHook)
|
||||
}
|
||||
hooks = append(hooks, o.ExtraHooks...)
|
||||
if len(hooks) == 0 {
|
||||
return nil
|
||||
}
|
||||
return mapstructure.ComposeDecodeHookFunc(hooks...)
|
||||
}
|
||||
|
||||
// Decode maps in into struct or map dst (dst must be pointer).
|
||||
func Decode(in map[string]any, dst any, o Options) error {
|
||||
tag := o.TagName
|
||||
if tag == "" {
|
||||
tag = "mapstructure"
|
||||
}
|
||||
cfg := &mapstructure.DecoderConfig{
|
||||
Result: dst,
|
||||
WeaklyTypedInput: o.WeaklyTyped,
|
||||
TagName: tag,
|
||||
DecodeHook: composeHooks(o),
|
||||
}
|
||||
dec, err := mapstructure.NewDecoder(cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("structconv: decoder: %w", err)
|
||||
}
|
||||
if err := dec.Decode(in); err != nil {
|
||||
return fmt.Errorf("structconv: decode: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encode flattens src (struct or map) into a new map[string]any.
|
||||
func Encode(src any) (map[string]any, error) {
|
||||
out := make(map[string]any)
|
||||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{Result: &out})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("structconv: encoder: %w", err)
|
||||
}
|
||||
if err := dec.Decode(src); err != nil {
|
||||
return nil, fmt.Errorf("structconv: encode: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package structconv
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodeEncodeRoundTrip(t *testing.T) {
|
||||
type nested struct {
|
||||
V int `mapstructure:"v"`
|
||||
}
|
||||
type root struct {
|
||||
A nested `mapstructure:"a"`
|
||||
}
|
||||
in := map[string]any{"a": map[string]any{"v": 7}}
|
||||
var dst root
|
||||
if err := Decode(in, &dst, Options{WeaklyTyped: true, Trim: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := Encode(&dst)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
am := out["a"].(map[string]any)
|
||||
if fmt.Sprint(am["v"]) != "7" {
|
||||
t.Fatalf("%#v", out)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user