mirror of
https://github.com/gone-io/gone.git
synced 2025-01-23 01:00:23 +08:00
feat: test for gone.TagStringParse
This commit is contained in:
parent
b13651e811
commit
1e80a30b07
36
help.go
36
help.go
@ -284,6 +284,24 @@ func (p *Preparer) Test(fn any) {
|
||||
p.testKit().AfterStart(fn).Run()
|
||||
}
|
||||
|
||||
// TagStringParse parse tag string to map
|
||||
// example: "a=1,b=2" -> map[string]string{"a":"1","b":"2"}
|
||||
func TagStringParse(conf string) map[string]string {
|
||||
conf = strings.TrimSpace(conf)
|
||||
specs := strings.Split(conf, ",")
|
||||
m := make(map[string]string)
|
||||
for _, spec := range specs {
|
||||
spec = strings.TrimSpace(spec)
|
||||
pairs := strings.Split(spec, "=")
|
||||
if len(pairs) == 1 && pairs[0] != "" {
|
||||
m[pairs[0]] = ""
|
||||
} else if len(pairs) > 1 && pairs[0] != "" {
|
||||
m[pairs[0]] = pairs[1]
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type provider[T any] struct {
|
||||
Flag
|
||||
cemetery Cemetery `gone:"*"`
|
||||
@ -299,24 +317,6 @@ func (p *provider[T]) Suck(conf string, v reflect.Value, field reflect.StructFie
|
||||
return nil
|
||||
}
|
||||
|
||||
// TagStringParse parse tag string to map
|
||||
// example: "a=1,b=2" -> map[string]string{"a":"1","b":"2"}
|
||||
func TagStringParse(conf string) map[string]string {
|
||||
conf = strings.TrimSpace(conf)
|
||||
specs := strings.Split(conf, ",")
|
||||
m := make(map[string]string)
|
||||
for _, spec := range specs {
|
||||
spec = strings.TrimSpace(spec)
|
||||
pairs := strings.Split(spec, "=")
|
||||
if len(pairs) == 1 {
|
||||
m[pairs[0]] = ""
|
||||
} else if len(pairs) > 1 {
|
||||
m[pairs[0]] = pairs[1]
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// NewProviderPriest create a provider priest function for goner from a function like: `func(tagConf string, injectableStructParam struct{}) (provideType T, err error)`
|
||||
// example:
|
||||
// ```go
|
||||
|
40
help_test.go
40
help_test.go
@ -314,3 +314,43 @@ func TestBuryMockCemetery_Bury(t *testing.T) {
|
||||
tombs := cemetery.GetTomByType(reflect.TypeOf(*point))
|
||||
assert.Equal(t, 2, len(tombs))
|
||||
}
|
||||
|
||||
func TestTagStringParse(t *testing.T) {
|
||||
type args struct {
|
||||
conf string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want map[string]string
|
||||
}{
|
||||
{
|
||||
name: "suc",
|
||||
args: args{
|
||||
conf: "a=1,b=2",
|
||||
},
|
||||
want: map[string]string{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "suc",
|
||||
args: args{
|
||||
conf: "x,a=1,b=2,,,default,ok=",
|
||||
},
|
||||
want: map[string]string{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
"x": "",
|
||||
"default": "",
|
||||
"ok": "",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equalf(t, tt.want, TagStringParse(tt.args.conf), "TagStringParse(%v)", tt.args.conf)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user