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)!: return ormerrors.NotFound for Get methods in codegen #11113

Merged
merged 6 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions orm/internal/codegen/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (t tableGen) genStoreInterface() {
t.P("Save(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") error")
t.P("Delete(ctx ", contextPkg.Ident("Context"), ", ", t.param(t.msg.GoIdent.GoName), " *", t.QualifiedGoIdent(t.msg.GoIdent), ") error")
t.P("Has(ctx ", contextPkg.Ident("Context"), ", ", t.fieldsArgs(t.primaryKeyFields.Names()), ") (found bool, err error)")
t.P("// Get returns nil, ormerrors.NotFound if the record wasn't found.")
t.P("Get(ctx ", contextPkg.Ident("Context"), ", ", t.fieldsArgs(t.primaryKeyFields.Names()), ") (*", t.QualifiedGoIdent(t.msg.GoIdent), ", error)")

for _, idx := range t.uniqueIndexes {
Expand All @@ -80,7 +81,7 @@ func (t tableGen) genStoreInterface() {
}

// returns the has and get (in that order) function signature for unique indexes.
func (t tableGen) uniqueIndexSig(idxFields string) (string, string) {
func (t tableGen) uniqueIndexSig(idxFields string) (string, string, string) {
fieldsSlc := strings.Split(idxFields, ",")
camelFields := t.fieldsToCamelCase(idxFields)

Expand All @@ -90,12 +91,13 @@ func (t tableGen) uniqueIndexSig(idxFields string) (string, string) {

hasFuncSig := fmt.Sprintf("%s (ctx context.Context, %s) (found bool, err error)", hasFuncName, args)
getFuncSig := fmt.Sprintf("%s (ctx context.Context, %s) (*%s, error)", getFuncName, args, t.msg.GoIdent.GoName)
return hasFuncSig, getFuncSig
return hasFuncSig, getFuncSig, getFuncName
}

func (t tableGen) genUniqueIndexSig(idx *ormv1alpha1.SecondaryIndexDescriptor) {
hasSig, getSig := t.uniqueIndexSig(idx.Fields)
hasSig, getSig, getFuncName := t.uniqueIndexSig(idx.Fields)
t.P(hasSig)
t.P("// ", getFuncName, " returns nil, ormerrors.NotFound if the record wasn't found.")
t.P(getSig)
}

Expand Down Expand Up @@ -191,16 +193,19 @@ func (t tableGen) genStoreImpl() {
t.P(receiver, "Get(ctx ", contextPkg.Ident("Context"), ", ", t.fieldsArgs(t.primaryKeyFields.Names()), ") (*", varTypeName, ", error) {")
t.P("var ", varName, " ", varTypeName)
t.P("found, err := ", receiverVar, ".table.PrimaryKey().Get(ctx, &", varName, ", ", t.primaryKeyFields.String(), ")")
t.P("if !found {")
t.P("if err != nil {")
t.P("return nil, err")
t.P("}")
t.P("return &", varName, ", err")
t.P("if !found {")
t.P("return nil, ", ormErrPkg.Ident("NotFound"))
t.P("}")
t.P("return &", varName, ", nil")
t.P("}")
t.P()

for _, idx := range t.uniqueIndexes {
fields := strings.Split(idx.Fields, ",")
hasName, getName := t.uniqueIndexSig(idx.Fields)
hasName, getName, _ := t.uniqueIndexSig(idx.Fields)

// has
t.P("func (", receiverVar, " ", t.messageStoreReceiverName(t.msg), ") ", hasName, "{")
Expand All @@ -224,9 +229,12 @@ func (t tableGen) genStoreImpl() {
t.P(field, ",")
}
t.P(")")
t.P("if !found {")
t.P("if err != nil {")
t.P("return nil, err")
t.P("}")
t.P("if !found {")
t.P("return nil, ", ormErrPkg.Ident("NotFound"))
t.P("}")
t.P("return &", varName, ", nil")
t.P("}")
t.P()
Expand Down
16 changes: 12 additions & 4 deletions orm/internal/testpb/bank.cosmos_orm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions orm/internal/testpb/test_schema.cosmos_orm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions orm/model/ormdb/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"

"github.com/cosmos/cosmos-sdk/errors"

"google.golang.org/protobuf/reflect/protoreflect"
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"
Expand Down Expand Up @@ -41,7 +45,7 @@ func (k keeper) Send(ctx context.Context, from, to, denom string, amount uint64)

func (k keeper) Mint(ctx context.Context, acct, denom string, amount uint64) error {
supply, err := k.store.SupplyStore().Get(ctx, denom)
if err != nil {
if err != nil && !errors.IsOf(err, ormerrors.NotFound) {
return err
}

Expand All @@ -66,10 +70,6 @@ func (k keeper) Burn(ctx context.Context, acct, denom string, amount uint64) err
return err
}

if supply == nil {
return fmt.Errorf("no supply for %s", denom)
}

if amount > supply.Amount {
return fmt.Errorf("insufficient supply")
}
Expand All @@ -90,7 +90,11 @@ func (k keeper) Burn(ctx context.Context, acct, denom string, amount uint64) err

func (k keeper) Balance(ctx context.Context, acct, denom string) (uint64, error) {
balance, err := k.store.BalanceStore().Get(ctx, acct, denom)
if balance == nil {
if err != nil {
if errors.IsOf(err, ormerrors.NotFound) {
return 0, nil
}

return 0, err
}
return balance.Amount, err
Expand All @@ -99,14 +103,18 @@ func (k keeper) Balance(ctx context.Context, acct, denom string) (uint64, error)
func (k keeper) Supply(ctx context.Context, denom string) (uint64, error) {
supply, err := k.store.SupplyStore().Get(ctx, denom)
if supply == nil {
if errors.IsOf(err, ormerrors.NotFound) {
return 0, nil
}

return 0, err
}
return supply.Amount, err
}

func (k keeper) addBalance(ctx context.Context, acct, denom string, amount uint64) error {
balance, err := k.store.BalanceStore().Get(ctx, acct, denom)
if err != nil {
if err != nil && !errors.IsOf(err, ormerrors.NotFound) {
return err
}

Expand All @@ -130,10 +138,6 @@ func (k keeper) safeSubBalance(ctx context.Context, acct, denom string, amount u
return err
}

if balance == nil {
return fmt.Errorf("acct %x has no balance for %s", acct, denom)
}

if amount > balance.Amount {
return fmt.Errorf("insufficient funds")
}
Expand Down
1 change: 1 addition & 0 deletions orm/types/ormerrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ var (
InvalidTableDefinition = errors.New(codespace, 25, "invalid table definition")
InvalidFileDescriptorID = errors.New(codespace, 26, "invalid file descriptor ID")
TableNotFound = errors.New(codespace, 27, "table not found")
NotFound = errors.New(codespace, 28, "not found")
)