Skip to content

Commit 2151427

Browse files
authored
feat(orm): add query proto codegen (#13438)
## Description This starts the implementation of approach (C) in #11774 by generating `_query.proto` files for `.proto` files which have ORM definitions. It does this using a new protoc plugin called `protoc-gen-go-cosmos-orm-proto`. Please review `bank_query.proto` and `test_schema_query.proto` as these are the outputs of the codegen. The approach taken does not attempt to do any sort of logical queries as discussed in #11774 and instead provides direct index access in the most simple and complete way that we can easily implement now. More advanced things in #11774 could be implemented later, but this should be possible to deliver relatively quickly and should allow developers to completely skip writing gRPC queries manually if they use ORM. Note that the implementation of these queries can provide merkle proofs because the mapping to state is well known. I did need to disable pulsar codegen in this PR because it doesn't support proto3 optionals which are needed here (depends on cosmos/cosmos-proto#12). Fortunately, pulsar is 100% compatible with the official google codegen and there is no problem running ORM tests against the official codegen. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
1 parent 5f01f6f commit 2151427

21 files changed

+8711
-6908
lines changed

orm/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
codegen:
22
go install ./cmd/protoc-gen-go-cosmos-orm
3+
go install ./cmd/protoc-gen-go-cosmos-orm-proto
4+
# generate .proto files first
5+
(cd internal; buf generate --template buf.proto.gen.yaml)
6+
# generate go code
37
(cd internal; buf generate)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"google.golang.org/protobuf/compiler/protogen"
5+
6+
"github.com/cosmos/cosmos-sdk/orm/internal/codegen"
7+
)
8+
9+
func main() {
10+
protogen.Options{}.Run(codegen.QueryProtoPluginRunner)
11+
}

orm/cmd/protoc-gen-go-cosmos-orm/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
)
88

99
func main() {
10-
protogen.Options{}.Run(codegen.PluginRunner)
10+
protogen.Options{}.Run(codegen.ORMPluginRunner)
1111
}

orm/internal/buf.gen.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ managed:
66
override:
77
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
88
plugins:
9-
- name: go-pulsar
9+
- name: go
10+
out: .
11+
opt: paths=source_relative
12+
- name: go-grpc
1013
out: .
1114
opt: paths=source_relative
1215
- name: go-cosmos-orm

orm/internal/buf.proto.gen.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: v1
2+
managed:
3+
enabled: true
4+
go_package_prefix:
5+
default: github.com/cosmos/cosmos-sdk/orm/internal
6+
override:
7+
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
8+
plugins:
9+
- name: go-cosmos-orm-proto
10+
out: .
11+
opt: paths=source_relative

orm/internal/codegen/codegen.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package codegen
22

33
import (
44
"fmt"
5+
"os"
56

67
"google.golang.org/protobuf/compiler/protogen"
78
"google.golang.org/protobuf/proto"
9+
"google.golang.org/protobuf/types/pluginpb"
810

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

20-
func PluginRunner(p *protogen.Plugin) error {
22+
func ORMPluginRunner(p *protogen.Plugin) error {
23+
p.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
2124
for _, f := range p.Files {
2225
if !f.Generate {
2326
continue
@@ -32,12 +35,42 @@ func PluginRunner(p *protogen.Plugin) error {
3235
GeneratedFile: gen,
3336
LocalPackages: map[string]bool{},
3437
}
35-
f := fileGen{GeneratedFile: cgen, file: f}
36-
err := f.gen()
38+
fgen := fileGen{GeneratedFile: cgen, file: f}
39+
err := fgen.gen()
40+
if err != nil {
41+
return err
42+
}
43+
}
44+
45+
return nil
46+
}
47+
48+
func QueryProtoPluginRunner(p *protogen.Plugin) error {
49+
p.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
50+
for _, f := range p.Files {
51+
if !f.Generate {
52+
continue
53+
}
54+
55+
if !hasTables(f) {
56+
continue
57+
}
58+
59+
out, err := os.OpenFile(fmt.Sprintf("%s_query.proto", f.GeneratedFilenamePrefix), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0644)
3760
if err != nil {
3861
return err
3962
}
4063

64+
err = queryProtoGen{
65+
File: f,
66+
svc: newWriter(),
67+
msgs: newWriter(),
68+
outFile: out,
69+
imports: map[string]bool{},
70+
}.gen()
71+
if err != nil {
72+
return err
73+
}
4174
}
4275

4376
return nil

orm/internal/codegen/file.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ func (f fileGen) storeStructName() string {
8787
}
8888

8989
func (f fileGen) fileShortName() string {
90-
filename := f.file.Proto.GetName()
90+
return fileShortName(f.file)
91+
}
92+
93+
func fileShortName(file *protogen.File) string {
94+
filename := file.Proto.GetName()
9195
shortName := filepath.Base(filename)
9296
i := strings.Index(shortName, ".")
9397
if i > 0 {
@@ -155,11 +159,20 @@ func (f fileGen) genStoreConstructor(stores []*protogen.Message) {
155159
f.P("}")
156160
}
157161

158-
func (f fileGen) fieldsToCamelCase(fields string) string {
162+
func fieldsToCamelCase(fields string) string {
159163
splitFields := strings.Split(fields, ",")
160164
camelFields := make([]string, len(splitFields))
161165
for i, field := range splitFields {
162166
camelFields[i] = strcase.ToCamel(field)
163167
}
164168
return strings.Join(camelFields, "")
165169
}
170+
171+
func fieldsToSnakeCase(fields string) string {
172+
splitFields := strings.Split(fields, ",")
173+
camelFields := make([]string, len(splitFields))
174+
for i, field := range splitFields {
175+
camelFields[i] = strcase.ToSnake(field)
176+
}
177+
return strings.Join(camelFields, "_")
178+
}

0 commit comments

Comments
 (0)