Skip to content

Commit

Permalink
support nested extends
Browse files Browse the repository at this point in the history
  • Loading branch information
joesonw committed May 20, 2023
1 parent f0cf55b commit e89ca79
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
32 changes: 23 additions & 9 deletions example/output.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ option go_package = 'generated/proto;pb';

package example.com;

message Base {
int64 id = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
}


message groups {
message Group {

// ↓↓↓↓↓ extends Intermediate
// ↓↓↓↓↓ extends Base
int64 id = 1;
// ↑↑↑↑↑ extends Base

int64 createdAt = 2;
int64 updatedAt = 3;
// ↑↑↑↑↑ extends Base
// ↑↑↑↑↑ extends Intermediate

string name = 11;
repeated users.User users = 12;
Expand All @@ -30,11 +26,14 @@ message groups {
message users {
message User {

// ↓↓↓↓↓ extends Intermediate
// ↓↓↓↓↓ extends Base
int64 id = 1;
// ↑↑↑↑↑ extends Base

int64 createdAt = 2;
int64 updatedAt = 3;
// ↑↑↑↑↑ extends Base
// ↑↑↑↑↑ extends Intermediate

string name = 11;
string email = 12;
Expand All @@ -44,3 +43,18 @@ message users {

}

message Base {
int64 id = 1;
}

message Intermediate {

// ↓↓↓↓↓ extends Base
int64 id = 1;
// ↑↑↑↑↑ extends Base

int64 createdAt = 2;
int64 updatedAt = 3;
}


2 changes: 0 additions & 2 deletions example/proto/base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ package example.com;

message Base {
int64 id = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
}
3 changes: 1 addition & 2 deletions example/proto/group.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ option go_package = 'generated/proto;pb';
package example.com.groups;

import "proto/user.proto";
import "proto/base.proto";
import "oneproto.proto";

message Group {
option (oneproto.extends) = 'example.com.Base';
option (oneproto.extends) = 'example.com.Intermediate';

string name = 11;
repeated users.User users = 12;
Expand Down
13 changes: 13 additions & 0 deletions example/proto/intermediate.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = 'proto3';
option go_package = 'generated/proto;pb';

package example.com;

import "oneproto.proto";

message Intermediate {
option (oneproto.extends) = 'example.com.Base';

int64 createdAt = 2;
int64 updatedAt = 3;
}
3 changes: 2 additions & 1 deletion example/proto/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package example.com.users;
import "proto/group.proto";
import "oneproto.proto";


message User {
option (oneproto.extends) = 'example.com.Base';
option (oneproto.extends) = 'example.com.Intermediate';

string name = 11;
string email = 12;
Expand Down
30 changes: 18 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,7 @@ func generateMessage(buf *Buffer, indentLevel int, message *descriptorpb.Descrip
indent := strings.Repeat(" ", indentLevel*4)
buf.Printf("%smessage %s {", indent, message.GetName())
generateHeadOptions(buf, indent, message.GetOptions().GetUninterpretedOption())
for _, opt := range message.GetOptions().GetUninterpretedOption() {
if isOptionOneProtoExtends(opt) {
name := trimPackageFromName(string(opt.GetStringValue()))
buf.Printf("%s // ↓↓↓↓↓ extends %s", indent, name)
parent := allMessageDescriptors[name]
for _, field := range parent.GetField() {
buf.Printf("%s %s %s = %d%s;", indent, stringifyField(message, field), field.GetName(), field.GetNumber(), stringifyValueOptions(field.GetOptions().GetUninterpretedOption()))
}
buf.Printf("%s // ↑↑↑↑↑ extends %s", indent, name)
buf.Printf("")
}
}
generateExtendedMessage(buf, indentLevel, message)
for _, field := range message.GetField() {
buf.Printf("%s %s %s = %d%s;", indent, stringifyField(message, field), field.GetName(), field.GetNumber(), stringifyValueOptions(field.GetOptions().GetUninterpretedOption()))
}
Expand All @@ -226,6 +215,23 @@ func generateMessage(buf *Buffer, indentLevel int, message *descriptorpb.Descrip
buf.Printf("%s}", indent)
}

func generateExtendedMessage(buf *Buffer, indentLevel int, message *descriptorpb.DescriptorProto) {
indent := strings.Repeat(" ", indentLevel*4)
for _, opt := range message.GetOptions().GetUninterpretedOption() {
if isOptionOneProtoExtends(opt) {
name := trimPackageFromName(string(opt.GetStringValue()))
buf.Printf("%s // ↓↓↓↓↓ extends %s", indent, name)
parent := allMessageDescriptors[name]
generateExtendedMessage(buf, indentLevel, parent)
for _, field := range parent.GetField() {
buf.Printf("%s %s %s = %d%s;", indent, stringifyField(message, field), field.GetName(), field.GetNumber(), stringifyValueOptions(field.GetOptions().GetUninterpretedOption()))
}
buf.Printf("%s // ↑↑↑↑↑ extends %s", indent, name)
buf.Printf("")
}
}
}

func generateHeadOptions(buf *Buffer, indent string, options []*descriptorpb.UninterpretedOption) {
if len(options) == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion oneproto.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package oneproto;
import 'google/protobuf/descriptor.proto';

extend google.protobuf.MessageOptions {
string extends = 9099;
string extends = 2099;
}

0 comments on commit e89ca79

Please sign in to comment.