Skip to content

Commit f906c8a

Browse files
committed
Add docs
1 parent 703294c commit f906c8a

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

storeutils/entity_id.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,36 @@ var ZeroEntityID = NewEntityID(0)
1010

1111
type EntityID sdk.Uint
1212

13+
// NewEntityID creates a new EntityID from the specified uint64.
1314
func NewEntityID(id uint64) EntityID {
1415
return EntityID(sdk.NewUint(id))
1516
}
1617

18+
// NewEntityIDFromString creates a new EntityID from the
19+
// provided string.
1720
func NewEntityIDFromString(str string) EntityID {
1821
return EntityID(sdk.NewUintFromString(str))
1922
}
2023

24+
// NewEntityIDFromBytes creates a new EntityID from the
25+
// provided byte slice. The byte slice must be encoded
26+
// as big-endian.
2127
func NewEntityIDFromBytes(b []byte) EntityID {
2228
s := new(big.Int).SetBytes(b)
2329
return EntityID(sdk.NewUintFromBigInt(s))
2430
}
2531

32+
// String returns the decimal string
33+
// representation of this EntityID.
2634
func (id EntityID) String() string {
2735
return sdk.Uint(id).String()
2836
}
2937

38+
// Bytes returns this EntityID as a
39+
// fixed-length big-endian byte slice
40+
// of size 32. The length is fixed in
41+
// order to facilitate sorting by EntityIDs
42+
// in the store.
3043
func (id EntityID) Bytes() []byte {
3144
var buf [32]byte
3245
bn := sdkUint2Big(sdk.Uint(id))
@@ -35,10 +48,15 @@ func (id EntityID) Bytes() []byte {
3548
return buf[:]
3649
}
3750

51+
// Inc returns a the current EntityID + 1.
52+
// A new EntityID is returned.
3853
func (id EntityID) Inc() EntityID {
3954
return EntityID(sdk.Uint(id).Add(sdk.OneUint()))
4055
}
4156

57+
// Dec returns the current EntityID - 1.
58+
// A new EntityID is returned. If the EntityID
59+
// is zero, this method is a no-op.
4260
func (id EntityID) Dec() EntityID {
4361
if id.IsZero() {
4462
return id
@@ -47,6 +65,9 @@ func (id EntityID) Dec() EntityID {
4765
return EntityID(sdk.Uint(id).Sub(sdk.OneUint()))
4866
}
4967

68+
// Cmp compares two entity IDs.
69+
// If a > b, 1 is returned. If a < b, -1 is returned.
70+
// If a == b, 0 is returned.
5071
func (id EntityID) Cmp(b EntityID) int {
5172
uintA := sdk.Uint(id)
5273
uintB := sdk.Uint(b)
@@ -62,18 +83,23 @@ func (id EntityID) Cmp(b EntityID) int {
6283
return 0
6384
}
6485

86+
// IsZero returns if this EntityID == 0.
6587
func (id EntityID) IsZero() bool {
6688
return sdk.Uint(id).IsZero()
6789
}
6890

91+
// Equals returns true if both
92+
// EntityIDs are equal.
6993
func (id EntityID) Equals(other EntityID) bool {
7094
return sdk.Uint(id).Equal(sdk.Uint(other))
7195
}
7296

97+
// MarshalAmino marshals this EntityID to Amino.
7398
func (id EntityID) MarshalAmino() (string, error) {
7499
return sdk.Uint(id).MarshalAmino()
75100
}
76101

102+
// UnmarshalAmino unmarshals this EntityID from Amino.
77103
func (id *EntityID) UnmarshalAmino(text string) error {
78104
var u sdk.Uint
79105
err := u.UnmarshalAmino(text)
@@ -85,14 +111,19 @@ func (id *EntityID) UnmarshalAmino(text string) error {
85111
return nil
86112
}
87113

114+
// UnmarshalJSON unmarshals this EntityID from JSON.
88115
func (id *EntityID) UnmarshalJSON(data []byte) error {
89116
return (*sdk.Uint)(id).UnmarshalJSON(data)
90117
}
91118

119+
// MarshalJSON marshals this EntityID to JSON.
92120
func (id EntityID) MarshalJSON() ([]byte, error) {
93121
return sdk.Uint(id).MarshalJSON()
94122
}
95123

124+
// Note: this method is a hack, and can be replaced once
125+
// the new sdk version (> v0.37.4) is released, since it
126+
// defines a .BigInt() method on sdk.Uint objects.
96127
func sdkUint2Big(in sdk.Uint) *big.Int {
97128
out, _ := new(big.Int).SetString(in.String(), 10)
98129
return out

storeutils/get_set.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ var (
1111
ErrAlreadyExists = errors.New("already exists")
1212
)
1313

14+
// Get unmarshals a binary object in the store
15+
// identified by sk and key into the object
16+
// identified by proto.
1417
func Get(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, proto interface{}) error {
1518
store := ctx.KVStore(sk)
1619
b := store.Get(key)
@@ -21,11 +24,18 @@ func Get(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, proto i
2124
return nil
2225
}
2326

27+
// Upsert inserts val into the store
28+
// identified by sk and at the key
29+
// identified by key.
2430
func Upsert(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, val interface{}) {
2531
store := ctx.KVStore(sk)
2632
store.Set(key, cdc.MustMarshalBinaryBare(val))
2733
}
2834

35+
// Create inserts val into the store
36+
// identified by sk and at the key
37+
// identified by key. Create will return
38+
// an error if the key already exists.
2939
func Create(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, val interface{}) error {
3040
if Has(ctx, sk, key) {
3141
return ErrAlreadyExists
@@ -34,6 +44,10 @@ func Create(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, val
3444
return nil
3545
}
3646

47+
// Update inserts val into the store
48+
// identified by sk and at the key
49+
// identified by key. Update will return
50+
// an error if the key does not exist.
3751
func Update(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, val interface{}) error {
3852
if !Has(ctx, sk, key) {
3953
return ErrNotFound
@@ -42,6 +56,10 @@ func Update(ctx sdk.Context, sk sdk.StoreKey, cdc *codec.Codec, key []byte, val
4256
return nil
4357
}
4458

59+
// Del deletes the value in the store
60+
// identified by sk and at the key
61+
// identified by key. Del will return an error
62+
// if the key does not exist.
4563
func Del(ctx sdk.Context, sk sdk.StoreKey, key []byte) error {
4664
if !Has(ctx, sk, key) {
4765
return ErrNotFound
@@ -51,6 +69,8 @@ func Del(ctx sdk.Context, sk sdk.StoreKey, key []byte) error {
5169
return nil
5270
}
5371

72+
// Has returns true if the specified key
73+
// exists in the store identified by sk.
5474
func Has(ctx sdk.Context, sk sdk.StoreKey, key []byte) bool {
5575
store := ctx.KVStore(sk)
5676
return store.Has(key)

storeutils/keys.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import (
55
"encoding/binary"
66
)
77

8+
// PrefixKeyString returns a byte slice
9+
// consisting of the prefix string concatenated
10+
// with all subkeys.
811
func PrefixKeyString(prefix string, subkeys ...[]byte) []byte {
912
buf := [][]byte{[]byte(prefix)}
1013
return PrefixKeyBytes(append(buf, subkeys...)...)
1114
}
1215

16+
// PrefixKeyBytes returns a byte slice consisting
17+
// of all subkeys concatenated together.
1318
func PrefixKeyBytes(subkeys ...[]byte) []byte {
1419
if len(subkeys) == 0 {
1520
return []byte{}
@@ -32,13 +37,19 @@ func PrefixKeyBytes(subkeys ...[]byte) []byte {
3237
return buf.Bytes()
3338
}
3439

40+
// Int64Subkey returns a byte slice
41+
// from the provided subkey suitable
42+
// for use as a key in the store.
3543
func Int64Subkey(subkey int64) []byte {
3644
if subkey < 0 {
3745
panic("cannot use negative numbers in subkeys")
3846
}
3947
return Uint64Subkey(uint64(subkey))
4048
}
4149

50+
// Uint64Subkey returns a byte slice
51+
// from the provided subkey suitable
52+
// for use as a key in the store.
4253
func Uint64Subkey(subkey uint64) []byte {
4354
b := make([]byte, 8, 8)
4455
binary.BigEndian.PutUint64(b, subkey)

storeutils/seq.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
55
)
66

7+
// IncrementSeq increments the EntityID in the store
8+
// identified by sk at the key seqKey.
79
func IncrementSeq(ctx sdk.Context, sk sdk.StoreKey, seqKey []byte) EntityID {
810
store := ctx.KVStore(sk)
911
seq := GetSeq(ctx, sk, seqKey).Inc()
1012
store.Set(seqKey, []byte(seq.String()))
1113
return seq
1214
}
1315

16+
// GetSeq returns the EntityID in the store
17+
// identified by sk at the key seqKey.
1418
func GetSeq(ctx sdk.Context, sk sdk.StoreKey, seqKey []byte) EntityID {
1519
store := ctx.KVStore(sk)
1620
if !store.Has(seqKey) {

0 commit comments

Comments
 (0)