Skip to content
Closed
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
3 changes: 1 addition & 2 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri
} else {
field.SetInt(intVal)
}
// byte is an alias for uint8, so supporting uint8 breaks support for byte
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
durationVal, err := key.Duration()
// Skip zero value
if err == nil && uint64(durationVal) > 0 {
Expand Down
45 changes: 42 additions & 3 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,17 @@ AllowedIPs = 10.2.0.3/32,fd00:2::3/128
`

type unsupport struct {
Byte byte
Byte complex64
}

type unsupport2 struct {
Others struct {
Cities byte
Cities complex64
}
}

type Unsupport3 struct {
Cities byte
Cities complex64
}

type unsupport4 struct {
Expand Down Expand Up @@ -910,3 +910,42 @@ Title = Consultant Engineer
)
})
}

func TestMapToUint8(t *testing.T) {
type count uint8
type config struct {
Value uint8
Byte byte
Named count
Pointer *uint8
NamedPointer *count
}
for _, value := range []uint8{0, 90, 255} {
for _, strict := range []bool{false, true} {
t.Run(fmt.Sprintf("%d/strict=%t", value, strict), func(t *testing.T) {
source := fmt.Sprintf("[mysection]\nValue=%d\nByte=%d\nNamed=%d\nPointer=%d\nNamedPointer=%d\n", value, value, value, value, value)
cfg, err := Load([]byte(source))
require.NoError(t, err)
var got config
if strict {
err = cfg.Section("mysection").StrictMapTo(&got)
} else {
err = cfg.Section("mysection").MapTo(&got)
}
require.NoError(t, err)
assert.Equal(t, value, got.Value)
assert.Equal(t, value, got.Byte)
assert.Equal(t, count(value), got.Named)
require.NotNil(t, got.Pointer)
assert.Equal(t, value, *got.Pointer)
require.NotNil(t, got.NamedPointer)
assert.Equal(t, count(value), *got.NamedPointer)
reflected := Empty()
require.NoError(t, reflected.Section("mysection").ReflectFrom(&got))
var roundtrip config
require.NoError(t, reflected.Section("mysection").StrictMapTo(&roundtrip))
assert.Equal(t, got, roundtrip)
})
}
}
}