cosmos-sdk/x/group/internal/orm/example_test.go
Marie Gauthier c5b879a03d
feat: Add Table-Store (aka ORM) package - Index and Iterator (#10451)
<!--
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

ref: #9237, #9156

This PR is a follow-up of #10415 and #9751.
It adds multi-key secondary indexes, iterator and pagination support.

There will be one last follow-up PR for adding 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
- [ ] reviewed "Files changed" and left comments if necessary
- [x] 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)
2021-11-09 18:01:27 +00:00

67 lines
2.3 KiB
Go

package orm
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
type TestKeeper struct {
autoUInt64Table *AutoUInt64Table
primaryKeyTable *PrimaryKeyTable
autoUInt64TableModelByMetadataIndex Index
primaryKeyTableModelByNameIndex Index
primaryKeyTableModelByNumberIndex Index
primaryKeyTableModelByMetadataIndex Index
}
var (
AutoUInt64TablePrefix [2]byte = [2]byte{0x0}
PrimaryKeyTablePrefix [2]byte = [2]byte{0x1}
AutoUInt64TableSeqPrefix byte = 0x2
AutoUInt64TableModelByMetadataPrefix byte = 0x4
PrimaryKeyTableModelByNamePrefix byte = 0x5
PrimaryKeyTableModelByNumberPrefix byte = 0x6
PrimaryKeyTableModelByMetadataPrefix byte = 0x7
)
func NewTestKeeper(cdc codec.Codec) TestKeeper {
k := TestKeeper{}
var err error
k.autoUInt64Table, err = NewAutoUInt64Table(AutoUInt64TablePrefix, AutoUInt64TableSeqPrefix, &testdata.TableModel{}, cdc)
if err != nil {
panic(err.Error())
}
k.autoUInt64TableModelByMetadataIndex, err = NewIndex(k.autoUInt64Table, AutoUInt64TableModelByMetadataPrefix, func(val interface{}) ([]interface{}, error) {
return []interface{}{val.(*testdata.TableModel).Metadata}, nil
}, testdata.TableModel{}.Metadata)
if err != nil {
panic(err.Error())
}
k.primaryKeyTable, err = NewPrimaryKeyTable(PrimaryKeyTablePrefix, &testdata.TableModel{}, cdc)
if err != nil {
panic(err.Error())
}
k.primaryKeyTableModelByNameIndex, err = NewIndex(k.primaryKeyTable, PrimaryKeyTableModelByNamePrefix, func(val interface{}) ([]interface{}, error) {
return []interface{}{val.(*testdata.TableModel).Name}, nil
}, testdata.TableModel{}.Name)
if err != nil {
panic(err.Error())
}
k.primaryKeyTableModelByNumberIndex, err = NewIndex(k.primaryKeyTable, PrimaryKeyTableModelByNumberPrefix, func(val interface{}) ([]interface{}, error) {
return []interface{}{val.(*testdata.TableModel).Number}, nil
}, testdata.TableModel{}.Number)
if err != nil {
panic(err.Error())
}
k.primaryKeyTableModelByMetadataIndex, err = NewIndex(k.primaryKeyTable, PrimaryKeyTableModelByMetadataPrefix, func(val interface{}) ([]interface{}, error) {
return []interface{}{val.(*testdata.TableModel).Metadata}, nil
}, testdata.TableModel{}.Metadata)
if err != nil {
panic(err.Error())
}
return k
}