Skip to content

Commit

Permalink
feat(mappable,record): defining and implementing ReadMappableFromIter…
Browse files Browse the repository at this point in the history
…ator generic method

Signed-off-by: Deepanshu Tripathi <[email protected]>
  • Loading branch information
deepanshutr committed Sep 4, 2024
1 parent 9ae7d22 commit 261472f
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 63 deletions.
9 changes: 9 additions & 0 deletions helpers/mappable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ package helpers

import (
"github.com/cosmos/cosmos-sdk/codec"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
)

type Mappable interface {
codec.ProtoMarshaler
ValidateBasic() error
}

func ReadMappableFromIterator[T Mappable](iterator sdkTypes.Iterator, mappable T) Mappable {
if err := mappable.Unmarshal(iterator.Value()); err != nil {
panic(err)
}

return mappable
}
10 changes: 1 addition & 9 deletions x/assets/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(mappable.GetAsset(Mappable))
return NewRecord(mappable.GetAsset(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down
10 changes: 1 addition & 9 deletions x/classifications/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(mappable.GetClassification(Mappable))
return NewRecord(mappable.GetClassification(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down
10 changes: 1 addition & 9 deletions x/identities/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(mappable.GetIdentity(Mappable))
return NewRecord(mappable.GetIdentity(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down
10 changes: 1 addition & 9 deletions x/maintainers/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(mappable.GetMaintainer(Mappable))
return NewRecord(mappable.GetMaintainer(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down
10 changes: 1 addition & 9 deletions x/metas/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(mappable.GetData(Mappable))
return NewRecord(mappable.GetData(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down
10 changes: 1 addition & 9 deletions x/orders/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(mappable.GetOrder(Mappable))
return NewRecord(mappable.GetOrder(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down
10 changes: 1 addition & 9 deletions x/splits/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ func (record *Record) WithKey(Key helpers.Key) helpers.Record {
return record
}
func (record *Record) ReadFromIterator(iterator sdkTypes.Iterator) helpers.Record {
Bytes := iterator.Value()
if Bytes == nil {
return Prototype()
}

Mappable := record.GetMappable()
base.CodecPrototype().MustUnmarshal(iterator.Value(), Mappable)

return NewRecord(baseIDs.PrototypeSplitID().MustGetFromPrefixedStoreKeyBytes(record.GetKey().GenerateStorePrefixBytes(), iterator.Key()), mappable.GetSplit(Mappable))
return NewRecord(baseIDs.PrototypeSplitID().MustGetFromPrefixedStoreKeyBytes(record.GetKey().GenerateStorePrefixBytes(), iterator.Key()), mappable.GetSplit(helpers.ReadMappableFromIterator(iterator, record.GetMappable())))
}
func (record *Record) Read(kvStore sdkTypes.KVStore) helpers.Record {
if record.GetKey() == nil || len(record.GetKey().GeneratePrefixedStoreKeyBytes()) == 0 {
Expand Down

0 comments on commit 261472f

Please sign in to comment.