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:
+163
@@ -0,0 +1,163 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/eslider/go-config/internal/bytesutil"
|
||||
"github.com/eslider/go-config/internal/keymap"
|
||||
"github.com/eslider/go-config/internal/merge"
|
||||
"github.com/eslider/go-config/internal/source"
|
||||
"github.com/eslider/go-config/internal/structconv"
|
||||
iniv1 "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// Codec loads INI from multiple sources.
|
||||
type Codec struct {
|
||||
sources []source.Source
|
||||
normalizer keymap.Normalizer
|
||||
sliceStrat merge.SliceStrategy
|
||||
structOpts structconv.Options
|
||||
mergeOpts []merge.Option
|
||||
httpClient *http.Client
|
||||
urlHeader http.Header
|
||||
}
|
||||
|
||||
// New creates an INI codec.
|
||||
func New(opts ...Option) *Codec {
|
||||
c := &Codec{
|
||||
normalizer: keymap.LowerAlnum,
|
||||
sliceStrat: merge.Replace,
|
||||
structOpts: structconv.Options{
|
||||
TagName: "mapstructure",
|
||||
WeaklyTyped: true,
|
||||
Trim: true,
|
||||
},
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(c)
|
||||
}
|
||||
c.mergeOpts = []merge.Option{merge.WithSliceStrategy(c.sliceStrat)}
|
||||
return c
|
||||
}
|
||||
|
||||
// Map merges all INI sources into one map.
|
||||
func (c *Codec) Map(ctx context.Context) (map[string]any, error) {
|
||||
if len(c.sources) == 0 {
|
||||
return nil, fmt.Errorf("ini: no sources")
|
||||
}
|
||||
acc := make(map[string]any)
|
||||
for _, s := range c.sources {
|
||||
b, err := bytesutil.ReadAll(ctx, s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ini: read %s: %w", s.String(), err)
|
||||
}
|
||||
f, err := iniv1.LoadSources(iniv1.LoadOptions{Loose: true, Insensitive: false}, b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ini: parse %s: %w", s.String(), err)
|
||||
}
|
||||
parsed := mapFromINIFile(f)
|
||||
merge.DeepMerge(acc, parsed, c.mergeOpts...)
|
||||
}
|
||||
if c.normalizer != nil {
|
||||
keymap.Walk(acc, c.normalizer)
|
||||
}
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
// Unmarshal decodes using context.Background.
|
||||
func (c *Codec) Unmarshal(dst any) error {
|
||||
return c.UnmarshalContext(context.Background(), dst)
|
||||
}
|
||||
|
||||
// UnmarshalContext decodes merged INI into dst.
|
||||
func (c *Codec) UnmarshalContext(ctx context.Context, dst any) error {
|
||||
m, err := c.Map(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return structconv.Decode(m, dst, c.structOpts)
|
||||
}
|
||||
|
||||
// Marshal encodes src to INI bytes (struct or map[string]any).
|
||||
func (c *Codec) Marshal(src any) ([]byte, error) {
|
||||
m, err := encodeToMap(src)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ini: encode: %w", err)
|
||||
}
|
||||
f := iniv1.Empty()
|
||||
if err := emitMap(nil, m, f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var buf strings.Builder
|
||||
if _, err := f.WriteTo(&buf); err != nil {
|
||||
return nil, fmt.Errorf("ini: write: %w", err)
|
||||
}
|
||||
return []byte(buf.String()), nil
|
||||
}
|
||||
|
||||
// WriteTo writes INI to w.
|
||||
func (c *Codec) WriteTo(w io.Writer, src any) (int64, error) {
|
||||
b, err := c.Marshal(src)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, err := w.Write(b)
|
||||
return int64(n), err
|
||||
}
|
||||
|
||||
func emitMap(prefix []string, m map[string]any, f *iniv1.File) error {
|
||||
for k, v := range m {
|
||||
switch t := v.(type) {
|
||||
case map[string]any:
|
||||
if err := emitMap(append(prefix, k), t, f); err != nil {
|
||||
return err
|
||||
}
|
||||
case []any:
|
||||
parts := make([]string, 0, len(t))
|
||||
for _, e := range t {
|
||||
parts = append(parts, fmt.Sprint(e))
|
||||
}
|
||||
secName := strings.Join(prefix, ".")
|
||||
if secName == "" {
|
||||
secName = iniv1.DefaultSection
|
||||
}
|
||||
sec, err := f.GetSection(secName)
|
||||
if err != nil {
|
||||
sec, err = f.NewSection(secName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := sec.NewKey(k, strings.Join(parts, ",")); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
secName := strings.Join(prefix, ".")
|
||||
if secName == "" {
|
||||
secName = iniv1.DefaultSection
|
||||
}
|
||||
sec, err := f.GetSection(secName)
|
||||
if err != nil {
|
||||
sec, err = f.NewSection(secName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := sec.NewKey(k, fmt.Sprint(t)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func encodeToMap(src any) (map[string]any, error) {
|
||||
if m, ok := src.(map[string]any); ok {
|
||||
return m, nil
|
||||
}
|
||||
return structconv.Encode(src)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/eslider/go-config/testfixtures"
|
||||
)
|
||||
|
||||
func TestCodec_IdentityINIFixture(t *testing.T) {
|
||||
c := New(WithBytes(testfixtures.Load(t, "identity", "service.ini")))
|
||||
m, err := c.Map(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
svc := m["service"].(map[string]any)
|
||||
if svc["name"] != "my-service" {
|
||||
t.Fatalf("%#v", svc)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// Package ini loads INI configuration from bytes, files, or URLs into maps
|
||||
// and structs. Section names may use dot notation for nesting.
|
||||
package ini
|
||||
@@ -0,0 +1,49 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
iniv1 "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
func insertPath(root map[string]any, path []string, value string) {
|
||||
current := root
|
||||
last := len(path) - 1
|
||||
for i, raw := range path {
|
||||
k := strings.ToLower(raw)
|
||||
if i == last {
|
||||
if _, exists := current[k]; exists {
|
||||
return
|
||||
}
|
||||
current[k] = value
|
||||
return
|
||||
}
|
||||
next, ok := current[k].(map[string]any)
|
||||
if !ok {
|
||||
next = make(map[string]any)
|
||||
current[k] = next
|
||||
}
|
||||
current = next
|
||||
}
|
||||
}
|
||||
|
||||
func mapFromINIFile(f *iniv1.File) map[string]any {
|
||||
out := make(map[string]any)
|
||||
for _, name := range f.SectionStrings() {
|
||||
sec, err := f.GetSection(name)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(name, iniv1.DefaultSection) {
|
||||
for k, v := range sec.KeysHash() {
|
||||
insertPath(out, []string{k}, v)
|
||||
}
|
||||
continue
|
||||
}
|
||||
path := strings.Split(name, ".")
|
||||
for k, v := range sec.KeysHash() {
|
||||
insertPath(out, append(append([]string{}, path...), k), v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package ini
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/eslider/go-config/internal/keymap"
|
||||
"github.com/eslider/go-config/internal/merge"
|
||||
"github.com/eslider/go-config/internal/source"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
)
|
||||
|
||||
// Option configures a Codec.
|
||||
type Option func(*Codec)
|
||||
|
||||
// WithBytes appends INI from bytes.
|
||||
func WithBytes(b []byte) Option {
|
||||
return func(c *Codec) { c.sources = append(c.sources, source.Bytes{Data: b, Name: "bytes"}) }
|
||||
}
|
||||
|
||||
// WithReader appends INI from r.
|
||||
func WithReader(r io.Reader) Option {
|
||||
return func(c *Codec) { c.sources = append(c.sources, source.Reader{R: r, Name: "reader"}) }
|
||||
}
|
||||
|
||||
// WithFile appends an INI file path.
|
||||
func WithFile(path string) Option {
|
||||
return func(c *Codec) { c.sources = append(c.sources, source.File{Path: path}) }
|
||||
}
|
||||
|
||||
// WithURL appends INI from an HTTP(S) URL.
|
||||
func WithURL(raw string) Option {
|
||||
return func(c *Codec) {
|
||||
var hdr http.Header
|
||||
if c.urlHeader != nil {
|
||||
hdr = c.urlHeader.Clone()
|
||||
}
|
||||
c.sources = append(c.sources, source.URL{Raw: raw, Header: hdr, HTTPClient: c.httpClient})
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient sets the client for subsequent WithURL sources.
|
||||
func WithHTTPClient(client *http.Client) Option {
|
||||
return func(c *Codec) { c.httpClient = client }
|
||||
}
|
||||
|
||||
// WithHTTPHeader adds a header for subsequent WithURL sources.
|
||||
func WithHTTPHeader(k, v string) Option {
|
||||
return func(c *Codec) {
|
||||
if c.urlHeader == nil {
|
||||
c.urlHeader = make(http.Header)
|
||||
}
|
||||
c.urlHeader.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// WithKeyNormalizer sets key normalizer after merge (nil disables).
|
||||
func WithKeyNormalizer(n keymap.Normalizer) Option {
|
||||
return func(c *Codec) { c.normalizer = n }
|
||||
}
|
||||
|
||||
// WithSliceMerge sets slice merge strategy across sources.
|
||||
func WithSliceMerge(s merge.SliceStrategy) Option {
|
||||
return func(c *Codec) { c.sliceStrat = s }
|
||||
}
|
||||
|
||||
// WithTrim toggles string trim on struct decode.
|
||||
func WithTrim(enable bool) Option {
|
||||
return func(c *Codec) { c.structOpts.Trim = enable }
|
||||
}
|
||||
|
||||
// WithWeaklyTyped toggles weak typing for struct decode.
|
||||
func WithWeaklyTyped(enable bool) Option {
|
||||
return func(c *Codec) { c.structOpts.WeaklyTyped = enable }
|
||||
}
|
||||
|
||||
// WithTagName sets mapstructure tag name.
|
||||
func WithTagName(name string) Option {
|
||||
return func(c *Codec) { c.structOpts.TagName = name }
|
||||
}
|
||||
|
||||
// WithDecodeHook appends a decode hook.
|
||||
func WithDecodeHook(h mapstructure.DecodeHookFunc) Option {
|
||||
return func(c *Codec) {
|
||||
c.structOpts.ExtraHooks = append(c.structOpts.ExtraHooks, h)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user