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

Generator #1

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ test-compile: build-protoc-gen-litepb
rm -rf ./test/generated/*
go mod init -C ./test/generated generated
protoc --plugin ./bin/protoc-gen-litepb --litepb_out ./test/ --proto_path=./test/proto/ ./test/proto/test.proto
#protoc --plugin ./bin/protoc-gen-litepb --litepb_out ./test/ --proto_path=./test/proto/ ./test/proto/test4.proto

test-compile-google:
protoc --proto_path=./test/proto/ --go_out ./test/ ./test/proto/test.proto

build-protoc-gen-litepb:
go build -tags debug -o ./bin/protoc-gen-litepb ./cmd/protoc-gen-litepb

build-plugin-proto:
protoc \
--proto_path=pkg/plugin\
--go_out ./pkg/plugin/\
--go_opt=paths=source_relative \
./pkg/plugin/plugin.proto
66 changes: 66 additions & 0 deletions cmd/qtest/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
_ "embed"
"os"
"path"
"path/filepath"
"strings"
"time"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"

"github.com/e-tape/litepb/pkg/generator"
"github.com/e-tape/litepb/pkg/stderr"
)

func main() {
if err := run(); err != nil {
stderr.Failf("%s: %s", filepath.Base(os.Args[0]), err)
}
}

func run() error {
in, err := os.ReadFile(`bin/1.bin`)
if err != nil {
return err
}

request := &pluginpb.CodeGeneratorRequest{}
if err = proto.Unmarshal(in, request); err != nil {
return err
}

stderr.Logf("COMPILER: %s", request.GetCompilerVersion())
stderr.Logf("FILES TO GENERATE: %s", strings.Join(request.GetFileToGenerate(), ", "))

start := time.Now()
response := generator.NewGenerator(request).Generate()
stderr.Logf("GENERATED IN: %s", time.Since(start))

for _, f := range response.File {
err = os.MkdirAll(`test/`+path.Dir(f.GetName()), 0766)
if err != nil {
panic(err)
}
err = os.WriteFile(`test/`+f.GetName(), []byte(f.GetContent()), 0666)
if err != nil {
panic(err)
}
}
return nil

generator.GoFmt(response)

out, err := proto.Marshal(response)
if err != nil {
return err
}

if _, err = os.Stdout.Write(out); err != nil {
return err
}

return nil
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/e-tape/litepb

go 1.22.3

require google.golang.org/protobuf v1.34.1
require (
golang.org/x/text v0.16.0
google.golang.org/protobuf v1.34.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
Expand Down
1 change: 1 addition & 0 deletions pkg/common/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// SnakeCaseToPascalCase converts snake_case to PascalCase
func SnakeCaseToPascalCase(text string) string {
// TODO rathil refactoring or add trim "_"
if text == "" {
return ""
}
Expand Down
40 changes: 27 additions & 13 deletions pkg/generator/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,44 @@ package generator
import (
"slices"
"strings"

"google.golang.org/protobuf/types/descriptorpb"
)

func (g *FileGenerator) findMessageComments(sourceCodePath []int32, messageIndex int) string {
return findComments(g.sourceCodeInfo, append(sourceCodePath, int32(messageIndex)))
func (a *generatorFile) findMessageComments(
sourceCodePath []int32,
messageIndex int,
) string {
return a.findComments(append(sourceCodePath, int32(messageIndex)))
}

func (g *FileGenerator) findMessageFieldComments(sourceCodePath []int32, messageIndex, fieldIndex int) string {
return findComments(g.sourceCodeInfo, append(sourceCodePath, int32(messageIndex), 2, int32(fieldIndex)))
func (a *generatorFile) findMessageFieldComments(
sourceCodePath []int32,
messageIndex int,
fieldIndex int,
) string {
// TODO rathil move 2 to const
return a.findComments(append(sourceCodePath, int32(messageIndex), 2, int32(fieldIndex)))
}

func (g *FileGenerator) findEnumComments(sourceCodePath []int32, enumIndex int) string {
return findComments(g.sourceCodeInfo, append(sourceCodePath, int32(enumIndex)))
func (a *generatorFile) findEnumComments(
sourceCodePath []int32,
enumIndex int,
) string {
return a.findComments(append(sourceCodePath, int32(enumIndex)))
}

func (g *FileGenerator) findEnumValueComments(sourceCodePath []int32, enumIndex, valueIndex int) string {
return findComments(g.sourceCodeInfo, append(sourceCodePath, int32(enumIndex), 2, int32(valueIndex)))
func (a *generatorFile) findEnumValueComments(
sourceCodePath []int32,
enumIndex int,
valueIndex int,
) string {
// TODO rathil move 2 to const
return a.findComments(append(sourceCodePath, int32(enumIndex), 2, int32(valueIndex)))
}

func findComments(info *descriptorpb.SourceCodeInfo, ps []int32) string {
for _, loc := range info.GetLocation() {
func (a *generatorFile) findComments(ps []int32) string {
for _, loc := range a.sourceCodeInfo.GetLocation() {
if slices.Equal(loc.GetPath(), ps) {
return strings.TrimSuffix(loc.GetLeadingComments()+loc.GetTrailingComments(), "\n")
return strings.Trim(loc.GetLeadingComments()+loc.GetTrailingComments(), "\n")
}
}
return ""
Expand Down
Loading