Skip to content

Commit

Permalink
display all maps as their own key/value
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Jun 18, 2024
1 parent f5233b9 commit c7e9def
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
39 changes: 38 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"sort"
"strings"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/pactus-project/protoc-gen-doc/extensions"
"github.com/pseudomuto/protokit"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)

// Template is a type for encapsulating all the parsed files, messages, fields, enums, services, extensions, etc. into
Expand Down Expand Up @@ -63,6 +63,33 @@ func NewTemplate(descs []*protokit.FileDescriptor) *Template {
addFromMessage(m)
}

/// Post processing the messages
for _, msg := range file.Messages {
for _, f := range msg.Fields {
if f.IsMap {
index, msg := getMessageByName(&file.Messages, f.Type)
if msg == nil || len(msg.Fields) != 2 {
panic(fmt.Sprintf("unable to find key/va;ue for %s", f.Name))
}

keyField := msg.Fields[0]
valueField := msg.Fields[1]
if keyField.Name != "key" {
panic(fmt.Sprintf("expected map entry's first field to be 'key', not '%s'", keyField.Name))
}
if valueField.Name != "value" {
panic(fmt.Sprintf("expected map entry's first field to be 'value', not '%s'", valueField.Name))
}
typeName := fmt.Sprintf("map<%s, %s>", keyField.Type, valueField.Type)
f.Type = typeName
f.FullType = typeName
f.LongType = typeName

file.Messages = append(file.Messages[:index], file.Messages[index+1:]...)
}
}
}

for _, s := range f.Services {
file.Services = append(file.Services, parseService(s))
}
Expand All @@ -78,6 +105,16 @@ func NewTemplate(descs []*protokit.FileDescriptor) *Template {
return &Template{Files: files, Scalars: makeScalars()}
}

func getMessageByName(orderedMessages *orderedMessages, name string) (int, *Message) {
for index, msg := range *orderedMessages {
if msg.Name == name {
return index, msg
}
}

return -1, nil
}

func makeScalars() []*ScalarValue {
var scalars []*ScalarValue
json.Unmarshal(scalarsJSON, &scalars)
Expand Down
6 changes: 3 additions & 3 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ func TestFieldProperties(t *testing.T) {
field = findField("properties", findMessage("Vehicle", vehicleFile))
require.Equal(t, "properties", field.Name)
require.Equal(t, "repeated", field.Label)
require.Equal(t, "PropertiesEntry", field.Type)
require.Equal(t, "Vehicle.PropertiesEntry", field.LongType)
require.Equal(t, "com.example.Vehicle.PropertiesEntry", field.FullType)
require.Equal(t, "map<string, string>", field.Type)
require.Equal(t, "map<string, string>", field.LongType)
require.Equal(t, "map<string, string>", field.FullType)
require.Empty(t, field.DefaultValue)
require.True(t, field.IsMap)
require.False(t, field.IsOneof)
Expand Down

0 comments on commit c7e9def

Please sign in to comment.