Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding Sanitize() method to Data interface #168

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
95170b0
fix: handling nil value for `compare` method of decData
0xankit Dec 8, 2022
19574b3
Merge branch 'master' into 0xankit/decDatafixes
deepanshutr Dec 9, 2022
764911a
fix: add `sanitize` method to `Data` interface
0xankit Dec 9, 2022
8ac65d1
Merge branch '0xankit/decDatafixes' of github.com:AssetMantle/modules…
0xankit Dec 9, 2022
51f8a47
test: update test for GetID method
0xankit Dec 9, 2022
30aac7c
test(schema): add `sanitize` method for accAddressData
0xankit Dec 9, 2022
9338cd3
test(schema): add `sanitize` method for booleanData
0xankit Dec 9, 2022
9dac67c
test(schema): add `sanitize` method for heightData
0xankit Dec 9, 2022
9ec054d
test(schema): add `sanitize` method for idData
0xankit Dec 9, 2022
79299ce
test(schema): add `sanitize` method for listData
0xankit Dec 12, 2022
7a2be4b
test(schema): add `sanitize` method for stringData
0xankit Dec 12, 2022
d28cd94
closes #123
0xankit Dec 12, 2022
2576b11
test(schema): update tests for propertylist
0xankit Dec 13, 2022
7b266b5
tests(schema): closes #30
0xankit Dec 13, 2022
b5012c0
Merge branch 'master' of github.com:AssetMantle/modules into 0xankit/…
0xankit Dec 13, 2022
79a69e5
fix: resolving merge conflicts
0xankit Dec 13, 2022
feb7915
test(schema): update test for accAddressData compare method
0xankit Dec 13, 2022
1239590
Merge branch 'master' of github.com:AssetMantle/modules into 0xankit/…
0xankit Dec 14, 2022
e0d80a7
test: mock fix
0xankit Dec 14, 2022
7fe7c7c
test: closes #165
0xankit Dec 14, 2022
9d3e003
test(schema): update tests for propertyList
0xankit Dec 14, 2022
3eb6751
test(schema): add test for nil case in idList
0xankit Dec 14, 2022
f2b4a84
test(schema): add test for maintainers
0xankit Dec 14, 2022
12787d6
code refactoring
0xankit Dec 14, 2022
5e3a5d5
test(schema): add tests for order Documents
0xankit Dec 14, 2022
7a84786
code refactoring
0xankit Dec 14, 2022
33fecbd
fix(schema): add sanitize method for documents
0xankit Dec 15, 2022
8fad47f
test(schema): add tests for ids utilities
0xankit Dec 15, 2022
85797a4
fix(schema): add sanitize method for dataList
0xankit Dec 15, 2022
756cc1e
fix(schema): add sanitize method for idList
0xankit Dec 15, 2022
3045e99
fix(schema): add sanitize method for propertyList
0xankit Dec 16, 2022
747f4df
fix: add sanitize method for lists
0xankit Dec 16, 2022
cbd3307
tests(schema): add tests for storeKeyPrefix
0xankit Dec 16, 2022
ce0cf08
test(schema): add tests for splitIDs ID
0xankit Dec 16, 2022
aac9b1d
test(schema): add tests for ownableID
0xankit Dec 16, 2022
e4833a5
test(schema): add tests for assetID
0xankit Dec 19, 2022
97593be
test(schema): add tests for classification IDs
0xankit Dec 19, 2022
cffbfa6
test(schema): add tests for identityIDs
0xankit Dec 19, 2022
63a607b
test(schema): add tests for maintainerID
0xankit Dec 20, 2022
0a5fb29
test(schema): add tests for orderID
0xankit Dec 20, 2022
5709958
code refactoring
0xankit Dec 21, 2022
a4eddc7
Merge pull request #181 from AssetMantle/0xankit/schemaTests
0xankit Dec 21, 2022
3004412
Merge branch '0xankit/ReadDataStringSanitize' of github.com:AssetMant…
0xankit Dec 21, 2022
37f268f
Merge branch '0xankit/decDatafixes' of github.com:AssetMantle/modules…
0xankit Dec 21, 2022
342dc34
Merge branch '0xankit/sanitizeData' of github.com:AssetMantle/modules…
0xankit Dec 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions schema/data/base/accAddressData.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (accAddressData accAddressData) Compare(listable traits.Listable) int {
if err != nil {
panic(err)
}
if sanitizedAccAddressData, err := accAddressData.Sanitize(); err != nil {
accAddressData.Value = sanitizedAccAddressData.(data.AccAddressData).Get()
}
if sanitizedCompareAccAddressData, err := compareAccAddressData.Sanitize(); err != nil {
compareAccAddressData.Value = sanitizedCompareAccAddressData.(data.AccAddressData).Get()
}

return bytes.Compare(accAddressData.Value.Bytes(), compareAccAddressData.Value.Bytes())
}
Expand All @@ -56,6 +62,13 @@ func (accAddressData accAddressData) Get() sdkTypes.AccAddress {
return accAddressData.Value
}

func (accAddressData accAddressData) Sanitize() (data.Data, error) {
if accAddressData.Value.Empty() {
0xankit marked this conversation as resolved.
Show resolved Hide resolved
return accAddressData.ZeroValue(), nil
}
return accAddressData, nil
}

func accAddressDataFromInterface(listable traits.Listable) (accAddressData, error) {
switch value := listable.(type) {
case accAddressData:
Expand Down
2 changes: 2 additions & 0 deletions schema/data/base/accAddressData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func Test_accAddressData_Compare(t *testing.T) {
args args
want int
}{
{"-ve nil", fields{}, args{NewAccAddressData(nil)}, 0},
{"+ve with nil", fields{types.AccAddress{}}, args{NewAccAddressData(nil)}, 0},
{"-ve empty String", fields{fromAccAddress}, args{accAddressData{}}, 1},
{"+ve", fields{fromAccAddress}, args{accAddressData{fromAccAddress}}, 0},
}
Expand Down
4 changes: 4 additions & 0 deletions schema/data/base/booleanData.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (booleanData booleanData) Get() bool {
return booleanData.Value
}

func (booleanData booleanData) Sanitize() (data.Data, error) {
return booleanData, nil
0xankit marked this conversation as resolved.
Show resolved Hide resolved
}

func booleanDataFromInterface(listable traits.Listable) (booleanData, error) {
switch value := listable.(type) {
case booleanData:
Expand Down
26 changes: 1 addition & 25 deletions schema/data/base/booleanData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,6 @@ func TestNewBooleanData(t *testing.T) {
}
}

func TestBooleanDataFromInterface(t *testing.T) {
type args struct {
dataString data.Data
}
tests := []struct {
name string
args args
want data.Data
wantErr assert.ErrorAssertionFunc
}{
{"-ve", args{NewBooleanData(false)}, booleanData{false}, assert.NoError},
{"+ve", args{NewBooleanData(true)}, booleanData{true}, assert.NoError},
{"-ve", args{NewStringData("test")}, booleanData{false}, assert.Error},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := booleanDataFromInterface(tt.args.dataString)
if !tt.wantErr(t, err, fmt.Sprintf("booleanDataFromInterface(%v)", tt.args.dataString)) {
return
}
assert.Equalf(t, tt.want, got, "booleanDataFromInterface(%v)", tt.args.dataString)
})
}
}

func Test_booleanDataFromInterface(t *testing.T) {
type args struct {
listable traits.Listable
Expand Down Expand Up @@ -99,6 +74,7 @@ func Test_booleanData_Compare(t *testing.T) {
args args
want int
}{
{"+ve with nil", fields{}, args{booleanData{}}, 0},
{"+ve", fields{false}, args{booleanData{true}}, -1},
{"+ve", fields{true}, args{booleanData{false}}, 1},
{"+ve", fields{false}, args{booleanData{false}}, 0},
Expand Down
17 changes: 17 additions & 0 deletions schema/data/base/decData.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func (decData decData) Compare(listable traits.Listable) int {
panic(err)
}

if sanitizedDecData, err := decData.Sanitize(); err != nil {
decData.Value = sanitizedDecData.(data.DecData).Get()
}
if sanitizedDecData, err := compareDecData.Sanitize(); err != nil {
compareDecData.Value = sanitizedDecData.(data.DecData).Get()
}

if decData.Value.GT(compareDecData.Value) {
return 1
} else if decData.Value.LT(compareDecData.Value) {
Expand All @@ -41,6 +48,9 @@ func (decData decData) String() string {
return decData.Value.String()
}
func (decData decData) Bytes() []byte {
if sanitizedDecData, err := decData.Sanitize(); err != nil {
decData.Value = sanitizedDecData.(data.DecData).Get()
0xankit marked this conversation as resolved.
Show resolved Hide resolved
}
return decData.Value.Bytes()
}
func (decData decData) GetType() ids.StringID {
Expand All @@ -60,6 +70,13 @@ func (decData decData) Get() sdkTypes.Dec {
return decData.Value
}

func (decData decData) Sanitize() (data.Data, error) {
if decData.Value.Int == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that there is no reference for Int in my cosmos-sdk version, can you please test this, The only option I can find is BigInt()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int is being used as anonymous field here which is of the type big.Int. BigInt() is a method but we needed to check the value here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is relevant unit tests:

{"+ve with nil", fields{}, args{decData{}}, 0},
{"+ve with nil", fields{types.Dec{}}, args{decData{types.Dec{}}}, 0},
{"+ve with zero dec", fields{types.ZeroDec()}, args{decData{types.ZeroDec()}}, 0},
{"+ve", fields{types.NewDec(100)}, args{decData{types.NewDec(100)}}, 0},
{"-ve", fields{types.NewDec(-100)}, args{decData{types.NewDec(100)}}, -1},
{"+ve with -ve Dec", fields{types.NewDec(-100)}, args{decData{types.NewDec(-100)}}, 0},

return decData.ZeroValue(), constants.MetaDataError
}
return decData, nil
}

func decDataFromInterface(listable traits.Listable) (decData, error) {
switch value := listable.(type) {
case decData:
Expand Down
6 changes: 1 addition & 5 deletions schema/data/base/decData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,7 @@ func Test_decData_GetID(t *testing.T) {
decData := decData{
Value: tt.fields.Value,
}
if tt.wantPanic {
require.Panics(t, func() {
decData.GetID()
})
} else if got := decData.GetID(); !reflect.DeepEqual(got, tt.want) {
if got := decData.GetID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetID() = %v, want %v", got, tt.want)
}
})
Expand Down
17 changes: 17 additions & 0 deletions schema/data/base/heightData.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func (heightData heightData) Compare(listable traits.Listable) int {
panic(err)
}

if sanitizedHeightData, err := heightData.Sanitize(); err != nil {
heightData.Value = sanitizedHeightData.(data.HeightData).Get()
}
if sanitizedCompareHeightData, err := compareHeightData.Sanitize(); err != nil {
compareHeightData.Value = sanitizedCompareHeightData.(data.HeightData).Get()
}

return heightData.Value.Compare(compareHeightData.Value)
}
func (heightData heightData) String() string {
Expand All @@ -56,9 +63,19 @@ func (heightData heightData) GenerateHashID() ids.HashID {
return baseIDs.GenerateHashID(heightData.Bytes())
}
func (heightData heightData) Get() types.Height {
if sanitizedHeightData, err := heightData.Sanitize(); err != nil {
return sanitizedHeightData.(data.HeightData).Get()
}
return heightData.Value
}

func (heightData heightData) Sanitize() (data.Data, error) {
if heightData.Value == nil {
return heightData.ZeroValue(), constants.MetaDataError
}
return heightData, nil
}

func heightDataFromInterface(listable traits.Listable) (heightData, error) {
switch value := listable.(type) {
case heightData:
Expand Down
8 changes: 4 additions & 4 deletions schema/data/base/heightData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Test_heightData_Compare(t *testing.T) {
args args
want int
}{

{"+ve with nil", fields{}, args{heightData{}}, 0},
{"Test for Equal case", fields{baseTypes.NewHeight(100)}, args{heightData{baseTypes.NewHeight(100)}}, 0},
{"Test for LT case", fields{baseTypes.NewHeight(0)}, args{heightData{baseTypes.NewHeight(100)}}, -1},
{"Test for GT case", fields{baseTypes.NewHeight(100)}, args{heightData{baseTypes.NewHeight(0)}}, 1},
Expand All @@ -118,7 +118,7 @@ func Test_heightData_GenerateHashID(t *testing.T) {
fields fields
want ids.HashID
}{

{"+ve with nil", fields{}, baseIDs.GenerateHashID()},
{"Test for zero value", fields{baseTypes.NewHeight(-1)}, baseIDs.GenerateHashID()},
{"Test for -ve value", fields{baseTypes.NewHeight(-100)}, baseIDs.GenerateHashID()},
{"Test for +ve value", fields{baseTypes.NewHeight(100)}, baseIDs.GenerateHashID(heightData{baseTypes.NewHeight(100)}.Bytes())},
Expand All @@ -144,7 +144,7 @@ func Test_heightData_Get(t *testing.T) {
fields fields
want types.Height
}{

{"+ve with nil", fields{}, baseTypes.NewHeight(-1)},
{"Test for zero value", fields{baseTypes.NewHeight(0)}, heightData{baseTypes.NewHeight(0)}.Value},
{"Test for +ve value", fields{baseTypes.NewHeight(100)}, heightData{baseTypes.NewHeight(100)}.Value},
{"Test for -ve value", fields{baseTypes.NewHeight(-100)}, heightData{baseTypes.NewHeight(-100)}.Value},
Expand Down Expand Up @@ -273,7 +273,7 @@ func Test_heightData_Bytes(t *testing.T) {
want []byte
}{
{"+ve with ZeroHeight", fields{baseTypes.NewHeight(-1)}, baseTypes.NewHeight(-1).Bytes()},
{"+ve with nil", fields{nil}, []byte{}},
{"+ve with nil", fields{nil}, baseTypes.NewHeight(-1).Bytes()},
{"+ve", fields{baseTypes.NewHeight(100)}, baseTypes.NewHeight(100).Bytes()},
{"+ve with max int", fields{baseTypes.NewHeight(int64(^uint(0) >> 1))}, baseTypes.NewHeight(int64(^uint(0) >> 1)).Bytes()},
}
Expand Down
24 changes: 24 additions & 0 deletions schema/data/base/idData.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ func (idData idData) Compare(listable traits.Listable) int {
panic(err)
}

if sanitizedIDData, err := idData.Sanitize(); err != nil {
idData.Value = sanitizedIDData.(data.IDData).Get()
}

if sanitizedCompareIDData, err := compareIDData.Sanitize(); err != nil {
compareIDData.Value = sanitizedCompareIDData.(data.IDData).Get()
}

return bytes.Compare(idData.Value.Bytes(), compareIDData.Value.Bytes())
}
func (idData idData) String() string {
if sanitizedIDData, err := idData.Sanitize(); err != nil {
idData.Value = sanitizedIDData.(data.IDData).Get()
}
return idData.Value.String()
}
func (idData idData) Bytes() []byte {
if sanitizedIDData, err := idData.Sanitize(); err != nil {
idData.Value = sanitizedIDData.(data.IDData).Get()
}
return idData.Value.Bytes()
}
func (idData idData) GetType() ids.StringID {
Expand All @@ -47,9 +61,19 @@ func (idData idData) GenerateHashID() ids.HashID {
return baseIDs.GenerateHashID(idData.Bytes())
}
func (idData idData) Get() ids.ID {
if sanitizedIDData, err := idData.Sanitize(); err != nil {
return sanitizedIDData.(data.IDData).Get()
}
return idData.Value
}

func (idData idData) Sanitize() (data.Data, error) {
if idData.Value == nil {
return idData.ZeroValue(), constants.MetaDataError
}
return idData, nil
}

func idDataFromInterface(listable traits.Listable) (idData, error) {
switch value := listable.(type) {
case idData:
Expand Down
5 changes: 4 additions & 1 deletion schema/data/base/idData_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func Test_idDataFromInterface(t *testing.T) {
}{
{"+ve", args{NewIDData(NewStringData("Data"))}, idData{NewStringData("Data")}, assert.NoError},
{"+ve", args{NewIDData(NewStringData(""))}, idData{NewStringData("")}, assert.NoError},
{"-ve", args{NewStringData("Data")}, idData{}, assert.Error},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -69,6 +68,7 @@ func Test_idData_Bytes(t *testing.T) {
fields fields
want []byte
}{
{"+ve with nil", fields{nil}, []byte{}},
{"+ve", fields{NewStringData("")}, []byte{}},
{"+ve", fields{NewStringData("Data")}, NewStringData("Data").Bytes()},
}
Expand All @@ -95,6 +95,7 @@ func Test_idData_Compare(t *testing.T) {
args args
want int
}{
{"+ve with nil", fields{nil}, args{idData{}}, 0},
{"+ve", fields{NewStringData("Data")}, args{idData{NewStringData("Data")}}, 0},
{"+ve", fields{NewStringData("Data")}, args{idData{NewStringData("0")}}, 1},
}
Expand Down Expand Up @@ -139,6 +140,7 @@ func Test_idData_Get(t *testing.T) {
fields fields
want ids.ID
}{
{"+ve with nil", fields{nil}, baseIDs.NewStringID("")},
{"+ve", fields{NewStringData("Data")}, NewStringData("Data")},
{"+ve", fields{NewStringData("")}, NewStringData("")},
}
Expand Down Expand Up @@ -203,6 +205,7 @@ func Test_idData_String(t *testing.T) {
fields fields
want string
}{
{"+ve with nil", fields{nil}, ""},
{"+ve", fields{NewStringData("Data")}, "Data"},
}
for _, tt := range tests {
Expand Down
36 changes: 30 additions & 6 deletions schema/data/base/listData.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,29 @@ type listData struct {
var _ data.ListData = (*listData)(nil)

func (listData listData) Get() []data.Data {
if sanitizedData, err := listData.Sanitize(); err != nil {
listData.Value = base.NewDataList(sanitizedData.(data.ListData).Get()...)
}
return listData.Value.GetList()
}
func (listData listData) Search(data data.Data) (int, bool) {
return listData.Value.Search(data)
func (listData listData) Search(_data data.Data) (int, bool) {
if sanitizedData, err := listData.Sanitize(); err != nil {
listData.Value = base.NewDataList(sanitizedData.(data.ListData).Get()...)
}
return listData.Value.Search(_data)
}
func (listData listData) Add(data ...data.Data) data.ListData {
listData.Value = listData.Value.Add(data...)
func (listData listData) Add(_data ...data.Data) data.ListData {
if sanitizedData, err := listData.Sanitize(); err != nil {
listData.Value = base.NewDataList(sanitizedData.(data.ListData).Get()...)
}
listData.Value = listData.Value.Add(_data...)
return listData
}
func (listData listData) Remove(data ...data.Data) data.ListData {
listData.Value = listData.Value.Remove(data...)
func (listData listData) Remove(_data ...data.Data) data.ListData {
if sanitizedData, err := listData.Sanitize(); err != nil {
listData.Value = base.NewDataList(sanitizedData.(data.ListData).Get()...)
}
listData.Value = listData.Value.Remove(_data...)
return listData
}
func (listData listData) GetID() ids.DataID {
Expand All @@ -59,6 +71,9 @@ func (listData listData) String() string {
return stringUtilities.JoinListStrings(dataStrings...)
}
func (listData listData) Bytes() []byte {
if sanitizedData, err := listData.Sanitize(); err != nil {
listData.Value = base.NewDataList(sanitizedData.(data.ListData).Get()...)
}
bytesList := make([][]byte, listData.Value.Size())

for i, datum := range listData.Value.GetList() {
Expand All @@ -83,6 +98,15 @@ func (listData listData) GenerateHashID() ids.HashID {

return baseIDs.GenerateHashID(listData.Bytes())
}

func (listData listData) Sanitize() (data.Data, error) {
//TODO implement me
if listData.Value == nil {
return listData.ZeroValue(), errorConstants.MetaDataError
}
return listData, nil
}

func listDataFromInterface(listable traits.Listable) (listData, error) {
switch value := listable.(type) {
case listData:
Expand Down
Loading