Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Unified Diff: client/flagpb/unmarshal.go

Issue 1940683002: client/flagpb: fix unmarshaling of maps (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@flagpb-parse-value
Patch Set: -mapcase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | client/flagpb/unmarshal_test.desc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/flagpb/unmarshal.go
diff --git a/client/flagpb/unmarshal.go b/client/flagpb/unmarshal.go
index c2a5486e639ed4975a0705d5cd79b2f869d37f82..b02b47bebf6b0e7be27f6e0bc89c579bccad2662 100644
--- a/client/flagpb/unmarshal.go
+++ b/client/flagpb/unmarshal.go
@@ -93,14 +93,20 @@ func (p *parser) parseOneFlag(flags []string, root message) (flagsRest []string,
lastMsg := pathMsgs[len(pathMsgs)-1]
target = &lastMsg.message
}
-
- // Resolve last field name.
name := fieldPath[len(fieldPath)-1]
- fi := target.desc.FindField(name)
- if fi == -1 {
- return nil, fmt.Errorf("field %s not found in message %s", name, target.desc.GetName())
+
+ // Resolve target field.
+ var fieldIndex int
+ if target.desc.GetOptions().GetMapEntry() {
+ if fieldIndex = target.desc.FindField("value"); fieldIndex == -1 {
+ return nil, fmt.Errorf("map entry type %s does not have value field", target.desc.GetName())
+ }
+ } else {
+ if fieldIndex = target.desc.FindField(name); fieldIndex == -1 {
+ return nil, fmt.Errorf("field %s not found in message %s", name, target.desc.GetName())
+ }
}
- field := target.desc.Field[fi]
+ field := target.desc.Field[fieldIndex]
var value interface{}
hasValue := false
@@ -176,12 +182,19 @@ func (p *parser) subMessages(root message, path []string) ([]subMsg, error) {
parent := &root
for i, name := range path {
curPath := path[:i+1]
- fi := parent.desc.FindField(name)
- if fi == -1 {
- return nil, fmt.Errorf("field %q not found in message %s", name, parent.desc.GetName())
+
+ var fieldIndex int
+ if parent.desc.GetOptions().GetMapEntry() {
+ if fieldIndex = parent.desc.FindField("value"); fieldIndex == -1 {
+ return nil, fmt.Errorf("map entry type %s does not have value field", parent.desc.GetName())
+ }
+ } else {
+ if fieldIndex = parent.desc.FindField(name); fieldIndex == -1 {
+ return nil, fmt.Errorf("field %q not found in message %s", name, parent.desc.GetName())
+ }
nodir 2016/04/30 04:43:58 deduping this would causes an obscure abstraction
}
- f := parent.desc.Field[fi]
+ f := parent.desc.Field[fieldIndex]
if f.GetType() != descriptor.FieldDescriptorProto_TYPE_MESSAGE {
return nil, fmt.Errorf("field %s is not a message", strings.Join(curPath, "."))
}
@@ -197,7 +210,7 @@ func (p *parser) subMessages(root message, path []string) ([]subMsg, error) {
sub := subMsg{
message: message{desc: subDesc},
- repeated: f.Repeated(),
+ repeated: f.Repeated() && !subDesc.GetOptions().GetMapEntry(),
path: curPath,
}
if value, ok := parent.data[name]; !ok {
« no previous file with comments | « no previous file | client/flagpb/unmarshal_test.desc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698