fix: test failure

master
kevin 2 years ago committed by Kevin Wan
parent fb7664a764
commit a972f400c6

@ -13,7 +13,10 @@ import (
"github.com/zeromicro/go-zero/internal/encoding" "github.com/zeromicro/go-zero/internal/encoding"
) )
const jsonTagKey = "json" const (
jsonTagKey = "json"
jsonTagSep = ','
)
var ( var (
fillDefaultUnmarshaler = mapping.NewUnmarshaler(jsonTagKey, mapping.WithDefault()) fillDefaultUnmarshaler = mapping.NewUnmarshaler(jsonTagKey, mapping.WithDefault())
@ -257,7 +260,14 @@ func buildStructFieldsInfo(tp reflect.Type) (*fieldInfo, error) {
func getTagName(field reflect.StructField) string { func getTagName(field reflect.StructField) string {
if tag, ok := field.Tag.Lookup(jsonTagKey); ok { if tag, ok := field.Tag.Lookup(jsonTagKey); ok {
return tag if pos := strings.IndexByte(tag, jsonTagSep); pos >= 0 {
tag = tag[:pos]
}
tag = strings.TrimSpace(tag)
if len(tag) > 0 {
return tag
}
} }
return field.Name return field.Name

@ -1076,44 +1076,86 @@ func TestFillDefaultUnmarshal(t *testing.T) {
func TestConfigWithJsonTag(t *testing.T) { func TestConfigWithJsonTag(t *testing.T) {
t.Run("map with value", func(t *testing.T) { t.Run("map with value", func(t *testing.T) {
var input = []byte(`[BannedNotificationTemplates] var input = []byte(`[Value]
[BannedNotificationTemplates.pt-BR] [Value.first]
EmailTemplate = "910707,2,3,4" Email = "foo"
[BannedNotificationTemplates.ch-MY] [Value.second]
EmailTemplate = "910707,2,3,4"`) Email = "bar"`)
type BannedNotificationTemplates struct { type Value struct {
EmailTemplate string Email string
} }
type Config struct { type Config struct {
BannedNotificationTemplatesMap map[string]BannedNotificationTemplates `json:"BannedNotificationTemplates"` // 各个语言的封禁模板设置, map.key=语言 ValueMap map[string]Value `json:"Value"`
} }
var c Config var c Config
if assert.NoError(t, LoadFromTomlBytes(input, &c)) { if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
assert.Len(t, c.BannedNotificationTemplatesMap, 2) assert.Len(t, c.ValueMap, 2)
} }
}) })
t.Run("map with ptr value", func(t *testing.T) { t.Run("map with ptr value", func(t *testing.T) {
var input = []byte(`[BannedNotificationTemplates] var input = []byte(`[Value]
[BannedNotificationTemplates.pt-BR] [Value.first]
EmailTemplate = "910707,2,3,4" Email = "foo"
[BannedNotificationTemplates.ch-MY] [Value.second]
EmailTemplate = "910707,2,3,4"`) Email = "bar"`)
type BannedNotificationTemplates struct { type Value struct {
EmailTemplate string Email string
} }
type Config struct { type Config struct {
BannedNotificationTemplatesMap map[string]*BannedNotificationTemplates `json:"BannedNotificationTemplates"` // 各个语言的封禁模板设置, map.key=语言 ValueMap map[string]*Value `json:"Value"`
} }
var c Config var c Config
if assert.NoError(t, LoadFromTomlBytes(input, &c)) { if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
assert.Len(t, c.BannedNotificationTemplatesMap, 2) assert.Len(t, c.ValueMap, 2)
}
})
t.Run("map with optional", func(t *testing.T) {
var input = []byte(`[Value]
[Value.first]
Email = "foo"
[Value.second]
Email = "bar"`)
type Value struct {
Email string
}
type Config struct {
Value map[string]Value `json:",optional"`
}
var c Config
if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
assert.Len(t, c.Value, 2)
}
})
t.Run("map with empty tag", func(t *testing.T) {
var input = []byte(`[Value]
[Value.first]
Email = "foo"
[Value.second]
Email = "bar"`)
type Value struct {
Email string
}
type Config struct {
Value map[string]Value `json:" "`
}
var c Config
if assert.NoError(t, LoadFromTomlBytes(input, &c)) {
assert.Len(t, c.Value, 2)
} }
}) })
} }

@ -255,7 +255,7 @@ func parseGroupedSegments(val string) []string {
// don't modify returned fieldOptions, it's cached and shared among different calls. // don't modify returned fieldOptions, it's cached and shared among different calls.
func parseKeyAndOptions(tagName string, field reflect.StructField) (string, *fieldOptions, error) { func parseKeyAndOptions(tagName string, field reflect.StructField) (string, *fieldOptions, error) {
value := field.Tag.Get(tagName) value := strings.TrimSpace(field.Tag.Get(tagName))
if len(value) == 0 { if len(value) == 0 {
return field.Name, nil, nil return field.Name, nil, nil
} }

@ -144,6 +144,10 @@ func TestParseSegments(t *testing.T) {
input: "", input: "",
expect: []string{}, expect: []string{},
}, },
{
input: " ",
expect: []string{},
},
{ {
input: ",", input: ",",
expect: []string{""}, expect: []string{""},

Loading…
Cancel
Save