From d28cd942a6eeea2663290a5c9c5e54c0d56aeae2 Mon Sep 17 00:00:00 2001 From: Ankit Kumar Date: Mon, 12 Dec 2022 18:08:22 +0530 Subject: [PATCH] closes #123 --- schema/data/utilities/string.go | 22 +++++++++++++--------- schema/data/utilities/string_test.go | 6 ++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/schema/data/utilities/string.go b/schema/data/utilities/string.go index 24b8a9d1c..d6c1aafd6 100644 --- a/schema/data/utilities/string.go +++ b/schema/data/utilities/string.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 { @@ -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] @@ -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 @@ -146,3 +146,7 @@ func ReadData(dataString string) (data.Data, error) { return nil, errorConstants.IncorrectFormat } + +func sanitizeDataString(dataString string) string { + return strings.ReplaceAll(dataString, " ", "") +} diff --git a/schema/data/utilities/string_test.go b/schema/data/utilities/string_test.go index 2a847a470..bac7ed688 100644 --- a/schema/data/utilities/string_test.go +++ b/schema/data/utilities/string_test.go @@ -36,12 +36,18 @@ func TestReadData(t *testing.T) { wantErr bool }{ // TODO: fix after issue #50 fix, https://github.com/AssetMantle/modules/issues/50 + {"+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 String Data with space", args{"S| newFact"}, base.NewStringData("newFact"), false}, + {"-ve String Data with space", args{" S| newFact"}, base.NewStringData("newFact"), false}, {"Unknown Data", args{"SomeRandomData"}, nil, true}, {"List Data", args{"L|A|cosmos1pkkayn066msg6kn33wnl5srhdt3tnu2vzasz9c,A|cosmos1x53dugvr4xvew442l9v2r5x7j8gfvged2zk5ef"}, base.NewListData(baseLists.NewDataList(dataList...)), false}, // TODO: stack overflow {"List Data empty list", args{"L|"}, base.NewListData(baseLists.NewDataList()), false}, // TODO: stack overflow {"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},