Skip to content

Commit

Permalink
merge nested namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
joesonw committed May 20, 2023
1 parent ea34c37 commit 35cfbc5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 19 deletions.
2 changes: 1 addition & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all
all:
go install github.com/joesonw/oneproto
oneproto -I ./proto -O output.proto -T template.proto -P example.com ./proto
oneproto --options -I ./proto -O output.proto -T template.proto -P example.com ./proto
21 changes: 20 additions & 1 deletion example/output.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ message Base {

// extends example.com.Base
message Intermediate {
option (test) = 'hello';

int64 id = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
}


message groups {
// extends example.com.Intermediate
message Group {
option (test) = 'hello';

int64 id = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
Expand All @@ -34,6 +37,8 @@ message groups {
message users {
// extends example.com.Intermediate
message User {
option (test) = 'hello';

int64 id = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
Expand All @@ -43,5 +48,19 @@ message users {
repeated groups.Group groups = 14;
}

message nested {
// extends example.com.Intermediate
message Test {
option (test) = 'hello';

int64 id = 1;
int64 createdAt = 2;
int64 updatedAt = 3;
string name = 11;
}

}

}


14 changes: 14 additions & 0 deletions example/proto/nested.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = 'proto3';
option go_package = 'generated/proto;pb';

package example.com.users.nested;

import "proto/group.proto";
import "oneproto.proto";


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

string name = 11;
}
66 changes: 49 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ var (
parentResolvedMap = map[*descriptorpb.DescriptorProto]bool{}
)

type PackageFileGroup struct {
name string
fullName string
files []*descriptorpb.FileDescriptorProto
subPackages map[string]*PackageFileGroup
}

func main() {
pflag.Parse()
if *pTemplate == "" || *pOutput == "" {
Expand Down Expand Up @@ -68,47 +75,72 @@ func main() {
buf.Printf(string(templateContent))

// group files by package name
packageFileGroups := make(map[string][]*descriptorpb.FileDescriptorProto)
for _, file := range files {
packageFileGroups[file.GetPackage()] = append(packageFileGroups[file.GetPackage()], file)
for _, message := range file.GetMessageType() {
allMessageDescriptors[trimPackageFromName(fmt.Sprintf("%s.%s", file.GetPackage(), message.GetName()))] = message
}
packageFileGroup := &PackageFileGroup{
subPackages: map[string]*PackageFileGroup{},
}

for pkg, packageFiles := range packageFileGroups {
for _, file := range files {
pkg := file.GetPackage()
var paths []string
if pkg != *pPackage {
paths = strings.Split(trimPackageFromName(pkg), ".")
}

if len(paths) == 0 {
packageFileGroup.files = append(packageFileGroup.files, file)
} else {
var currentGroup = packageFileGroup
for i, path := range paths {
buf.Printf("%smessage %s {", strings.Repeat(" ", i*4), path)
if currentGroup.subPackages[path] == nil {
currentGroup.subPackages[path] = &PackageFileGroup{
name: path,
fullName: strings.Join(paths[:i+1], "."),
subPackages: map[string]*PackageFileGroup{},
}
}
currentGroup = currentGroup.subPackages[path]
}
currentGroup.files = append(currentGroup.files, file)
}
indentLevel := len(paths)

for _, file := range packageFiles {
for _, message := range file.GetMessageType() {
allMessageDescriptors[trimPackageFromName(fmt.Sprintf("%s.%s", file.GetPackage(), message.GetName()))] = message
}
}

var iteratePackageGroup func(group *PackageFileGroup, identLevel int)
iteratePackageGroup = func(group *PackageFileGroup, identLevel int) {
if group.name != "" {
buf.Printf("%smessage %s {", strings.Repeat(" ", identLevel*4), group.name)
}

for _, file := range group.files {
for _, enum := range file.EnumType {
oneprotou_til.GenerateEnum(buf, indentLevel, enum)
oneprotou_til.GenerateEnum(buf, identLevel+1, enum)
buf.Printf("")
}

for _, service := range file.Service {
oneprotou_til.GenerateService(buf, indentLevel, service)
oneprotou_til.GenerateService(buf, identLevel+1, service)
buf.Printf("")
}

for _, message := range file.GetMessageType() {
resolveMessageExtends(buf, indentLevel, message)
oneprotou_til.GenerateMessage(buf, indentLevel, message)
resolveMessageExtends(buf, identLevel+1, message)
oneprotou_til.GenerateMessage(buf, identLevel+1, message)
buf.Printf("")
}
}

for i := range paths {
buf.Printf("%s}", strings.Repeat(" ", i*4))
for _, subPackage := range group.subPackages {
iteratePackageGroup(subPackage, identLevel+1)
}
if group.name != "" {
buf.Printf("%s}", strings.Repeat(" ", identLevel*4))
}
buf.Printf("")
}
iteratePackageGroup(packageFileGroup, -1)

if err := os.WriteFile(*pOutput, buf.Bytes(), 0644); err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 35cfbc5

Please sign in to comment.