Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 98 additions & 8 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"reflect"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -287,9 +288,8 @@ func tooDeep(path []reflect.Type, t reflect.Type) bool {

// decodeSlice sets proper values to slice based on its type.
func decodeSlice(key *Key, field reflect.Value, delim string, allowShadow, isStrict bool) error {
elemType := field.Type().Elem()
if !isSliceElemType(elemType) {
return fmt.Errorf("unsupported type '[]%s'", elemType)
if !isSliceElemType(field.Type().Elem()) {
return fmt.Errorf("unsupported type '[]%s'", field.Type().Elem())
}

var strs []string
Expand All @@ -310,7 +310,16 @@ func decodeSlice(key *Key, field reflect.Value, delim string, allowShadow, isStr
} else {
strs = key.Strings(delim)
}
return decodeElems(field, strs, isStrict)
}

// decodeElems sets the slice field to the elements parsed from strs, or
// leaves it as it is when strs is empty.
func decodeElems(field reflect.Value, strs []string, isStrict bool) error {
elemType := field.Type().Elem()
if !isSliceElemType(elemType) {
return fmt.Errorf("unsupported type '[]%s'", elemType)
}
numVals := len(strs)
if numVals == 0 {
return nil
Expand Down Expand Up @@ -380,16 +389,58 @@ func decodeValue(t reflect.Type, key *Key, field reflect.Value, delim string, al
return nil
}

func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bool, allowNonUnique bool, extends bool) {
opts := strings.SplitN(tag, ",", 5)
func parseTagOptions(tag string) (rawName string, omitEmpty, allowShadow, allowNonUnique, extends, autoIncrement bool) {
opts := strings.Split(tag, ",")
rawName = opts[0]
for _, opt := range opts[1:] {
omitEmpty = omitEmpty || (opt == "omitempty")
allowShadow = allowShadow || (opt == "allowshadow")
allowNonUnique = allowNonUnique || (opt == "nonunique")
extends = extends || (opt == "extends")
autoIncrement = autoIncrement || (opt == "autoincrement")
}
return rawName, omitEmpty, allowShadow, allowNonUnique, extends, autoIncrement
}

// autoIncrementKeys returns the auto-increment keys of s, read from
// "- = value" lines, in order.
func (s *Section) autoIncrementKeys() []*Key {
s.f.maybeRLock()
defer s.f.maybeRUnlock()
var keys []*Key
for _, k := range s.current().keys {
if k.isAutoIncrement {
keys = append(keys, k)
}
}
return keys
}

// setAutoIncrementValues replaces the auto-increment keys of s with one
// for each of vals, added after its other keys.
func (s *Section) setAutoIncrementValues(vals []string) {
s.f.maybeLock()
defer s.f.maybeUnlock()
s = s.attach()
s.keys = slices.DeleteFunc(s.keys, func(k *Key) bool {
if k.isAutoIncrement {
delete(s.byName, k.name)
}
return k.isAutoIncrement
})
n := 1
for _, v := range vals {
name := "#" + strconv.Itoa(n)
for s.byName[name] != nil {
n++
name = "#" + strconv.Itoa(n)
}
n++
k := newKey(s, name, v)
k.isAutoIncrement = true
s.keys = append(s.keys, k)
s.byName[name] = k
}
return rawName, omitEmpty, allowShadow, allowNonUnique, extends
}

// decodeStruct maps the given value to the matching field of the given section.
Expand All @@ -411,7 +462,7 @@ func (s *Section) decodeStruct(val reflect.Value, isStrict bool, sectionIndex in
continue
}

rawName, _, allowShadow, allowNonUnique, extends := parseTagOptions(tag)
rawName, _, allowShadow, allowNonUnique, extends, autoIncrement := parseTagOptions(tag)
fieldName := s.parseFieldName(tpField.Name, rawName)
if len(fieldName) == 0 || !field.CanSet() {
continue
Expand Down Expand Up @@ -474,6 +525,21 @@ func (s *Section) decodeStruct(val reflect.Value, isStrict bool, sectionIndex in
continue
}

if autoIncrement {
if tpField.Type.Kind() != reflect.Slice || isCustomType(tpField.Type) {
return fmt.Errorf("autoincrement field %q is not a slice", fieldName)
}
keys := s.autoIncrementKeys()
strs := make([]string, len(keys))
for i, k := range keys {
strs[i] = k.String()
}
if err := decodeElems(field, strs, isStrict); err != nil {
return fmt.Errorf("set field %q: %v", fieldName, err)
}
continue
}

if key, err := s.GetKey(fieldName); err == nil {
delim := parseDelim(tpField.Tag.Get("delim"))
if err = decodeValue(tpField.Type, key, field, delim, allowShadow, isStrict); err != nil {
Expand Down Expand Up @@ -553,6 +619,9 @@ func (s *Section) mapTo(v any, isStrict bool) error {
}

// MapTo maps section to given struct.
//
// A slice field tagged ini:",autoincrement" gets the values of the
// section's auto-increment keys, the "- = value" lines, in order.
func (s *Section) MapTo(v any) error {
return s.mapTo(v, false)
}
Expand Down Expand Up @@ -740,7 +809,7 @@ func (s *Section) encodeStruct(val reflect.Value, path []reflect.Type) error {
continue
}

rawName, omitEmpty, allowShadow, allowNonUnique, extends := parseTagOptions(tag)
rawName, omitEmpty, allowShadow, allowNonUnique, extends, autoIncrement := parseTagOptions(tag)
if omitEmpty && isEmptyValue(field) {
continue
}
Expand Down Expand Up @@ -835,6 +904,24 @@ func (s *Section) encodeStruct(val reflect.Value, path []reflect.Type) error {
continue
}

if autoIncrement {
if tpField.Type.Kind() != reflect.Slice || isCustomType(tpField.Type) {
return fmt.Errorf("autoincrement field %q is not a slice", fieldName)
}
if !isSliceElemType(tpField.Type.Elem()) {
return fmt.Errorf("reflect field %q: unsupported type '[]%s'", fieldName, tpField.Type.Elem())
}
vals := make([]string, field.Len())
for i := range vals {
var err error
if vals[i], err = formatValue(field.Index(i)); err != nil {
return fmt.Errorf("reflect field %q: %v", fieldName, err)
}
}
s.setAutoIncrementValues(vals)
continue
}

// Note: Same reason as section.
key, err := s.GetKey(fieldName)
if err != nil {
Expand All @@ -854,6 +941,9 @@ func (s *Section) encodeStruct(val reflect.Value, path []reflect.Type) error {
}

// ReflectFrom reflects section from given struct. It overwrites existing ones.
//
// A slice field tagged ini:",autoincrement" replaces the section's
// auto-increment keys with one "- = value" line for each element.
func (s *Section) ReflectFrom(v any) error {
typ := reflect.TypeOf(v)
val := reflect.ValueOf(v)
Expand Down
75 changes: 74 additions & 1 deletion struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,79 @@ None =
})
}

// https://github.com/go-ini/ini/issues/240
func TestAutoIncrement(t *testing.T) {
type agents struct {
Installed []string `ini:",autoincrement"`
Root string `ini:"root"`
}
type config struct {
Something string `ini:"something"`
Agents agents
}
const data = `something = 0
[Agents]
- = installRoot/agent_1.0.0
root = installRoot
- = installRoot/agent_1.0.1
- = %(root)s/agent_1.0.2
`

t.Run("MapTo", func(t *testing.T) {
var c config
require.NoError(t, StrictMapTo(&c, []byte(data)))
assert.Equal(t, []string{"installRoot/agent_1.0.0", "installRoot/agent_1.0.1", "installRoot/agent_1.0.2"}, c.Agents.Installed)
assert.Equal(t, "installRoot", c.Agents.Root)

// No auto-increment keys leave the field as it is.
c.Agents.Installed = []string{"default"}
require.NoError(t, MapTo(&c, []byte("[Agents]\nroot = x\n")))
assert.Equal(t, []string{"default"}, c.Agents.Installed)
})

t.Run("typed elements", func(t *testing.T) {
var v struct {
Ports []int `ini:",autoincrement"`
}
require.NoError(t, StrictMapTo(&v, []byte("- = 80\n- = 443\n")))
assert.Equal(t, []int{80, 443}, v.Ports)

assert.Error(t, StrictMapTo(&v, []byte("- = 80\n- = http\n")))
require.NoError(t, MapTo(&v, []byte("- = 80\n- = http\n")))
assert.Equal(t, []int{80, 0}, v.Ports)
})

t.Run("ReflectFrom replaces the auto-increment keys", func(t *testing.T) {
f, err := Load([]byte(data))
require.NoError(t, err)
c := config{Something: "1", Agents: agents{Installed: []string{"a", "b, c"}, Root: "r"}}
require.NoError(t, f.ReflectFrom(&c))

var buf bytes.Buffer
_, err = f.WriteTo(&buf)
require.NoError(t, err)
assert.Equal(t, `something = 1

[Agents]
root = r
- = a
- = b, c
`, buf.String())

var back config
require.NoError(t, StrictMapTo(&back, buf.Bytes()))
assert.Equal(t, c, back)
})

t.Run("non-slice field", func(t *testing.T) {
var v struct {
Name string `ini:",autoincrement"`
}
assert.Error(t, MapTo(&v, []byte("- = a\n")))
assert.Error(t, Empty().ReflectFrom(&v))
})
}

type testMapper struct {
PackageName string
}
Expand Down Expand Up @@ -940,7 +1013,7 @@ func tagOptions(t reflect.Type, seen map[reflect.Type]bool) (shadows, nonUnique
}
seen[t] = true
for i := 0; i < t.NumField(); i++ {
_, _, allowShadow, allowNonUnique, _ := parseTagOptions(t.Field(i).Tag.Get("ini"))
_, _, allowShadow, allowNonUnique, _, _ := parseTagOptions(t.Field(i).Tag.Get("ini"))
s, n := tagOptions(t.Field(i).Type, seen)
shadows = shadows || allowShadow || s
nonUnique = nonUnique || allowNonUnique || n
Expand Down
Loading