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)!: implement gRPC query support #14965

Closed
wants to merge 15 commits into from
3 changes: 2 additions & 1 deletion orm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ require (
cosmossdk.io/errors v1.0.0-beta.7
github.com/cosmos/cosmos-db v0.0.0-20221226095112-f3c38ecb5e32
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/gertd/go-pluralize v0.2.1
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/iancoleman/strcase v0.2.0
github.com/regen-network/gocuke v0.6.2
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20230131160201-f062dba9d201
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
gotest.tools/v3 v3.4.0
Expand Down Expand Up @@ -57,7 +59,6 @@ require (
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions orm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv1M62hOWzwo5OXotisrKc=
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
github.com/getsentry/sentry-go v0.12.0/go.mod h1:NSap0JBYWzHND8oMbyi0+XZhUalc1TBdRL1M71JZW2c=
github.com/getsentry/sentry-go v0.17.0 h1:UustVWnOoDFHBS7IJUB2QK/nB5pap748ZEp0swnQJak=
github.com/getsentry/sentry-go v0.17.0/go.mod h1:B82dxtBvxG0KaPD8/hfSV+VcHD+Lg/xUS4JuQn1P4cM=
Expand Down
4 changes: 3 additions & 1 deletion orm/internal/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ managed:
enabled: true
go_package_prefix:
default: github.com/cosmos/cosmos-sdk/orm/internal
except:
- buf.build/googleapis/googleapis
override:
buf.build/cosmos/cosmos-sdk: cosmossdk.io/api
plugins:
Expand All @@ -14,4 +16,4 @@ plugins:
opt: paths=source_relative
- name: go-cosmos-orm
out: .
opt: paths=source_relative
opt: paths=source_relative
11 changes: 11 additions & 0 deletions orm/internal/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: cosmos
repository: cosmos-proto
commit: 1935555c206d4afb9e94615dfd0fad31
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 75b4300737fb4efca0831636be94e517
3 changes: 3 additions & 0 deletions orm/internal/buf.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
version: v1
deps:
- buf.build/cosmos/cosmos-proto
- buf.build/googleapis/googleapis
lint:
use:
- DEFAULT
Expand Down
13 changes: 11 additions & 2 deletions orm/internal/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
ormListPkg = protogen.GoImportPath("github.com/cosmos/cosmos-sdk/orm/model/ormlist")
ormErrPkg = protogen.GoImportPath("github.com/cosmos/cosmos-sdk/orm/types/ormerrors")
ormTablePkg = protogen.GoImportPath("github.com/cosmos/cosmos-sdk/orm/model/ormtable")
grpcPkg = protogen.GoImportPath("google.golang.org/grpc")
)

func ORMPluginRunner(p *protogen.Plugin) error {
Expand All @@ -30,12 +31,16 @@ func ORMPluginRunner(p *protogen.Plugin) error {
continue
}

// check if query proto file exists
_, hasQueryProto := p.FilesByPath[queryProtoFilename(f)]

gen := p.NewGeneratedFile(fmt.Sprintf("%s.cosmos_orm.go", f.GeneratedFilenamePrefix), f.GoImportPath)
cgen := &generator.GeneratedFile{
GeneratedFile: gen,
LocalPackages: map[string]bool{},
}
fgen := fileGen{GeneratedFile: cgen, file: f}

fgen := fileGen{GeneratedFile: cgen, file: f, genQueryServer: hasQueryProto}
err := fgen.gen()
if err != nil {
return err
Expand All @@ -56,7 +61,7 @@ func QueryProtoPluginRunner(p *protogen.Plugin) error {
continue
}

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

Check failure

Code scanning / gosec

Expect file permissions to be 0600 or less

Expect file permissions to be 0600 or less
if err != nil {
return err
}
Expand Down Expand Up @@ -89,3 +94,7 @@ func hasTables(file *protogen.File) bool {

return false
}

func queryProtoFilename(f *protogen.File) string {
return fmt.Sprintf("%s_query.proto", f.GeneratedFilenamePrefix)
}
54 changes: 43 additions & 11 deletions orm/internal/codegen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
"google.golang.org/protobuf/proto"

ormv1 "cosmossdk.io/api/cosmos/orm/v1"

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

type fileGen struct {
*generator.GeneratedFile
file *protogen.File
file *protogen.File
genQueryServer bool
}

func (f fileGen) gen() error {
Expand Down Expand Up @@ -50,6 +53,7 @@ func (f fileGen) gen() error {
f.genStoreMethods(stores)
f.genStoreInterfaceGuard()
f.genStoreConstructor(stores)

return nil
}

Expand All @@ -60,6 +64,10 @@ func (f fileGen) genStoreInterface(stores []*protogen.Message) {
f.P(name, "()", name)
}
f.P()
if f.genQueryServer {
f.P("RegisterQueryServer(", grpcPkg.Ident("ServiceRegistrar"), ")")
}
f.P()
f.P("doNotImplement()")
f.P("}")
f.P()
Expand Down Expand Up @@ -87,11 +95,10 @@ func (f fileGen) storeStructName() string {
}

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

func fileShortName(file *protogen.File) string {
filename := file.Proto.GetName()
func fileShortName(filename string) string {
shortName := filepath.Base(filename)
i := strings.Index(shortName, ".")
if i > 0 {
Expand Down Expand Up @@ -126,14 +133,29 @@ func (f fileGen) messageConstructorName(m *protogen.Message) string {

func (f fileGen) genStoreMethods(stores []*protogen.Message) {
// getters
storeStructName := f.storeStructName()
for _, msg := range stores {
name := f.messageTableInterfaceName(msg)
f.P("func(x ", f.storeStructName(), ") ", name, "() ", name, "{")
f.P("func(x ", storeStructName, ") ", name, "() ", name, "{")
f.P("return x.", f.param(msg.GoIdent.GoName))
f.P("}")
f.P()
}
f.P("func(", f.storeStructName(), ") doNotImplement() {}")

if f.genQueryServer {
f.P("type ", storeStructName, "QueryServer struct {")
f.P("Unimplemented", strcase.ToCamel(f.fileShortName()), "QueryServiceServer")
f.P(storeStructName)
f.P("}")

f.P("func(x ", storeStructName, ") RegisterQueryServer(registrar ", grpcPkg.Ident("ServiceRegistrar"), ") {")
f.P("Register", queryServiceName(f.file), "ServiceServer(registrar, ", storeStructName, "QueryServer{",
storeStructName, ":x})")
f.P("}")
f.P()
}

f.P("func(", storeStructName, ") doNotImplement() {}")
f.P()
}

Expand All @@ -153,26 +175,36 @@ func (f fileGen) genStoreConstructor(stores []*protogen.Message) {

f.P("return ", f.storeStructName(), "{")
for _, store := range stores {
f.P(f.messageTableReceiverName(store), ",")

f.P(f.param(store.GoIdent.GoName), ":", f.messageTableReceiverName(store), ",")
}
f.P("}, nil")
f.P("}")
}

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

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

func fieldsToKebabCase(fields string) string {
splitFields := fieldnames.CommaSeparatedFieldNames(fields).Names()
fieldStrs := make([]string, len(splitFields))
for i, field := range splitFields {
fieldStrs[i] = string(field)
}
return strings.Join(fieldStrs, "-")
}
7 changes: 0 additions & 7 deletions orm/internal/codegen/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ func (t tableGen) indexKeyInterfaceName() string {
return t.msg.GoIdent.GoName + "IndexKey"
}

func (t tableGen) genIndexKey(idxKeyName string) {
t.P("type ", idxKeyName, " struct {")
t.P("vs []interface{}")
t.P("}")
t.P()
}

func (t tableGen) indexKeyParts(names []protoreflect.Name) string {
cnames := make([]string, len(names))
for i, name := range names {
Expand Down
Loading