Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix map decode #2979

Merged
merged 3 commits into from
Aug 30, 2023
Merged
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
17 changes: 15 additions & 2 deletions encoding/form/proto_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"
)

var ErrInvalidFormatMapKey = errors.New("invalid formatting for map key")
const fieldSeparater = "."

var errInvalidFormatMapKey = errors.New("invalid formatting for map key")

// DecodeValues decode url value into proto message.
func DecodeValues(msg proto.Message, values url.Values) error {
Expand Down Expand Up @@ -91,6 +93,8 @@ func getFieldDescriptor(v protoreflect.Message, fieldName string) protoreflect.F
fd = getDescriptorByFieldAndName(fields, strings.TrimSuffix(fieldName, "[]"))
default:
// If the type is map, you get the string "map[kratos]", where "map" is a field of proto and "kratos" is a key of map
// Use symbol . for separating fields/structs. (eg. structfield.field)
// ref: https://github.com/go-playground/form
field, _, err := parseURLQueryMapKey(fieldName)
if err != nil {
break
Expand Down Expand Up @@ -357,8 +361,17 @@ func parseURLQueryMapKey(key string) (string, string, error) {
startIndex = strings.IndexByte(key, '[')
endIndex = strings.IndexByte(key, ']')
)
if startIndex < 0 {
//nolint:gomnd
values := strings.SplitN(key, fieldSeparater, 2)
//nolint:gomnd
if len(values) != 2 {
return "", "", errInvalidFormatMapKey
}
return values[0], values[1], nil
}
if startIndex <= 0 || startIndex >= endIndex || len(key) != endIndex+1 {
return "", "", ErrInvalidFormatMapKey
return "", "", errInvalidFormatMapKey
}
return key[:startIndex], key[startIndex+1 : endIndex], nil
}
36 changes: 27 additions & 9 deletions encoding/form/proto_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ func TestPopulateMapField(t *testing.T) {
}
}

func TestPopulateMapSepField(t *testing.T) {
query, err := url.ParseQuery("map.name=kratos")
if err != nil {
t.Fatal(err)
}
comp := &complex.Complex{}
field := getFieldDescriptor(comp.ProtoReflect(), "map")
// Fill the comp map field with the url query values
err = populateMapField(field, comp.ProtoReflect().Mutable(field).Map(), []string{"map.name"}, query["map.name"])
if err != nil {
t.Fatal(err)
}
// Get the comp map field value
if query["map.name"][0] != comp.Map["name"] {
t.Errorf("want: %s, got: %s", query, comp.Map)
}
}

func TestParseField(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -230,31 +248,31 @@ func TestParseURLQueryMapKey(t *testing.T) {
fieldName: "map[]", field: "map", fieldKey: "", err: nil,
},
{
fieldName: "", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "[[]", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "[[]", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "map[kratos]=", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "map[kratos]=", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "[kratos]", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "[kratos]", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "map", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "map", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "map[", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "map[", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "]kratos[", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "]kratos[", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "[kratos", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "[kratos", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
{
fieldName: "kratos]", field: "", fieldKey: "", err: ErrInvalidFormatMapKey,
fieldName: "kratos]", field: "", fieldKey: "", err: errInvalidFormatMapKey,
},
}
for _, test := range tests {
Expand Down