## Description
This PR adds a `ModuleDB` interface which can be used directly by Cosmos SDK modules. A simplified bank example with Mint/Send/Burn functionality against Balance and Supply tables is included in the tests.
This PR also:
* adds simplified `Get` and `Has` methods to `Table` which use the primary key values in the message instead of `...interface{}`
* adds a stable deterministic proto JSON marshaler and updates the `Entry.String` methods to use it because the golden tests are not deterministic without this. This code is currently internal but can be extracted to a public `codec` or `cosmos-proto` package eventually.
---
### 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...
- [ ] 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
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] 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)
- [ ] 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`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] 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)
140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
package ormkv
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/internal/stablejson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// Entry defines a logical representation of a kv-store entry for ORM instances.
|
|
type Entry interface {
|
|
fmt.Stringer
|
|
|
|
// GetTableName returns the table-name (equivalent to the fully-qualified
|
|
// proto message name) this entry corresponds to.
|
|
GetTableName() protoreflect.FullName
|
|
|
|
// to allow new methods to be added without breakage, this interface
|
|
// shouldn't be implemented outside this package,
|
|
// see https://go.dev/blog/module-compatibility
|
|
doNotImplement()
|
|
}
|
|
|
|
// PrimaryKeyEntry represents a logically decoded primary-key entry.
|
|
type PrimaryKeyEntry struct {
|
|
|
|
// TableName is the table this entry represents.
|
|
TableName protoreflect.FullName
|
|
|
|
// Key represents the primary key values.
|
|
Key []protoreflect.Value
|
|
|
|
// Value represents the message stored under the primary key.
|
|
Value proto.Message
|
|
}
|
|
|
|
func (p *PrimaryKeyEntry) GetTableName() protoreflect.FullName {
|
|
return p.TableName
|
|
}
|
|
|
|
func (p *PrimaryKeyEntry) String() string {
|
|
if p.Value == nil {
|
|
return fmt.Sprintf("PK %s %s -> _", p.TableName, fmtValues(p.Key))
|
|
} else {
|
|
valBz, err := stablejson.Marshal(p.Value)
|
|
valStr := string(valBz)
|
|
if err != nil {
|
|
valStr = fmt.Sprintf("ERR %v", err)
|
|
}
|
|
return fmt.Sprintf("PK %s %s -> %s", p.TableName, fmtValues(p.Key), valStr)
|
|
}
|
|
}
|
|
|
|
func fmtValues(values []protoreflect.Value) string {
|
|
if len(values) == 0 {
|
|
return "_"
|
|
}
|
|
|
|
parts := make([]string, len(values))
|
|
for i, v := range values {
|
|
parts[i] = fmt.Sprintf("%v", v.Interface())
|
|
}
|
|
|
|
return strings.Join(parts, "/")
|
|
}
|
|
|
|
func (p *PrimaryKeyEntry) doNotImplement() {}
|
|
|
|
// IndexKeyEntry represents a logically decoded index entry.
|
|
type IndexKeyEntry struct {
|
|
|
|
// TableName is the table this entry represents.
|
|
TableName protoreflect.FullName
|
|
|
|
// Fields are the index fields this entry represents.
|
|
Fields []protoreflect.Name
|
|
|
|
// IsUnique indicates whether this index is unique or not.
|
|
IsUnique bool
|
|
|
|
// IndexValues represent the index values.
|
|
IndexValues []protoreflect.Value
|
|
|
|
// PrimaryKey represents the primary key values, it is empty if this is a
|
|
// prefix key
|
|
PrimaryKey []protoreflect.Value
|
|
}
|
|
|
|
func (i *IndexKeyEntry) GetTableName() protoreflect.FullName {
|
|
return i.TableName
|
|
}
|
|
|
|
func (i *IndexKeyEntry) doNotImplement() {}
|
|
|
|
func (i *IndexKeyEntry) string() string {
|
|
return fmt.Sprintf("%s %s : %s -> %s", i.TableName, fmtFields(i.Fields), fmtValues(i.IndexValues), fmtValues(i.PrimaryKey))
|
|
}
|
|
|
|
func fmtFields(fields []protoreflect.Name) string {
|
|
strs := make([]string, len(fields))
|
|
for i, field := range fields {
|
|
strs[i] = string(field)
|
|
}
|
|
return strings.Join(strs, "/")
|
|
}
|
|
|
|
func (i *IndexKeyEntry) String() string {
|
|
if i.IsUnique {
|
|
return fmt.Sprintf("UNIQ %s", i.string())
|
|
} else {
|
|
|
|
return fmt.Sprintf("IDX %s", i.string())
|
|
}
|
|
}
|
|
|
|
// SeqEntry represents a sequence for tables with auto-incrementing primary keys.
|
|
type SeqEntry struct {
|
|
|
|
// TableName is the table this entry represents.
|
|
TableName protoreflect.FullName
|
|
|
|
// Value is the uint64 value stored for this sequence.
|
|
Value uint64
|
|
}
|
|
|
|
func (s *SeqEntry) GetTableName() protoreflect.FullName {
|
|
return s.TableName
|
|
}
|
|
|
|
func (s *SeqEntry) doNotImplement() {}
|
|
|
|
func (s *SeqEntry) String() string {
|
|
return fmt.Sprintf("SEQ %s %d", s.TableName, s.Value)
|
|
}
|
|
|
|
var _, _, _ Entry = &PrimaryKeyEntry{}, &IndexKeyEntry{}, &SeqEntry{}
|