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

Support oneof in templates #436

Merged
merged 1 commit into from
Jan 22, 2021
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
75 changes: 75 additions & 0 deletions examples/templates/grpc-md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# API Reference

# Table of Contents
{{range .Files}}
{{if .HasServices}}
- Services
{{range .Services}} - [{{.Name}}](#{{.FullName | lower | replace "." ""}})
{{end}}
{{end}}
{{if .HasMessages}}
- Messages
{{range .Messages}} - [{{.LongName}}](#{{.LongName | lower | replace "." ""}})
{{end}}
{{end}}
{{if .HasEnums}}
- Enums
{{range .Enums}} - [{{.LongName}}](#{{.LongName | lower | replace "." ""}})
{{end}}
{{end}}
{{end}}
- [Scalar Value Types](#scalar-value-types)

{{range .Files}}

{{range .Services -}}
# {{.Name}} {#{{.FullName | lower | replace "." ""}}}
{{.Description}}

{{range .Methods -}}
## {{.Name}}

> **rpc** {{.Name}}([{{.RequestLongType}}](#{{.RequestLongType | lower | replace "." ""}}))
[{{.ResponseLongType}}](#{{.ResponseLongType | lower | replace "." ""}})

{{ .Description}}
{{end}} <!-- end methods -->
{{end}} <!-- end services -->

# Messages
{{range .Messages}}

## {{.LongName}} {#{{.LongName | lower | replace "." ""}}}
{{.Description}}

{{if .HasFields}}
| Field | Type | Description |
| ----- | ---- | ----------- |
{{range .Fields -}}
| {{if .IsOneof}}[**oneof**](https://developers.google.com/protocol-buffers/docs/proto3#oneof) {{.OneofDecl}}.{{end}}{{.Name}} | [{{if .IsMap}}map {{else}}{{.Label}} {{end}}{{.LongType}}](#{{.LongType | lower | replace "." ""}}) | {{if .Description}}{{nobr .Description}}{{if .DefaultValue}} Default: {{.DefaultValue}}{{end}}{{else}}none{{end}} |
{{end}} <!-- end Fields -->
{{end}} <!-- end HasFields -->
{{end}} <!-- end messages -->

# Enums
{{range .Enums}}

## {{.LongName}} {#{{.LongName | lower | replace "." ""}}}
{{.Description}}

| Name | Number | Description |
| ---- | ------ | ----------- |
{{range .Values -}}
| {{.Name}} | {{.Number}} | {{if .Description}}{{nobr .Description}}{{else}}none{{end}} |
{{end}}

{{end}} <!-- end Enums -->
{{end}} <!-- end Files -->

# Scalar Value Types

| .proto Type | Notes | C++ Type | Java Type | Python Type |
| ----------- | ----- | -------- | --------- | ----------- |
{{range .Scalars -}}
| <div><h4 id="{{.ProtoType | lower | replace "." ""}}" /></div><a name="{{.ProtoType}}" /> {{.ProtoType}} | {{.Notes}} | {{.CppType}} | {{.JavaType}} | {{.PythonType}} |
{{end}}
10 changes: 10 additions & 0 deletions fixtures/Vehicle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ message Vehicle {
repeated sint32 rates = 6;

map<string, string> properties = 7; // bag of properties related to the vehicle.

oneof travel {
int32 kilometers = 8;
int64 lightyears = 10;
}

oneof drivers {
string human_name = 11;
string cat_name = 12;
}
}
Binary file modified fixtures/cookie.pb
Binary file not shown.
Binary file modified fixtures/fileset.pb
Binary file not shown.
13 changes: 11 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ type Message struct {

HasExtensions bool `json:"hasExtensions"`
HasFields bool `json:"hasFields"`
HasOneofs bool `json:"hasOneofs"`

Extensions []*MessageExtension `json:"extensions"`
Fields []*MessageField `json:"fields"`
Expand Down Expand Up @@ -232,6 +233,8 @@ type MessageField struct {
LongType string `json:"longType"`
FullType string `json:"fullType"`
IsMap bool `json:"ismap"`
IsOneof bool `json:"isoneof"`
OneofDecl string `json:"oneofdecl"`
DefaultValue string `json:"defaultValue"`

Options map[string]interface{} `json:"options,omitempty"`
Expand Down Expand Up @@ -442,6 +445,7 @@ func parseMessage(pm *protokit.Descriptor) *Message {
Description: description(pm.GetComments().String()),
HasExtensions: len(pm.GetExtensions()) > 0,
HasFields: len(pm.GetMessageFields()) > 0,
HasOneofs: len(pm.GetOneofDecl()) > 0,
Extensions: make([]*MessageExtension, 0, len(pm.Extensions)),
Fields: make([]*MessageField, 0, len(pm.Fields)),
Options: mergeOptions(extractOptions(pm.GetOptions()), extensions.Transform(pm.OptionExtensions)),
Expand All @@ -452,7 +456,7 @@ func parseMessage(pm *protokit.Descriptor) *Message {
}

for _, f := range pm.Fields {
msg.Fields = append(msg.Fields, parseMessageField(f))
msg.Fields = append(msg.Fields, parseMessageField(f, pm.GetOneofDecl()))
}

return msg
Expand All @@ -467,7 +471,7 @@ func parseMessageExtension(pe *protokit.ExtensionDescriptor) *MessageExtension {
}
}

func parseMessageField(pf *protokit.FieldDescriptor) *MessageField {
func parseMessageField(pf *protokit.FieldDescriptor, oneofDecls []*descriptor.OneofDescriptorProto) *MessageField {
t, lt, ft := parseType(pf)

m := &MessageField{
Expand All @@ -479,6 +483,11 @@ func parseMessageField(pf *protokit.FieldDescriptor) *MessageField {
FullType: ft,
DefaultValue: pf.GetDefaultValue(),
Options: mergeOptions(extractOptions(pf.GetOptions()), extensions.Transform(pf.OptionExtensions)),
IsOneof: pf.OneofIndex != nil,
}

if m.IsOneof {
m.OneofDecl = oneofDecls[pf.GetOneofIndex()].GetName()
}

// Check if this is a map.
Expand Down
25 changes: 25 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func TestFieldProperties(t *testing.T) {
require.Equal(t, "int32", field.LongType)
require.Equal(t, "int32", field.FullType)
require.Empty(t, field.DefaultValue)
require.False(t, field.IsOneof)
require.NotEmpty(t, field.Options)
require.True(t, *field.Option(E_ExtendField.Name).(*bool))

Expand All @@ -262,6 +263,7 @@ func TestFieldProperties(t *testing.T) {
require.Equal(t, "BookingStatus.StatusCode", field.LongType)
require.Equal(t, "com.example.BookingStatus.StatusCode", field.FullType)
require.Empty(t, field.DefaultValue)
require.False(t, field.IsOneof)

field = findField("category", findMessage("Vehicle", vehicleFile))
require.Equal(t, "category", field.Name)
Expand All @@ -271,6 +273,7 @@ func TestFieldProperties(t *testing.T) {
require.Equal(t, "Vehicle.Category", field.LongType)
require.Equal(t, "com.example.Vehicle.Category", field.FullType)
require.Empty(t, field.DefaultValue)
require.False(t, field.IsOneof)

field = findField("properties", findMessage("Vehicle", vehicleFile))
require.Equal(t, "properties", field.Name)
Expand All @@ -280,6 +283,7 @@ func TestFieldProperties(t *testing.T) {
require.Equal(t, "com.example.Vehicle.PropertiesEntry", field.FullType)
require.Empty(t, field.DefaultValue)
require.True(t, field.IsMap)
require.False(t, field.IsOneof)

field = findField("rates", findMessage("Vehicle", vehicleFile))
require.Equal(t, "rates", field.Name)
Expand All @@ -288,6 +292,27 @@ func TestFieldProperties(t *testing.T) {
require.Equal(t, "sint32", field.LongType)
require.Equal(t, "sint32", field.FullType)
require.False(t, field.IsMap)
require.False(t, field.IsOneof)

field = findField("kilometers", findMessage("Vehicle", vehicleFile))
require.Equal(t, "kilometers", field.Name)
require.Equal(t, "", field.Label)
require.Equal(t, "int32", field.Type)
require.Equal(t, "int32", field.LongType)
require.Equal(t, "int32", field.FullType)
require.False(t, field.IsMap)
require.True(t, field.IsOneof)
require.Equal(t, "travel", field.OneofDecl)

field = findField("human_name", findMessage("Vehicle", vehicleFile))
require.Equal(t, "human_name", field.Name)
require.Equal(t, "", field.Label)
require.Equal(t, "string", field.Type)
require.Equal(t, "string", field.LongType)
require.Equal(t, "string", field.FullType)
require.False(t, field.IsMap)
require.True(t, field.IsOneof)
require.Equal(t, "drivers", field.OneofDecl)
}

func TestFieldPropertiesProto3(t *testing.T) {
Expand Down