Skip to content

Commit

Permalink
Merge branch '0xankit/ReadDataStringSanitize' of github.com:AssetMant…
Browse files Browse the repository at this point in the history
…le/modules into 0xankit/decDatafixes
  • Loading branch information
0xankit committed Dec 21, 2022
2 parents 3eb6751 + d28cd94 commit 3004412
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 13 additions & 9 deletions schema/data/utilities/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func readAccAddressData(dataString string) (data.AccAddressData, error) {
return base.AccAddressDataPrototype(), nil
}

accAddress, err := sdkTypes.AccAddressFromBech32(dataString)
accAddress, err := sdkTypes.AccAddressFromBech32(sanitizeDataString(dataString))
if err != nil {
return base.AccAddressDataPrototype(), err
}
Expand All @@ -33,7 +33,7 @@ func readBooleanData(dataString string) (data.BooleanData, error) {
return base.BooleanDataPrototype(), nil
}

Bool, err := strconv.ParseBool(dataString)
Bool, err := strconv.ParseBool(sanitizeDataString(dataString))
if err != nil {
return base.BooleanDataPrototype(), err
}
Expand All @@ -45,7 +45,7 @@ func readDecData(dataString string) (data.DecData, error) {
return base.DecDataPrototype(), nil
}

dec, err := sdkTypes.NewDecFromStr(dataString)
dec, err := sdkTypes.NewDecFromStr(sanitizeDataString(dataString))
if err != nil {
return base.DecDataPrototype(), err
}
Expand All @@ -57,7 +57,7 @@ func readHeightData(dataString string) (data.HeightData, error) {
return base.HeightDataPrototype(), nil
}

height, err := strconv.ParseInt(dataString, 10, 64)
height, err := strconv.ParseInt(sanitizeDataString(dataString), 10, 64)
if err != nil {
return base.HeightDataPrototype(), err
}
Expand All @@ -71,14 +71,14 @@ func readIDData(dataString string) (data.IDData, error) {
return base.IDDataPrototype(), nil
}

return base.NewIDData(baseIDs.NewStringID(dataString)), nil
return base.NewIDData(baseIDs.NewStringID(sanitizeDataString(dataString))), nil
}
func readListData(dataString string) (data.ListData, error) {
if dataString == "" {
return base.ListDataPrototype(), nil
}

dataStringList := stringUtilities.SplitListString(dataString)
dataStringList := stringUtilities.SplitListString(sanitizeDataString(dataString))
dataList := make([]data.Data, len(dataStringList))

for i, datumString := range dataStringList {
Expand All @@ -96,13 +96,13 @@ func readStringData(dataString string) (data.StringData, error) {
if dataString == "" {
return base.StringDataPrototype(), nil
}
return base.NewStringData(dataString), nil
return base.NewStringData(sanitizeDataString(dataString)), nil
}
func joinDataTypeAndValueStrings(dataType, dataValue string) string {
return strings.Join([]string{dataType, dataValue}, dataConstants.DataTypeAndValueSeparator)
}
func splitDataTypeAndValueStrings(dataTypeAndValueString string) (dataType, dataValue string) {
if dataTypeAndValue := strings.SplitN(dataTypeAndValueString, dataConstants.DataTypeAndValueSeparator, 2); len(dataTypeAndValue) < 2 {
if dataTypeAndValue := strings.SplitN(sanitizeDataString(dataTypeAndValueString), dataConstants.DataTypeAndValueSeparator, 2); len(dataTypeAndValue) < 2 {
return "", ""
} else {
return dataTypeAndValue[0], dataTypeAndValue[1]
Expand All @@ -112,7 +112,7 @@ func splitDataTypeAndValueStrings(dataTypeAndValueString string) (dataType, data
// ReadData
// CHECK-TODO if data type added see if added here
func ReadData(dataString string) (data.Data, error) {
dataTypeString, dataValueString := splitDataTypeAndValueStrings(dataString)
dataTypeString, dataValueString := splitDataTypeAndValueStrings(sanitizeDataString(dataString))
if dataTypeString != "" {
var Data data.Data

Expand Down Expand Up @@ -146,3 +146,7 @@ func ReadData(dataString string) (data.Data, error) {

return nil, errorConstants.IncorrectFormat
}

func sanitizeDataString(dataString string) string {
return strings.ReplaceAll(dataString, " ", "")
}
4 changes: 4 additions & 0 deletions schema/data/utilities/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ func TestReadData(t *testing.T) {
want data.Data
wantErr bool
}{
{"+ve with space in dataString", args{"L|A|cosmos1pkkayn066msg6kn33wnl5srhdt3tnu2vzasz9c, A|cosmos1x53dugvr4xvew442l9v2r5x7j8gfvged2zk5ef"}, base.NewListData(baseLists.NewDataList(dataList...)), false},
{"+ve with space in dataString", args{"L|A|cosmos1pkkayn066msg6kn33wnl5srhdt3tnu2vzasz9c,A| cosmos1x53dugvr4xvew442l9v2r5x7j8gfvged2zk5ef"}, base.NewListData(baseLists.NewDataList(dataList...)), false},
{"String Data", args{"S|newFact"}, base.NewStringData("newFact"), false},
{"-ve Unknown Data", args{"SomeRandomData"}, nil, true},
{"List Data", args{"L|A|cosmos1pkkayn066msg6kn33wnl5srhdt3tnu2vzasz9c,A|cosmos1x53dugvr4xvew442l9v2r5x7j8gfvged2zk5ef"}, base.NewListData(baseLists.NewDataList(dataList...)), false},
{"List Data empty list", args{"L|"}, base.NewListData(baseLists.NewDataList()), false},
{"Id Data", args{"I|data"}, base.NewIDData(baseIDs.NewStringID("data")), false},
{"Height Data", args{"H|100"}, base.NewHeightData(baseTypes.NewHeight(100)), false},
{"Height Data with space", args{" H| 100"}, base.NewHeightData(baseTypes.NewHeight(100)), false},
{"Height Data with space", args{"H |100"}, base.NewHeightData(baseTypes.NewHeight(100)), false},
{"Dec Data", args{"D|100"}, base.NewDecData(types.NewDec(100)), false},
{"Bool Data", args{"B|true"}, base.NewBooleanData(true), false},
{"AccAddress data", args{"A|cosmos1pkkayn066msg6kn33wnl5srhdt3tnu2vzasz9c"}, base.NewAccAddressData(fromAccAddress), false},
Expand Down

0 comments on commit 3004412

Please sign in to comment.