Skip to content

Commit

Permalink
Refactoring Data interface AsListData method to AsDataList.
Browse files Browse the repository at this point in the history
  • Loading branch information
deepanshutr committed Apr 4, 2022
1 parent fb06688 commit db85400
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 99 deletions.
4 changes: 2 additions & 2 deletions modules/identities/auxiliaries/verify/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/persistenceOne/persistenceSDK/modules/identities/internal/key"
"github.com/persistenceOne/persistenceSDK/modules/metas/auxiliaries/supplement"
"github.com/persistenceOne/persistenceSDK/schema/helpers"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/mappables"
"github.com/persistenceOne/persistenceSDK/schema/types"
"github.com/persistenceOne/persistenceSDK/schema/types/base"
)

Expand All @@ -37,7 +37,7 @@ func (auxiliaryKeeper auxiliaryKeeper) Help(context sdkTypes.Context, request he
return newAuxiliaryResponse(Error)
}

if metaProperties.Get(ids.AuthenticationProperty).(types.ListData).Search(base.NewAccAddressData(auxiliaryRequest.Address)) == -1 {
if metaProperties.Get(ids.AuthenticationProperty).(lists.DataList).Search(base.NewAccAddressData(auxiliaryRequest.Address)) == -1 {
return newAuxiliaryResponse(errors.NotAuthorized)
}

Expand Down
5 changes: 3 additions & 2 deletions schema/types/base/accAddressData.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
"github.com/persistenceOne/persistenceSDK/utilities/meta"

Expand Down Expand Up @@ -52,8 +53,8 @@ func (accAddressData accAddressData) GenerateHashID() types.ID {
func (accAddressData accAddressData) AsAccAddress() (sdkTypes.AccAddress, error) {
return accAddressData.Value, nil
}
func (accAddressData accAddressData) AsListData() (types.ListData, error) {
zeroValue, _ := listData{}.ZeroValue().AsListData()
func (accAddressData accAddressData) AsDataList() (lists.DataList, error) {
zeroValue, _ := listData{}.ZeroValue().AsDataList()
return zeroValue, errors.IncorrectFormat
}
func (accAddressData accAddressData) AsString() (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions schema/types/base/booleanData.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
)

Expand Down Expand Up @@ -58,8 +59,8 @@ func (booleanData booleanData) AsAccAddress() (sdkTypes.AccAddress, error) {
zeroValue, _ := accAddressData{}.ZeroValue().AsAccAddress()
return zeroValue, errors.IncorrectFormat
}
func (booleanData booleanData) AsListData() (types.ListData, error) {
zeroValue, _ := listData{}.ZeroValue().AsListData()
func (booleanData booleanData) AsDataList() (lists.DataList, error) {
zeroValue, _ := listData{}.ZeroValue().AsDataList()
return zeroValue, errors.IncorrectFormat
}
func (booleanData booleanData) AsString() (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions schema/types/base/decData.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
"github.com/persistenceOne/persistenceSDK/utilities/meta"
)
Expand Down Expand Up @@ -57,8 +58,8 @@ func (decData decData) AsAccAddress() (sdkTypes.AccAddress, error) {
zeroValue, _ := accAddressData{}.ZeroValue().AsAccAddress()
return zeroValue, errors.IncorrectFormat
}
func (decData decData) AsListData() (types.ListData, error) {
zeroValue, _ := listData{}.ZeroValue().AsListData()
func (decData decData) AsDataList() (lists.DataList, error) {
zeroValue, _ := listData{}.ZeroValue().AsDataList()
return zeroValue, errors.IncorrectFormat
}
func (decData decData) AsString() (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions schema/types/base/heightData.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
"github.com/persistenceOne/persistenceSDK/utilities/meta"
)
Expand Down Expand Up @@ -53,8 +54,8 @@ func (heightData heightData) AsAccAddress() (sdkTypes.AccAddress, error) {
zeroValue, _ := accAddressData{}.ZeroValue().AsAccAddress()
return zeroValue, errors.IncorrectFormat
}
func (heightData heightData) AsListData() (types.ListData, error) {
zeroValue, _ := listData{}.ZeroValue().AsListData()
func (heightData heightData) AsDataList() (lists.DataList, error) {
zeroValue, _ := listData{}.ZeroValue().AsDataList()
return zeroValue, errors.IncorrectFormat
}
func (heightData heightData) AsString() (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions schema/types/base/idData.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
"github.com/persistenceOne/persistenceSDK/utilities/meta"
)
Expand Down Expand Up @@ -49,8 +50,8 @@ func (idData idData) AsAccAddress() (sdkTypes.AccAddress, error) {
zeroValue, _ := accAddressData{}.ZeroValue().AsAccAddress()
return zeroValue, errors.EntityNotFound
}
func (idData idData) AsListData() (types.ListData, error) {
zeroValue, _ := listData{}.ZeroValue().AsListData()
func (idData idData) AsDataList() (lists.DataList, error) {
zeroValue, _ := listData{}.ZeroValue().AsDataList()
return zeroValue, errors.IncorrectFormat
}
func (idData idData) AsString() (string, error) {
Expand Down
31 changes: 8 additions & 23 deletions schema/types/base/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,15 @@ import (
// TODO Revise the code
type list []traits.Listable

func (list list) Search(f func()) int {
// TODO implement me
panic("implement me")
}
var _ types.List = (*list)(nil)

func (list list) Apply(f func()) types.List {
// TODO implement me
panic("implement me")
func (list list) GetList() []traits.Listable {
return list
}

func (list list) Mutate(listable ...traits.Listable) types.List {
// TODO implement me
panic("implement me")
func (list list) Size() int { // TODO write test
return len(list)
}

var _ types.List = (*list)(nil)

func (list list) SearchListable(listable traits.Listable) int {
func (list list) Search(listable traits.Listable) int {
index := sort.Search(
len(list),
func(i int) bool {
Expand All @@ -46,14 +37,9 @@ func (list list) SearchListable(listable traits.Listable) int {

return index
}

func (list list) GetList() []traits.Listable {
return list
}

func (list list) Add(listableList ...traits.Listable) types.List {
for _, listable := range listableList {
if list.SearchListable(listable) != len(list) {
if list.Search(listable) != len(list) {
return list
}

Expand All @@ -71,11 +57,10 @@ func (list list) Add(listableList ...traits.Listable) types.List {

return list
}

func (list list) Remove(listableList ...traits.Listable) types.List {
// TODO check if the return is properly assigned to
for _, listable := range listableList {
if index := list.SearchListable(listable); index != len(list) {
if index := list.Search(listable); index != len(list) {
list = append(list[:index], list[index+1:]...)
}
}
Expand Down
44 changes: 14 additions & 30 deletions schema/types/base/listData.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

"github.com/persistenceOne/persistenceSDK/constants"
"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
)

type listData struct {
Value sortedDataList `json:"value"`
Value lists.DataList `json:"value"`
}

var _ types.ListData = (*listData)(nil)
var _ types.Data = (*listData)(nil)

func (listData listData) GetID() types.ID {
return dataID{
Expand All @@ -26,6 +27,7 @@ func (listData listData) GetID() types.ID {
}
}
func (listData listData) Compare(data types.Data) int {
// TODO write test and see if correct
compareListData, Error := listDataFromInterface(data)
if Error != nil {
panic(Error)
Expand All @@ -34,9 +36,9 @@ func (listData listData) Compare(data types.Data) int {
return strings.Compare(listData.GenerateHashID().String(), compareListData.GenerateHashID().String())
}
func (listData listData) String() string {
dataStringList := make([]string, len(listData.Value))
dataStringList := make([]string, listData.Value.Size())

for i, data := range listData.Value {
for i, data := range listData.Value.GetList() {
dataStringList[i] = data.String()
}

Expand All @@ -49,13 +51,13 @@ func (listData listData) ZeroValue() types.Data {
return NewListData([]types.Data{}...)
}
func (listData listData) GenerateHashID() types.ID {
if len(listData.Value) == 0 {
if listData.Value.Size() == 0 {
return NewID("")
}

hashList := make([]string, len(listData.Value))
hashList := make([]string, listData.Value.Size())

for i, data := range listData.Value {
for i, data := range listData.Value.GetList() {
hashList[i] = data.GenerateHashID().String()
}

Expand All @@ -67,8 +69,8 @@ func (listData listData) AsAccAddress() (sdkTypes.AccAddress, error) {
zeroValue, _ := accAddressData{}.ZeroValue().AsAccAddress()
return zeroValue, errors.IncorrectFormat
}
func (listData listData) AsListData() (types.ListData, error) {
return listData, nil
func (listData listData) AsDataList() (lists.DataList, error) {
return listData.Value, nil
}
func (listData listData) AsString() (string, error) {
zeroValue, _ := stringData{}.ZeroValue().AsString()
Expand All @@ -89,26 +91,6 @@ func (listData listData) AsID() (types.ID, error) {
func (listData listData) Get() interface{} {
return listData.Value
}
func (listData listData) Search(data types.Data) int {
return listData.Value.Search(data)
}
func (listData listData) GetList() []types.Data {
return listData.Value
}
func (listData listData) Add(dataList ...types.Data) types.ListData {
for _, data := range dataList {
listData.Value = listData.Value.Add(data).(sortedDataList)
}

return listData
}
func (listData listData) Remove(dataList ...types.Data) types.ListData {
for _, data := range dataList {
listData.Value = listData.Value.Remove(data).(sortedDataList)
}

return listData
}
func listDataFromInterface(data types.Data) (listData, error) {
switch value := data.(type) {
case listData:
Expand All @@ -119,10 +101,12 @@ func listDataFromInterface(data types.Data) (listData, error) {
}

func NewListData(value ...types.Data) types.Data {
return listData{}.Add(value...)
// TODO Implement
return nil
}

func ReadAccAddressListData(dataString string) (types.Data, error) {
// TODO revise
if dataString == "" {
return listData{}.ZeroValue(), nil
}
Expand Down
47 changes: 22 additions & 25 deletions schema/types/base/listID.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import (
"strings"

"github.com/persistenceOne/persistenceSDK/constants"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/traits"

"github.com/persistenceOne/persistenceSDK/schema/types"
)

type listID struct {
IDList []types.ID `json:"idList"`
IDList lists.IDList `json:"idList"`
}

var _ types.ID = (*listID)(nil)
var _ types.List = (*listID)()
var _ types.List = (*listID)(nil)

func (listID listID) String() string {
idStringList := make([]string, len(listID.IDList))
idStringList := make([]string, listID.Size())

for i, id := range listID.IDList {
for i, id := range listID.IDList.GetList() {
idStringList[i] = id.String()
}

Expand All @@ -31,7 +33,7 @@ func (listID listID) String() string {
func (listID listID) Bytes() []byte {
var byteList []byte

for _, id := range listID.IDList {
for _, id := range listID.IDList.GetList() {
byteList = append(byteList, id.Bytes()...)
}

Expand All @@ -40,28 +42,23 @@ func (listID listID) Bytes() []byte {
func (listID listID) Compare(compareID types.ID) int {
return bytes.Compare(listID.Bytes(), compareID.Bytes())
}

func (listID listID) GetList() []interface{} {
// TODO
return nil
}
func (listID listID) Search(f func()) int {
// TODO
return 0
func (listID listID) GetList() []traits.Listable {
// TODO implement me
panic("implement me")
}
func (listID listID) Apply(f func()) types.List {
// TODO
return nil
func (listID listID) Size() int {
// TODO implement me
panic("implement me")
}
func (listID listID) Add(i ...interface{}) types.List {
// TODO
return nil
func (listID listID) Search(listable traits.Listable) int {
// TODO implement me
panic("implement me")
}
func (listID listID) Remove(i ...interface{}) types.List {
// TODO
return nil
func (listID listID) Add(listableList ...traits.Listable) types.List {
// TODO implement me
panic("implement me")
}
func (listID listID) Mutate(i ...interface{}) types.List {
// TODO
return nil
func (listID listID) Remove(listableList ...traits.Listable) types.List {
// TODO implement me
panic("implement me")
}
5 changes: 3 additions & 2 deletions schema/types/base/stringData.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"

"github.com/persistenceOne/persistenceSDK/constants/errors"
"github.com/persistenceOne/persistenceSDK/schema/lists"
"github.com/persistenceOne/persistenceSDK/schema/types"
"github.com/persistenceOne/persistenceSDK/utilities/meta"
)
Expand Down Expand Up @@ -49,8 +50,8 @@ func (stringData stringData) AsAccAddress() (sdkTypes.AccAddress, error) {
zeroValue, _ := accAddressData{}.ZeroValue().AsAccAddress()
return zeroValue, errors.EntityNotFound
}
func (stringData stringData) AsListData() (types.ListData, error) {
zeroValue, _ := listData{}.ZeroValue().AsListData()
func (stringData stringData) AsDataList() (lists.DataList, error) {
zeroValue, _ := listData{}.ZeroValue().AsDataList()
return zeroValue, errors.IncorrectFormat
}
func (stringData stringData) AsString() (string, error) {
Expand Down
4 changes: 3 additions & 1 deletion schema/types/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package types

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

"github.com/persistenceOne/persistenceSDK/schema/lists"
)

// TODO URI and ID data type
Expand All @@ -25,7 +27,7 @@ type Data interface {
GenerateHashID() ID

AsAccAddress() (sdkTypes.AccAddress, error)
AsListData() (ListData, error)
AsDataList() (lists.DataList, error)
AsString() (string, error)
AsDec() (sdkTypes.Dec, error)
AsHeight() (Height, error)
Expand Down
Loading

0 comments on commit db85400

Please sign in to comment.