-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Address first batch of changes from orm audit (#490)
Address first batch of updates from ORM internal API audit #490: Change Save to Update Refactor TableExportable interface to not expose private table structure Add Set function Change Create to check for non-existence Rename AfterSaveInterceptor to AfterSetInterceptor for consistency Closes #490
- Loading branch information
Showing
17 changed files
with
255 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,15 @@ | ||
package orm | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
"github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
// TableExportable | ||
type TableExportable interface { | ||
// Table returns the table to export | ||
Table() table | ||
} | ||
|
||
// SequenceExportable | ||
type SequenceExportable interface { | ||
// Sequence returns the sequence to export | ||
Sequence() Sequence | ||
} | ||
|
||
// ExportTableData iterates over the given table entries and stores them at the passed ModelSlicePtr. | ||
// When the given table implements the `SequenceExportable` interface then it's current value | ||
// is returned as well or otherwise defaults to 0. | ||
func ExportTableData(ctx HasKVStore, t TableExportable, dest ModelSlicePtr) (uint64, error) { | ||
it, err := t.Table().PrefixScan(ctx, nil, nil) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "table PrefixScan failure when exporting table data") | ||
} | ||
_, err = ReadAll(it, dest) | ||
if err != nil { | ||
return 0, err | ||
} | ||
var seqValue uint64 | ||
if st, ok := t.(SequenceExportable); ok { | ||
seqValue = st.Sequence().CurVal(ctx) | ||
} | ||
return seqValue, err | ||
} | ||
|
||
// ImportTableData initializes a table and attaches indexers from the given data interface{}. | ||
// data should be a slice of structs that implement PrimaryKeyed (eg []*GroupInfo). | ||
// The seqValue is optional and only used with tables that implement the `SequenceExportable` interface. | ||
func ImportTableData(ctx HasKVStore, t TableExportable, data interface{}, seqValue uint64) error { | ||
table := t.Table() | ||
if err := clearAllInTable(ctx, table); err != nil { | ||
return errors.Wrap(err, "clear old entries") | ||
} | ||
|
||
if st, ok := t.(SequenceExportable); ok { | ||
if err := st.Sequence().InitVal(ctx, seqValue); err != nil { | ||
return errors.Wrap(err, "sequence") | ||
} | ||
} | ||
|
||
// Provided data must be a slice | ||
modelSlice := reflect.ValueOf(data) | ||
if modelSlice.Kind() != reflect.Slice { | ||
return errors.Wrap(ErrArgument, "data must be a slice") | ||
} | ||
|
||
// Create table entries | ||
for i := 0; i < modelSlice.Len(); i++ { | ||
obj, ok := modelSlice.Index(i).Interface().(PrimaryKeyed) | ||
if !ok { | ||
return errors.Wrapf(ErrArgument, "unsupported type :%s", reflect.TypeOf(data).Elem().Elem()) | ||
} | ||
err := table.Create(ctx, PrimaryKey(obj), obj) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// clearAllInTable deletes all entries in a table with delete interceptors called | ||
func clearAllInTable(ctx HasKVStore, table table) error { | ||
store := prefix.NewStore(ctx.KVStore(table.storeKey), []byte{table.prefix}) | ||
it := store.Iterator(nil, nil) | ||
defer it.Close() | ||
for ; it.Valid(); it.Next() { | ||
if err := table.Delete(ctx, it.Key()); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
// Export stores all the values in the table in the passed | ||
// ModelSlicePtr. If the table has an associated sequence, then its | ||
// current value is returned, otherwise 0 is returned by default. | ||
Export(HasKVStore, ModelSlicePtr) (uint64, error) | ||
|
||
// Import clears the table and initializes it from the given data | ||
// interface{}. data should be a slice of structs that implement | ||
// PrimaryKeyed (eg []*GroupInfo). The seqValue is optional and only | ||
// used with tables that have an associated sequence. | ||
Import(HasKVStore, interface{}, uint64) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.