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

feat-add-field:完善json对象字段 #504

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewTemplate(descs []*protokit.FileDescriptor) *Template {
Messages: make(orderedMessages, 0, len(f.Messages)),
Services: make(orderedServices, 0, len(f.Services)),
Options: mergeOptions(extractOptions(f.GetOptions()), extensions.Transform(f.OptionExtensions)),
Syntax: f.GetSyntax(),
}

for _, e := range f.Enums {
Expand Down Expand Up @@ -141,6 +142,8 @@ type File struct {
Services orderedServices `json:"services"`

Options map[string]interface{} `json:"options,omitempty"`

Syntax string `json:"syntax,omitempty"`
}

// Option returns the named option.
Expand Down Expand Up @@ -236,6 +239,8 @@ type MessageField struct {
DefaultValue string `json:"defaultValue"`

Options map[string]interface{} `json:"options,omitempty"`

Number int `json:"number,omitempty"`
}

// Option returns the named option.
Expand Down Expand Up @@ -482,6 +487,7 @@ func parseMessageField(pf *protokit.FieldDescriptor, oneofDecls []*descriptor.On
DefaultValue: pf.GetDefaultValue(),
Options: mergeOptions(extractOptions(pf.GetOptions()), extensions.Transform(pf.OptionExtensions)),
IsOneof: pf.OneofIndex != nil,
Number: int(pf.GetNumber()),
}

if m.IsOneof {
Expand Down
14 changes: 14 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func TestFileProperties(t *testing.T) {
require.True(t, bookingFile.HasServices)
require.NotEmpty(t, bookingFile.Options)
require.True(t, *bookingFile.Option(E_ExtendFile.Name).(*bool))
require.Equal(t, "", bookingFile.Syntax)
}

func TestFileEnumProperties(t *testing.T) {
Expand Down Expand Up @@ -254,6 +255,7 @@ func TestFieldProperties(t *testing.T) {
require.False(t, field.IsOneof)
require.NotEmpty(t, field.Options)
require.True(t, *field.Option(E_ExtendField.Name).(*bool))
require.Equal(t, 1, field.Number)

field = findField("status_code", msg)
require.Equal(t, "status_code", field.Name)
Expand All @@ -264,6 +266,7 @@ func TestFieldProperties(t *testing.T) {
require.Equal(t, "com.example.BookingStatus.StatusCode", field.FullType)
require.Empty(t, field.DefaultValue)
require.False(t, field.IsOneof)
require.Equal(t, 3, field.Number)

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

field = findField("properties", findMessage("Vehicle", vehicleFile))
require.Equal(t, "properties", field.Name)
Expand All @@ -284,6 +288,7 @@ func TestFieldProperties(t *testing.T) {
require.Empty(t, field.DefaultValue)
require.True(t, field.IsMap)
require.False(t, field.IsOneof)
require.Equal(t, 7, field.Number)

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

field = findField("kilometers", findMessage("Vehicle", vehicleFile))
require.Equal(t, "kilometers", field.Name)
Expand All @@ -303,6 +309,7 @@ func TestFieldProperties(t *testing.T) {
require.False(t, field.IsMap)
require.True(t, field.IsOneof)
require.Equal(t, "travel", field.OneofDecl)
require.Equal(t, 8, field.Number)

field = findField("human_name", findMessage("Vehicle", vehicleFile))
require.Equal(t, "human_name", field.Name)
Expand All @@ -313,6 +320,7 @@ func TestFieldProperties(t *testing.T) {
require.False(t, field.IsMap)
require.True(t, field.IsOneof)
require.Equal(t, "drivers", field.OneofDecl)
require.Equal(t, 11, field.Number)
}

func TestFieldPropertiesProto3(t *testing.T) {
Expand All @@ -327,6 +335,7 @@ func TestFieldPropertiesProto3(t *testing.T) {
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
require.Equal(t, 1, field.Number)

field = findField("model_code", msg)
require.Equal(t, "model_code", field.Name)
Expand All @@ -337,6 +346,7 @@ func TestFieldPropertiesProto3(t *testing.T) {
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
require.Equal(t, 2, field.Number)

field = findField("daily_hire_rate_dollars", msg)
require.Equal(t, "daily_hire_rate_dollars", field.Name)
Expand All @@ -347,6 +357,7 @@ func TestFieldPropertiesProto3(t *testing.T) {
require.Equal(t, "sint32", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
require.Equal(t, 4, field.Number)
}

func TestFieldPropertiesProto3Optional(t *testing.T) {
Expand All @@ -361,6 +372,7 @@ func TestFieldPropertiesProto3Optional(t *testing.T) {
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
require.Equal(t, 1, field.Number)

field = findField("name", msg)
require.Equal(t, "name", field.Name)
Expand All @@ -371,6 +383,7 @@ func TestFieldPropertiesProto3Optional(t *testing.T) {
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
require.Equal(t, 2, field.Number)

field = findField("ingredients", msg)
require.Equal(t, "ingredients", field.Name)
Expand All @@ -381,6 +394,7 @@ func TestFieldPropertiesProto3Optional(t *testing.T) {
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
require.Equal(t, 3, field.Number)
}

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