forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Table-Store (aka ORM) package - Genesis (cosmos#10481)
<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description closes: cosmos#9237 ref: cosmos#9156 This PR is a follow-up of cosmos#10451, cosmos#10415 and cosmos#9751 and the last PR for adding the ORM package to `x/group`. It adds import/export genesis features. --- ### 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... - [x] 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 - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] 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) - [x] 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` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] 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
Showing
9 changed files
with
361 additions
and
50 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package orm | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// TableExportable | ||
type TableExportable interface { | ||
// 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(store sdk.KVStore, dest 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. The seqValue is optional and only | ||
// used with tables that have an associated sequence. | ||
Import(store sdk.KVStore, data interface{}, seqValue 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package orm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/testutil/testdata" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestImportExportTableData(t *testing.T) { | ||
interfaceRegistry := types.NewInterfaceRegistry() | ||
cdc := codec.NewProtoCodec(interfaceRegistry) | ||
|
||
table, err := NewAutoUInt64Table(AutoUInt64TablePrefix, AutoUInt64TableSeqPrefix, &testdata.TableModel{}, cdc) | ||
require.NoError(t, err) | ||
|
||
ctx := NewMockContext() | ||
store := ctx.KVStore(sdk.NewKVStoreKey("test")) | ||
|
||
tms := []*testdata.TableModel{ | ||
{ | ||
Id: 1, | ||
Name: "my test 1", | ||
Number: 123, | ||
Metadata: []byte("metadata 1"), | ||
}, | ||
{ | ||
Id: 2, | ||
Name: "my test 2", | ||
Number: 456, | ||
Metadata: []byte("metadata 2"), | ||
}, | ||
} | ||
|
||
err = table.Import(store, tms, 2) | ||
require.NoError(t, err) | ||
|
||
for _, g := range tms { | ||
var loaded testdata.TableModel | ||
_, err := table.GetOne(store, g.Id, &loaded) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, g, &loaded) | ||
} | ||
|
||
var exported []*testdata.TableModel | ||
seq, err := table.Export(store, &exported) | ||
require.NoError(t, err) | ||
require.Equal(t, seq, uint64(2)) | ||
|
||
for i, g := range exported { | ||
require.Equal(t, g, tms[i]) | ||
} | ||
} |
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.