Merge 91508b6fe011b2a708ad04f1f5a11018253a3bcb into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
jee 2026-10-25 13:41:07 +08:00 committed by GitHub
commit ebc2f1bcc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -328,8 +328,8 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
return err
}
// If it is a string type, no spaces are removed, and the user data is not modified here
if value.Kind() != reflect.String {
// Preserve string whitespace; defer pointers until their element type is known.
if value.Kind() != reflect.String && value.Kind() != reflect.Ptr {
val = strings.TrimSpace(val)
}

View File

@ -23,6 +23,9 @@ func TestMappingBaseTypes(t *testing.T) {
intPtr := func(i int) *int {
return &i
}
text := " \ttest\n "
textPtr := &text
whitespace := " \t\n "
for _, tt := range []struct {
name string
value any
@ -45,6 +48,14 @@ func TestMappingBaseTypes(t *testing.T) {
{"base type", struct{ F string }{}, "test", string("test")},
{"base type", struct{ F *int }{}, "9", intPtr(9)},
// Preserve string whitespace through collection element pointers.
{"whitespace", struct{ F []*string }{}, text, []*string{&text}},
{"whitespace", struct{ F [1]*string }{}, text, [1]*string{&text}},
{"whitespace", struct{ F []**string }{}, text, []**string{&textPtr}},
{"whitespace only", struct{ F []*string }{}, whitespace, []*string{&whitespace}},
{"whitespace only", struct{ F [1]*string }{}, whitespace, [1]*string{&whitespace}},
{"whitespace", struct{ F []*int }{}, " \t9\n ", []*int{intPtr(9)}},
// zero values
{"zero value", struct{ F int }{}, "", int(0)},
{"zero value", struct{ F uint }{}, "", uint(0)},