@@ -10,23 +10,36 @@ var ZeroEntityID = NewEntityID(0)
1010
1111type EntityID sdk.Uint
1212
13+ // NewEntityID creates a new EntityID from the specified uint64.
1314func NewEntityID (id uint64 ) EntityID {
1415 return EntityID (sdk .NewUint (id ))
1516}
1617
18+ // NewEntityIDFromString creates a new EntityID from the
19+ // provided string.
1720func 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.
2127func 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.
2634func (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.
3043func (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.
3853func (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.
4260func (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.
5071func (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.
6587func (id EntityID ) IsZero () bool {
6688 return sdk .Uint (id ).IsZero ()
6789}
6890
91+ // Equals returns true if both
92+ // EntityIDs are equal.
6993func (id EntityID ) Equals (other EntityID ) bool {
7094 return sdk .Uint (id ).Equal (sdk .Uint (other ))
7195}
7296
97+ // MarshalAmino marshals this EntityID to Amino.
7398func (id EntityID ) MarshalAmino () (string , error ) {
7499 return sdk .Uint (id ).MarshalAmino ()
75100}
76101
102+ // UnmarshalAmino unmarshals this EntityID from Amino.
77103func (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.
88115func (id * EntityID ) UnmarshalJSON (data []byte ) error {
89116 return (* sdk .Uint )(id ).UnmarshalJSON (data )
90117}
91118
119+ // MarshalJSON marshals this EntityID to JSON.
92120func (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.
96127func sdkUint2Big (in sdk.Uint ) * big.Int {
97128 out , _ := new (big.Int ).SetString (in .String (), 10 )
98129 return out
0 commit comments