Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Dec 17, 2024
1 parent 3dff90b commit 32fded4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
49 changes: 30 additions & 19 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,8 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value,
return nil
}

if refValue.Len() == 1 {
element := refValue.Index(0)
if element.Kind() == reflect.String {
if val, ok := element.Interface().(string); ok {
splits := strings.Split(val, comma)
if len(splits) > 1 {
refValue = makeStringSlice(splits)
}
}
}
if u.opts.fromArray {
refValue = makeStringSlice(refValue)
}

var valid bool
Expand Down Expand Up @@ -461,15 +453,6 @@ func (u *Unmarshaler) implementsUnmarshaler(t reflect.Type) bool {
}
}

func makeStringSlice(vals []string) reflect.Value {
slice := reflect.MakeSlice(stringSliceType, len(vals), len(vals))
for i, split := range vals {
slice.Index(i).Set(reflect.ValueOf(split))
}

return slice
}

func (u *Unmarshaler) parseOptionsWithContext(field reflect.StructField, m Valuer, fullName string) (
string, *fieldOptionsWithContext, error) {
key, options, err := parseKeyAndOptions(u.key, field)
Expand Down Expand Up @@ -1178,6 +1161,34 @@ func join(elem ...string) string {
return builder.String()
}

func makeStringSlice(refValue reflect.Value) reflect.Value {
if refValue.Len() != 1 {
return refValue
}

element := refValue.Index(0)
if element.Kind() != reflect.String {
return refValue
}

val, ok := element.Interface().(string)
if !ok {
return refValue
}

splits := strings.Split(val, comma)
if len(splits) <= 1 {
return refValue
}

slice := reflect.MakeSlice(stringSliceType, len(splits), len(splits))
for i, split := range splits {
slice.Index(i).Set(reflect.ValueOf(split))
}

return slice
}

func newInitError(name string) error {
return fmt.Errorf("field %q is not set", name)
}
Expand Down
14 changes: 7 additions & 7 deletions tools/goctl/pkg/parser/api/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
)

const (
idAPI = "api"
groupKeyText = "group"
infoTitleKey = "Title"
infoDescKey = "Desc"
infoVersionKey = "Version"
infoAuthorKey = "Author"
infoEmailKey = "Email"
idAPI = "api"
groupKeyText = "group"
infoTitleKey = "Title"
infoDescKey = "Desc"
infoVersionKey = "Version"
infoAuthorKey = "Author"
infoEmailKey = "Email"
)

// Parser is the parser for api file.
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/pkg/parser/api/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestParser_Parse_atServerStmt(t *testing.T) {
"prefix3:": "v1/v2_",
"prefix4:": "a-b-c",
"summary:": `"test"`,
"key:": `"bar"`,
"key:": `"bar"`,
}

p := New("foo.api", atServerTestAPI)
Expand Down

0 comments on commit 32fded4

Please sign in to comment.