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] compatile both map[kratos] and map.kratos for a map type in the quer… #3284

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
16 changes: 6 additions & 10 deletions encoding/form/proto_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ func populateFieldValues(v protoreflect.Message, fieldPath []string, values []st
// ignore unexpected field.
return nil
}

if fd.IsMap() && len(fieldPath) == 2 {
return populateMapField(fd, v.Mutable(fd).Map(), fieldPath, values)
}
if i == len(fieldPath)-1 {
break
}

if fd.Message() == nil || fd.Cardinality() == protoreflect.Repeated {
if fd.IsMap() && len(fieldPath) > 1 {
// post subfield
Expand Down Expand Up @@ -137,20 +138,15 @@ func populateRepeatedField(fd protoreflect.FieldDescriptor, list protoreflect.Li
}

func populateMapField(fd protoreflect.FieldDescriptor, mp protoreflect.Map, fieldPath []string, values []string) error {
var (
nKey = len(fieldPath) - 1 // post sub key
vKey = len(values) - 1
fieldName = fieldPath[nKey]
)
_, keyName, err := parseURLQueryMapKey(fieldName)
_, keyName, err := parseURLQueryMapKey(strings.Join(fieldPath, fieldSeparater))
if err != nil {
return err
}
key, err := parseField(fd.MapKey(), keyName)
if err != nil {
return fmt.Errorf("parsing map key %q: %w", fd.FullName().Name(), err)
}
value, err := parseField(fd.MapValue(), values[vKey])
value, err := parseField(fd.MapValue(), values[len(values)-1])
if err != nil {
return fmt.Errorf("parsing map value %q: %w", fd.FullName().Name(), err)
}
Expand Down Expand Up @@ -363,7 +359,7 @@ func parseURLQueryMapKey(key string) (string, string, error) {
)
if startIndex < 0 {
//nolint:gomnd
values := strings.SplitN(key, fieldSeparater, 2)
values := strings.Split(key, fieldSeparater)
//nolint:gomnd
if len(values) != 2 {
return "", "", errInvalidFormatMapKey
Expand Down
Loading