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(orm): add query proto codegen #13438

Merged
merged 11 commits into from
Oct 7, 2022
4 changes: 4 additions & 0 deletions orm/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
codegen:
go install ./cmd/protoc-gen-go-cosmos-orm
go install ./cmd/protoc-gen-go-cosmos-orm-proto
# generate .proto files first
(cd internal; buf generate --template buf.proto.gen.yaml)
# generate go code
(cd internal; buf generate)
11 changes: 11 additions & 0 deletions orm/cmd/protoc-gen-go-cosmos-orm-proto/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"google.golang.org/protobuf/compiler/protogen"

"github.com/cosmos/cosmos-sdk/orm/internal/codegen"
)

func main() {
protogen.Options{}.Run(codegen.QueryProtoPluginRunner)
}
2 changes: 1 addition & 1 deletion orm/cmd/protoc-gen-go-cosmos-orm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func main() {
protogen.Options{}.Run(codegen.PluginRunner)
protogen.Options{}.Run(codegen.ORMPluginRunner)
}
5 changes: 4 additions & 1 deletion orm/internal/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ managed:
override:
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
plugins:
- name: go-pulsar
- name: go
out: .
opt: paths=source_relative
- name: go-grpc
out: .
opt: paths=source_relative
- name: go-cosmos-orm
Expand Down
11 changes: 11 additions & 0 deletions orm/internal/buf.proto.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v1
managed:
enabled: true
go_package_prefix:
default: github.com/cosmos/cosmos-sdk/orm/internal
override:
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
plugins:
- name: go-cosmos-orm-proto
out: .
opt: paths=source_relative
39 changes: 36 additions & 3 deletions orm/internal/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package codegen

import (
"fmt"
"os"

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

ormv1 "cosmossdk.io/api/cosmos/orm/v1"
"github.com/cosmos/cosmos-proto/generator"
Expand All @@ -17,7 +19,8 @@ const (
ormTablePkg = protogen.GoImportPath("github.com/cosmos/cosmos-sdk/orm/model/ormtable")
)

func PluginRunner(p *protogen.Plugin) error {
func ORMPluginRunner(p *protogen.Plugin) error {
p.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm.. the value of the const is 1. gosec should be able to statically inspect const values...

for _, f := range p.Files {
if !f.Generate {
continue
Expand All @@ -32,12 +35,42 @@ func PluginRunner(p *protogen.Plugin) error {
GeneratedFile: gen,
LocalPackages: map[string]bool{},
}
f := fileGen{GeneratedFile: cgen, file: f}
err := f.gen()
fgen := fileGen{GeneratedFile: cgen, file: f}
err := fgen.gen()
if err != nil {
return err
}
}

return nil
}

func QueryProtoPluginRunner(p *protogen.Plugin) error {
p.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)

Check failure

Code scanning / gosec

Potential integer overflow by integer type conversion

Potential integer overflow by integer type conversion
for _, f := range p.Files {
if !f.Generate {
continue
}

if !hasTables(f) {
continue
}

out, err := os.OpenFile(fmt.Sprintf("%s_query.proto", f.GeneratedFilenamePrefix), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0644)

Check failure

Code scanning / gosec

Expect file permissions to be 0600 or less

Expect file permissions to be 0600 or less
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to say this seems rather strange... 0644 is pretty standard 🤔

if err != nil {
return err
}

err = queryProtoGen{
File: f,
svc: newWriter(),
msgs: newWriter(),
outFile: out,
imports: map[string]bool{},
}.gen()
if err != nil {
return err
}
}

return nil
Expand Down
17 changes: 15 additions & 2 deletions orm/internal/codegen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ func (f fileGen) storeStructName() string {
}

func (f fileGen) fileShortName() string {
filename := f.file.Proto.GetName()
return fileShortName(f.file)
}

func fileShortName(file *protogen.File) string {
filename := file.Proto.GetName()
shortName := filepath.Base(filename)
i := strings.Index(shortName, ".")
if i > 0 {
Expand Down Expand Up @@ -155,11 +159,20 @@ func (f fileGen) genStoreConstructor(stores []*protogen.Message) {
f.P("}")
}

func (f fileGen) fieldsToCamelCase(fields string) string {
func fieldsToCamelCase(fields string) string {
splitFields := strings.Split(fields, ",")
camelFields := make([]string, len(splitFields))
for i, field := range splitFields {
camelFields[i] = strcase.ToCamel(field)
}
return strings.Join(camelFields, "")
}

func fieldsToSnakeCase(fields string) string {
splitFields := strings.Split(fields, ",")
camelFields := make([]string, len(splitFields))
for i, field := range splitFields {
camelFields[i] = strcase.ToSnake(field)
}
return strings.Join(camelFields, "_")
}
Loading