Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Make orm validation callback optional
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Apr 2, 2020
1 parent cfdc68f commit 5dc4fd1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
5 changes: 4 additions & 1 deletion incubator/group/testdata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/modules/incubator/group"
"github.com/cosmos/modules/incubator/orm"
)

func (m *MyAppProposal) GetBase() group.ProposalBase {
Expand Down Expand Up @@ -32,7 +33,9 @@ func (m *MyAppProposal) SetMsgs(new []sdk.Msg) error {
return nil
}

func (m *MyAppProposal) ValidateBasic() error {
var _ orm.Validateable = MyAppProposal{}

func (m MyAppProposal) ValidateBasic() error {
if err:=m.Base.ValidateBasic(); err!=nil{
return errors.Wrap(err, "base")
}
Expand Down
11 changes: 11 additions & 0 deletions incubator/group/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DecisionPolicyResult struct {
// DecisionPolicy is the persistent set of rules to determine the result of election on a proposal.
type DecisionPolicy interface {
orm.Persistent
orm.Validateable
Allow(tally Tally, totalPower sdk.Dec, votingDuration time.Duration) (DecisionPolicyResult, error)
}

Expand Down Expand Up @@ -126,6 +127,8 @@ func (s StdGroupAccountMetadata) NaturalKey() []byte {
return s.Base.NaturalKey()
}

var _ orm.Validateable = StdGroupAccountMetadata{}

func (s StdGroupAccountMetadata) ValidateBasic() error {
if err := s.Base.ValidateBasic(); err != nil {
return errors.Wrap(err, "base")
Expand All @@ -147,6 +150,8 @@ func (v Vote) NaturalKey() []byte {
return result
}

var _ orm.Validateable = Vote{}

func (v Vote) ValidateBasic() error {
if len(v.Voter) == 0 {
return errors.Wrap(ErrEmpty, "voter")
Expand Down Expand Up @@ -206,6 +211,8 @@ func noopValidator() subspace.ValueValidatorFn {
return func(value interface{}) error { return nil }
}

var _ orm.Validateable = GroupMetadata{}

func (m GroupMetadata) ValidateBasic() error {
if m.Group.Empty() {
return sdkerrors.Wrap(ErrEmpty, "group")
Expand All @@ -225,6 +232,8 @@ func (m GroupMetadata) ValidateBasic() error {
return nil
}

var _ orm.Validateable = GroupMember{}

func (m GroupMember) ValidateBasic() error {
if m.Group.Empty() {
return sdkerrors.Wrap(ErrEmpty, "group")
Expand All @@ -241,6 +250,8 @@ func (m GroupMember) ValidateBasic() error {
return nil
}

var _ orm.Validateable = ProposalBase{}

func (p ProposalBase) ValidateBasic() error {
if p.GroupAccount.Empty() {
return sdkerrors.Wrap(ErrEmpty, "group account")
Expand Down
8 changes: 6 additions & 2 deletions incubator/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (r RowID) Bytes() []byte {
return r
}

// Validateable is an interface that Persistent types can implement and is called on any orm save or update operation.
type Validateable interface {
// ValidateBasic is a sanity check on the data. Any error returned prevents create or updates.
ValidateBasic() error
}

// Persistent supports Marshal and Unmarshal
//
// This is separated from Marshal, as this almost always requires
Expand All @@ -46,8 +52,6 @@ func (r RowID) Bytes() []byte {
// As with Marshaller, this may do internal validation on the data
// and errors should be expected.
type Persistent interface {
// ValidateBasic is a sanity check on the data. Any error returned prevents create or updates.
ValidateBasic() error
// Marshal serializes object into binary representation
Marshal() ([]byte, error)
// Unmarshal deserializes the object from the binary representation
Expand Down
13 changes: 11 additions & 2 deletions incubator/orm/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (a Table) Create(ctx HasKVStore, rowID RowID, obj Persistent) error {
if err := assertCorrectType(a.model, obj); err != nil {
return err
}
if err := obj.ValidateBasic(); err != nil {
if err := assertValid(obj); err != nil {
return err
}
store := prefix.NewStore(ctx.KVStore(a.storeKey), []byte{a.prefix})
Expand All @@ -128,7 +128,7 @@ func (a Table) Save(ctx HasKVStore, rowID RowID, newValue Persistent) error {
if err := assertCorrectType(a.model, newValue); err != nil {
return err
}
if err := newValue.ValidateBasic(); err != nil {
if err := assertValid(newValue); err != nil {
return err
}

Expand All @@ -152,6 +152,15 @@ func (a Table) Save(ctx HasKVStore, rowID RowID, newValue Persistent) error {
return nil
}

func assertValid(obj Persistent) error {
if v, ok := obj.(Validateable); ok {
if err := v.ValidateBasic(); err != nil {
return err
}
}
return nil
}

// Delete removes the object under the rowID key. It expects the key to exists already
// and fails with a `ErrNotFound` otherwise. Any caller must therefore make sure that this contract
// is fulfilled.
Expand Down

0 comments on commit 5dc4fd1

Please sign in to comment.