Skip to content

Commit

Permalink
feat(orm): add query proto codegen (cosmos#13438)
Browse files Browse the repository at this point in the history
## Description

This starts the implementation of approach (C) in cosmos#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 cosmos#11774 and instead provides direct index access in the most simple and complete way that we can easily implement now. More advanced things in cosmos#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)
  • Loading branch information
aaronc authored and Wryhder committed Oct 26, 2022
1 parent ef8f43f commit fbff3c5
Show file tree
Hide file tree
Showing 21 changed files with 8,711 additions and 6,908 deletions.
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)
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)
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)
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, "_")
}

0 comments on commit fbff3c5

Please sign in to comment.