Skip to content

Commit

Permalink
Merge pull request #122 from nilslice/pr-121
Browse files Browse the repository at this point in the history
fix: nested message checks, bump proto parser module version
  • Loading branch information
nilslice authored Oct 21, 2019
2 parents 409a348 + 20c36a1 commit ee8cda0
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
protolock
pkg
node_modules
logs
plugin-samples/plugin-sample-js/etc
plugin-samples/plugin-sample-js/plugin-sample-js
plugin-samples/plugin-sample-js/package-lock.json
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/nilslice/protolock

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/proto v1.6.13
github.com/emicklei/proto v1.7.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/proto v1.6.13 h1:8iuAuKbFmFhkmstObb0EV/Hrn9W+x6EuV1y5Da8Ye9E=
github.com/emicklei/proto v1.6.13/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/emicklei/proto v1.7.0 h1:5iEEswaKG9xfE8aqbERdx5MXV/QizS63UvYWga2v9FY=
github.com/emicklei/proto v1.7.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
Expand Down
117 changes: 75 additions & 42 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,25 @@ func getReservedEnumFields(lock Protolock) (lockIDsMap, lockNamesMap) {
return reservedIDMap, reservedNameMap
}

func getFieldIDNameRecursive(fieldIDNameMap lockFieldIDNameMap, filepath Protopath, prefix string, msg Message) {
msgName := prefix + msg.Name
for _, field := range msg.Fields {
if fieldIDNameMap[filepath][msgName] == nil {
fieldIDNameMap[filepath][msgName] = make(map[int]string)
}
fieldIDNameMap[filepath][msgName][field.ID] = field.Name
}
for _, mp := range msg.Maps {
if fieldIDNameMap[filepath][msgName] == nil {
fieldIDNameMap[filepath][msgName] = make(map[int]string)
}
fieldIDNameMap[filepath][msgName][mp.Field.ID] = mp.Field.Name
}
for _, nestedMsg := range msg.Messages {
getFieldIDNameRecursive(fieldIDNameMap, filepath, msgName + nestedPrefix, nestedMsg)
}
}

// getFieldsIDName gets all the fields mapped by the field ID to its name for
// all messages.
func getFieldsIDName(lock Protolock) lockFieldIDNameMap {
Expand All @@ -911,18 +930,7 @@ func getFieldsIDName(lock Protolock) lockFieldIDNameMap {
fieldIDNameMap[def.Filepath] = make(map[string]map[int]string)
}
for _, msg := range def.Def.Messages {
for _, field := range msg.Fields {
if fieldIDNameMap[def.Filepath][msg.Name] == nil {
fieldIDNameMap[def.Filepath][msg.Name] = make(map[int]string)
}
fieldIDNameMap[def.Filepath][msg.Name][field.ID] = field.Name
}
for _, mp := range msg.Maps {
if fieldIDNameMap[def.Filepath][msg.Name] == nil {
fieldIDNameMap[def.Filepath][msg.Name] = make(map[int]string)
}
fieldIDNameMap[def.Filepath][msg.Name][mp.Field.ID] = mp.Field.Name
}
getFieldIDNameRecursive(fieldIDNameMap, def.Filepath, "", msg)
}
}

Expand Down Expand Up @@ -951,6 +959,25 @@ func getEnumFieldsIDName(lock Protolock) lockFieldIDNameMap {
return fieldIDNameMap
}

func getNonReservedFieldsRecursive(nameIDMap lockNamesMap, filepath Protopath, prefix string, msg Message) {
msgName := prefix + msg.Name
for _, field := range msg.Fields {
if nameIDMap[filepath][msgName] == nil {
nameIDMap[filepath][msgName] = make(map[string]int)
}
nameIDMap[filepath][msgName][field.Name] = field.ID
}
for _, mp := range msg.Maps {
if nameIDMap[filepath][msgName] == nil {
nameIDMap[filepath][msgName] = make(map[string]int)
}
nameIDMap[filepath][msgName][mp.Field.Name] = mp.Field.ID
}
for _, nestedMsg := range msg.Messages {
getNonReservedFieldsRecursive(nameIDMap, filepath, msgName+nestedPrefix, nestedMsg)
}
}

// getNonReservedFields gets all the non-reserved field numbers and names, and
// stashes them in a lockNamesMap to be checked against.
func getNonReservedFields(lock Protolock) lockNamesMap {
Expand All @@ -961,18 +988,7 @@ func getNonReservedFields(lock Protolock) lockNamesMap {
nameIDMap[def.Filepath] = make(map[string]map[string]int)
}
for _, msg := range def.Def.Messages {
for _, field := range msg.Fields {
if nameIDMap[def.Filepath][msg.Name] == nil {
nameIDMap[def.Filepath][msg.Name] = make(map[string]int)
}
nameIDMap[def.Filepath][msg.Name][field.Name] = field.ID
}
for _, mp := range msg.Maps {
if nameIDMap[def.Filepath][msg.Name] == nil {
nameIDMap[def.Filepath][msg.Name] = make(map[string]int)
}
nameIDMap[def.Filepath][msg.Name][mp.Field.Name] = mp.Field.ID
}
getNonReservedFieldsRecursive(nameIDMap, def.Filepath, "", msg)
}
}

Expand Down Expand Up @@ -1001,6 +1017,20 @@ func getNonReservedEnumFields(lock Protolock) lockNamesMap {
return nameIDMap
}

func getMapMapRecursive(nameTypeMap lockMapMap, filepath Protopath, prefix string, msg Message) {
msgName := prefix + msg.Name
for _, mp := range msg.Maps {
if nameTypeMap[filepath][msgName] == nil {
nameTypeMap[filepath][msgName] = make(map[string]Map)
}
nameTypeMap[filepath][msgName][mp.Field.Name] = mp
}
for _, nestedMsg := range msg.Messages {

getMapMapRecursive(nameTypeMap, filepath, msgName + nestedPrefix, nestedMsg)
}
}

// getMapMap gets all the map names and types, and stashes them in a
// lockMapMap to be checked against.
func getMapMap(lock Protolock) lockMapMap {
Expand All @@ -1011,18 +1041,32 @@ func getMapMap(lock Protolock) lockMapMap {
nameTypeMap[def.Filepath] = make(map[string]map[string]Map)
}
for _, msg := range def.Def.Messages {
for _, mp := range msg.Maps {
if nameTypeMap[def.Filepath][msg.Name] == nil {
nameTypeMap[def.Filepath][msg.Name] = make(map[string]Map)
}
nameTypeMap[def.Filepath][msg.Name][mp.Field.Name] = mp
}
getMapMapRecursive(nameTypeMap, def.Filepath, "", msg)
}
}

return nameTypeMap
}

func getFieldMapRecursive(nameTypeMap lockFieldMap, filepath Protopath, prefix string, msg Message) {
msgName := prefix + msg.Name
for _, field := range msg.Fields {
if nameTypeMap[filepath][msgName] == nil {
nameTypeMap[filepath][msgName] = make(map[string]Field)
}
nameTypeMap[filepath][msgName][field.Name] = field
}
for _, mp := range msg.Maps {
if nameTypeMap[filepath][msgName] == nil {
nameTypeMap[filepath][msgName] = make(map[string]Field)
}
nameTypeMap[filepath][msgName][mp.Field.Name] = mp.Field
}
for _, nestedMsg := range msg.Messages {
getFieldMapRecursive(nameTypeMap, filepath, msgName + nestedPrefix, nestedMsg)
}
}

// getFieldMap gets all the field names and types, and stashes them in a
// lockFieldMap to be checked against.
func getFieldMap(lock Protolock) lockFieldMap {
Expand All @@ -1033,18 +1077,7 @@ func getFieldMap(lock Protolock) lockFieldMap {
nameTypeMap[def.Filepath] = make(map[string]map[string]Field)
}
for _, msg := range def.Def.Messages {
for _, field := range msg.Fields {
if nameTypeMap[def.Filepath][msg.Name] == nil {
nameTypeMap[def.Filepath][msg.Name] = make(map[string]Field)
}
nameTypeMap[def.Filepath][msg.Name][field.Name] = field
}
for _, mp := range msg.Maps {
if nameTypeMap[def.Filepath][msg.Name] == nil {
nameTypeMap[def.Filepath][msg.Name] = make(map[string]Field)
}
nameTypeMap[def.Filepath][msg.Name][mp.Field.Name] = mp.Field
}
getFieldMapRecursive(nameTypeMap, def.Filepath, "", msg)
}
}

Expand Down
Loading

0 comments on commit ee8cda0

Please sign in to comment.