Skip to content

Commit

Permalink
refactor(orm)!: rename table interfaces from Store -> Table in codegen (
Browse files Browse the repository at this point in the history
cosmos#11176)

## Description

I realize it's more intuitive to name the interfaces in codegen `FooTable` rather than `FooStore` for the type `Foo`.

Since we (and possibly others) are starting to build off of this, better to change now rather than later.

How does this change look?



---

### 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/master/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/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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 Feb 14, 2022
1 parent 875378b commit 0d4cb92
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 145 deletions.
24 changes: 12 additions & 12 deletions orm/internal/codegen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ func (f fileGen) gen() error {
stores = append(stores, msg)
}
}
f.genStoreInterfaces(stores)
f.genStoreInterface(stores)
f.genStoreStruct(stores)
f.genStoreMethods(stores)
f.genStoreInterfaceGuard()
f.genStoreConstructor(stores)
return nil
}

func (f fileGen) genStoreInterfaces(stores []*protogen.Message) {
func (f fileGen) genStoreInterface(stores []*protogen.Message) {
f.P("type ", f.storeInterfaceName(), " interface {")
for _, store := range stores {
name := f.messageStoreInterfaceName(store)
name := f.messageTableInterfaceName(store)
f.P(name, "()", name)
}
f.P()
Expand All @@ -69,7 +69,7 @@ func (f fileGen) genStoreStruct(stores []*protogen.Message) {
// struct
f.P("type ", f.storeStructName(), " struct {")
for _, message := range stores {
f.P(f.param(message.GoIdent.GoName), " ", f.messageStoreInterfaceName(message))
f.P(f.param(message.GoIdent.GoName), " ", f.messageTableInterfaceName(message))
}
f.P("}")
}
Expand All @@ -96,8 +96,8 @@ func (f fileGen) fileShortName() string {
return strcase.ToCamel(shortName)
}

func (f fileGen) messageStoreInterfaceName(m *protogen.Message) string {
return m.GoIdent.GoName + "Store"
func (f fileGen) messageTableInterfaceName(m *protogen.Message) string {
return m.GoIdent.GoName + "Table"
}

func (f fileGen) messageReaderInterfaceName(m *protogen.Message) string {
Expand All @@ -112,18 +112,18 @@ func (f fileGen) param(name string) string {
return strcase.ToLowerCamel(name)
}

func (f fileGen) messageStoreReceiverName(m *protogen.Message) string {
return f.param(f.messageStoreInterfaceName(m))
func (f fileGen) messageTableReceiverName(m *protogen.Message) string {
return f.param(f.messageTableInterfaceName(m))
}

func (f fileGen) messageConstructorName(m *protogen.Message) string {
return "New" + f.messageStoreInterfaceName(m)
return "New" + f.messageTableInterfaceName(m)
}

func (f fileGen) genStoreMethods(stores []*protogen.Message) {
// getters
for _, msg := range stores {
name := f.messageStoreInterfaceName(msg)
name := f.messageTableInterfaceName(msg)
f.P("func(x ", f.storeStructName(), ") ", name, "() ", name, "{")
f.P("return x.", f.param(msg.GoIdent.GoName))
f.P("}")
Expand All @@ -140,7 +140,7 @@ func (f fileGen) genStoreInterfaceGuard() {
func (f fileGen) genStoreConstructor(stores []*protogen.Message) {
f.P("func New", f.storeInterfaceName(), "(db ", ormTablePkg.Ident("Schema"), ") (", f.storeInterfaceName(), ", error) {")
for _, store := range stores {
f.P(f.messageStoreReceiverName(store), ", err := ", f.messageConstructorName(store), "(db)")
f.P(f.messageTableReceiverName(store), ", err := ", f.messageConstructorName(store), "(db)")
f.P("if err != nil {")
f.P("return nil, err")
f.P("}")
Expand All @@ -149,7 +149,7 @@ func (f fileGen) genStoreConstructor(stores []*protogen.Message) {

f.P("return ", f.storeStructName(), "{")
for _, store := range stores {
f.P(f.messageStoreReceiverName(store), ",")
f.P(f.messageTableReceiverName(store), ",")
}
f.P("}, nil")
f.P("}")
Expand Down
12 changes: 6 additions & 6 deletions orm/internal/codegen/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ func (s singletonGen) gen() {

func (s singletonGen) genInterface() {
s.P("// singleton store")
s.P("type ", s.messageStoreInterfaceName(s.msg), " interface {")
s.P("type ", s.messageTableInterfaceName(s.msg), " interface {")
s.P("Get(ctx ", contextPkg.Ident("Context"), ") (*", s.msg.GoIdent.GoName, ", error)")
s.P("Save(ctx ", contextPkg.Ident("Context"), ", ", s.param(s.msg.GoIdent.GoName), "*", s.msg.GoIdent.GoName, ") error")
s.P("}")
s.P()
}

func (s singletonGen) genStruct() {
s.P("type ", s.messageStoreReceiverName(s.msg), " struct {")
s.P("type ", s.messageTableReceiverName(s.msg), " struct {")
s.P("table ", ormTablePkg.Ident("Table"))
s.P("}")
s.P()
}

func (s singletonGen) genInterfaceGuard() {
s.P("var _ ", s.messageStoreInterfaceName(s.msg), " = ", s.messageStoreReceiverName(s.msg), "{}")
s.P("var _ ", s.messageTableInterfaceName(s.msg), " = ", s.messageTableReceiverName(s.msg), "{}")
}

func (s singletonGen) genMethods() {
receiver := fmt.Sprintf("func (x %s) ", s.messageStoreReceiverName(s.msg))
receiver := fmt.Sprintf("func (x %s) ", s.messageTableReceiverName(s.msg))
varName := s.param(s.msg.GoIdent.GoName)
// Get
s.P(receiver, "Get(ctx ", contextPkg.Ident("Context"), ") (*", s.msg.GoIdent.GoName, ", error) {")
Expand All @@ -74,12 +74,12 @@ func (s singletonGen) genMethods() {
}

func (s singletonGen) genConstructor() {
iface := s.messageStoreInterfaceName(s.msg)
iface := s.messageTableInterfaceName(s.msg)
s.P("func New", iface, "(db ", ormTablePkg.Ident("Schema"), ") (", iface, ", error) {")
s.P("table := db.GetTable(&", s.msg.GoIdent.GoName, "{})")
s.P("if table == nil {")
s.P("return nil, ", ormErrPkg.Ident("TableNotFound.Wrap"), "(string((&", s.msg.GoIdent.GoName, "{}).ProtoReflect().Descriptor().FullName()))")
s.P("}")
s.P("return &", s.messageStoreReceiverName(s.msg), "{table}, nil")
s.P("return &", s.messageTableReceiverName(s.msg), "{table}, nil")
s.P("}")
}
30 changes: 15 additions & 15 deletions orm/internal/codegen/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ func newTableGen(fileGen fileGen, msg *protogen.Message, table *ormv1alpha1.Tabl
}

func (t tableGen) gen() {
t.genStoreInterface()
t.getTableInterface()
t.genIterator()
t.genIndexKeys()
t.genStruct()
t.genStoreImpl()
t.genStoreImplGuard()
t.genTableImpl()
t.genTableImplGuard()
t.genConstructor()
}

func (t tableGen) genStoreInterface() {
t.P("type ", t.messageStoreInterfaceName(t.msg), " interface {")
func (t tableGen) getTableInterface() {
t.P("type ", t.messageTableInterfaceName(t.msg), " interface {")
t.P("Insert(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") error")
if t.table.PrimaryKey.AutoIncrement {
t.P("InsertReturningID(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") (uint64, error)")
Expand Down Expand Up @@ -153,7 +153,7 @@ func (t tableGen) fieldArg(name protoreflect.Name) string {
}

func (t tableGen) genStruct() {
t.P("type ", t.messageStoreReceiverName(t.msg), " struct {")
t.P("type ", t.messageTableReceiverName(t.msg), " struct {")
if t.table.PrimaryKey.AutoIncrement {
t.P("table ", ormTablePkg.Ident("AutoIncrementTable"))
} else {
Expand All @@ -163,9 +163,9 @@ func (t tableGen) genStruct() {
t.storeStructName()
}

func (t tableGen) genStoreImpl() {
func (t tableGen) genTableImpl() {
receiverVar := "this"
receiver := fmt.Sprintf("func (%s %s) ", receiverVar, t.messageStoreReceiverName(t.msg))
receiver := fmt.Sprintf("func (%s %s) ", receiverVar, t.messageTableReceiverName(t.msg))
varName := t.param(t.msg.GoIdent.GoName)
varTypeName := t.QualifiedGoIdent(t.msg.GoIdent)

Expand Down Expand Up @@ -210,7 +210,7 @@ func (t tableGen) genStoreImpl() {
hasName, getName, _ := t.uniqueIndexSig(idx.Fields)

// has
t.P("func (", receiverVar, " ", t.messageStoreReceiverName(t.msg), ") ", hasName, "{")
t.P("func (", receiverVar, " ", t.messageTableReceiverName(t.msg), ") ", hasName, "{")
t.P("return ", receiverVar, ".table.GetIndexByID(", idx.Id, ").(",
ormTablePkg.Ident("UniqueIndex"), ").Has(ctx,")
for _, field := range fields {
Expand All @@ -223,7 +223,7 @@ func (t tableGen) genStoreImpl() {
// get
varName := t.param(t.msg.GoIdent.GoName)
varTypeName := t.msg.GoIdent.GoName
t.P("func (", receiverVar, " ", t.messageStoreReceiverName(t.msg), ") ", getName, "{")
t.P("func (", receiverVar, " ", t.messageTableReceiverName(t.msg), ") ", getName, "{")
t.P("var ", varName, " ", varTypeName)
t.P("found, err := ", receiverVar, ".table.GetIndexByID(", idx.Id, ").(",
ormTablePkg.Ident("UniqueIndex"), ").Get(ctx, &", varName, ",")
Expand Down Expand Up @@ -274,24 +274,24 @@ func (t tableGen) genStoreImpl() {
t.P()
}

func (t tableGen) genStoreImplGuard() {
t.P("var _ ", t.messageStoreInterfaceName(t.msg), " = ", t.messageStoreReceiverName(t.msg), "{}")
func (t tableGen) genTableImplGuard() {
t.P("var _ ", t.messageTableInterfaceName(t.msg), " = ", t.messageTableReceiverName(t.msg), "{}")
}

func (t tableGen) genConstructor() {
iface := t.messageStoreInterfaceName(t.msg)
iface := t.messageTableInterfaceName(t.msg)
t.P("func New", iface, "(db ", ormTablePkg.Ident("Schema"), ") (", iface, ", error) {")
t.P("table := db.GetTable(&", t.msg.GoIdent.GoName, "{})")
t.P("if table == nil {")
t.P("return nil,", ormErrPkg.Ident("TableNotFound.Wrap"), "(string((&", t.msg.GoIdent.GoName, "{}).ProtoReflect().Descriptor().FullName()))")
t.P("}")
if t.table.PrimaryKey.AutoIncrement {
t.P(
"return ", t.messageStoreReceiverName(t.msg), "{table.(",
"return ", t.messageTableReceiverName(t.msg), "{table.(",
ormTablePkg.Ident("AutoIncrementTable"), ")}, nil",
)
} else {
t.P("return ", t.messageStoreReceiverName(t.msg), "{table}, nil")
t.P("return ", t.messageTableReceiverName(t.msg), "{table}, nil")
}
t.P("}")
}
Loading

0 comments on commit 0d4cb92

Please sign in to comment.