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

protoc-gen-protobqを実装した #6

Merged
merged 1 commit into from
Dec 7, 2024
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
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.PHONY: install-tools
install-tools:
go install ./cmd/protoc-gen-protobq
go install github.com/bufbuild/buf/cmd/buf@v1.47.2
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

Expand All @@ -21,6 +22,6 @@ lint:
# buf breaking --against '.git#branch=$(BREAKING_CHANGE_BASE_BRANCH)'

.PHONY: codegen
codegen:
find . -type f \( -name '*.pb.go' \) -delete
codegen: install-tools
find . -type f \( -name '*.pb.go' -or -name '*.protobq.go' \) -delete
buf generate
3 changes: 3 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ plugins:
- remote: buf.build/protocolbuffers/go:v1.35.2
out: internal/protobuf
opt: paths=source_relative
- local: protoc-gen-protobq
out: internal/protobuf
opt: paths=source_relative
22 changes: 22 additions & 0 deletions cmd/protoc-gen-protobq/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/averak/protobq/internal"
"google.golang.org/protobuf/compiler/protogen"
)

func main() {
protogen.Options{}.Run(func(plugin *protogen.Plugin) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
}

g := internal.NewCodeGenerator(plugin, file)
if err := g.Gen(); err != nil {
return err
}
}
return nil
})
}
108 changes: 108 additions & 0 deletions internal/codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package internal

import (
"runtime/debug"

"github.com/averak/protobq/internal/protobuf/protobq"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)

//goland:noinspection GoSnakeCaseUsage
var (
timeIdents = struct {
Duration protogen.GoIdent
}{
Duration: protogen.GoImportPath("time").Ident("Duration"),
}
protoIdents = struct {
GetExtension protogen.GoIdent
}{
GetExtension: protogen.GoImportPath("google.golang.org/protobuf/proto").Ident("GetExtension"),
}
internalIdents = struct {
MaterializedView protogen.GoIdent
E_MaterializedView protogen.GoIdent
}{
MaterializedView: protogen.GoImportPath("github.com/averak/protobq/internal/protobuf/protobq").Ident("MaterializedView"),
E_MaterializedView: protogen.GoImportPath("github.com/averak/protobq/internal/protobuf/protobq").Ident("E_MaterializedView"),
}
protobqIdents = struct {
MaterializedView protogen.GoIdent
MaterializedViewOptions protogen.GoIdent
}{
MaterializedView: protogen.GoImportPath("github.com/averak/protobq").Ident("MaterializedView"),
MaterializedViewOptions: protogen.GoImportPath("github.com/averak/protobq").Ident("MaterializedViewOptions"),
}
)

type CodeGenerator struct {
plugin *protogen.Plugin
file *protogen.File
}

func NewCodeGenerator(plugin *protogen.Plugin, file *protogen.File) *CodeGenerator {
return &CodeGenerator{
plugin: plugin,
file: file,
}
}

func (g CodeGenerator) Gen() error {
if !g.shouldGenerate() {
return nil
}

filename := g.file.GeneratedFilenamePrefix + ".protobq.go"
gf := g.plugin.NewGeneratedFile(filename, g.file.GoImportPath)

{ // generate file header
info, _ := debug.ReadBuildInfo()
gf.P("// Code generated by ", info.Path, ". DO NOT EDIT.")
gf.P("// source: ", g.file.Desc.Path())
gf.P()
gf.P("package ", g.file.GoPackageName)
gf.P()
}
{ // generate materialized view schema
for _, msg := range g.file.Messages {
if !g.isMaterializedViewSchema(msg) {
continue
}

gf.P("var _ ", protobqIdents.MaterializedView, " = (*", msg.GoIdent.GoName, ")(nil)")
gf.P()
gf.P("func (mv *", msg.GoIdent.GoName, ") Options() ", protobqIdents.MaterializedViewOptions, " {")
gf.P(" ext, _ := ", protoIdents.GetExtension, "(mv.ProtoReflect().Descriptor().Options(), ", internalIdents.E_MaterializedView, ").(*", internalIdents.MaterializedView, ")")
gf.P(" return ", protobqIdents.MaterializedViewOptions, "{")
gf.P(" EnableRefresh: ext.GetEnableRefresh(),")
gf.P(" RefreshInterval: ", timeIdents.Duration, "(ext.GetRefreshIntervalMinutes()) * time.Minute,")
gf.P(" }")
gf.P("}")
gf.P()
}
}
return nil
}

func (g CodeGenerator) shouldGenerate() bool {
for _, msg := range g.file.Messages {
if g.isMaterializedViewSchema(msg) {
return true
}
}
return false
}

func (g CodeGenerator) isMaterializedViewSchema(msg *protogen.Message) bool {
opts := msg.Desc.Options()
if opts == nil {
return false
}

ext, ok := proto.GetExtension(opts, protobq.E_MaterializedView).(*protobq.MaterializedView)
if !ok {
return false
}
return ext.GetIsMaterializedView()
}
165 changes: 165 additions & 0 deletions internal/protobuf/example/example.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions internal/protobuf/example/example.protobq.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading