diff --git a/constraints/constraints.go b/constraints/constraints.go new file mode 100644 index 0000000..af4e3a9 --- /dev/null +++ b/constraints/constraints.go @@ -0,0 +1,5 @@ +package constraints + +type SignedInteger interface { + int | int8 | int16 | int32 | int64 +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cb0428f --- /dev/null +++ b/go.mod @@ -0,0 +1,19 @@ +module github.com/hurbcom/aide-go + +go 1.18 + +require ( + github.com/fatih/structs v1.1.0 + github.com/pkg/errors v0.9.1 + github.com/spf13/cast v1.4.1 + github.com/stretchr/testify v1.7.0 + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c + gopkg.in/h2non/gock.v1 v1.1.2 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..eb560c3 --- /dev/null +++ b/go.sum @@ -0,0 +1,28 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= +github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= +github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= +github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY= +gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/lib/main.go b/lib/main.go index 6300e1e..5c70102 100644 --- a/lib/main.go +++ b/lib/main.go @@ -16,49 +16,30 @@ import ( "time" "github.com/fatih/structs" + "github.com/hurbcom/aide-go/constraints" "github.com/spf13/cast" ) // ToStringSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ToStringSlice(intslice []int) (stringSlice []string) { - for _, i := range intslice { - stringSlice = append(stringSlice, strconv.FormatInt(int64(i), 10)) - } - return stringSlice -} - -// ToStringSlice64 REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ToStringSlice64(int64Slice []int64) (stringSlice []string) { - for _, i := range int64Slice { - stringSlice = append(stringSlice, strconv.FormatInt(i, 10)) +func IntSliceToStringSlice[T constraints.SignedInteger](intSlice []T) (stringSlice []string) { + for _, v := range intSlice { + stringSlice = append(stringSlice, strconv.FormatInt(int64(v), 10)) } return stringSlice } // ToIntSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ToIntSlice(stringSlice []string) (intSlice []int) { +func StringSliceToIntSlice[T constraints.SignedInteger](stringSlice []string) (intSlice []T) { for _, i := range stringSlice { - intI, err := ParseStringToInt(i) + v, err := ParseStringToInt[T](i) if err != nil { continue } - intSlice = append(intSlice, intI) + intSlice = append(intSlice, v) } return intSlice } -// ToInt64Slice REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ToInt64Slice(stringSlice []string) (int64Slice []int64) { - for _, i := range stringSlice { - intI, err := ParseStringToInt64(i) - if err != nil { - continue - } - int64Slice = append(int64Slice, intI) - } - return int64Slice -} - // StringToStringSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION func StringToStringSlice(s string) []string { stringSlice := []string{} @@ -86,39 +67,34 @@ func StringToStringSlice(s string) []string { } // StringToIntSlice REQUIRE THEM TO DOCUMENT THIS FUNCTION -func StringToIntSlice(s string) []int { +func StringToIntSlice[T constraints.SignedInteger](s string) []T { if len(s) == 0 { - return []int{} + return []T{} } sl := StringToStringSlice(s) if len(sl) == 0 { - return []int{} + return []T{} } - return ToIntSlice(sl) -} - -// ParseStringToInt REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ParseStringToInt(s string) (int, error) { - if s == "" { - return 0, nil - } - - return strconv.Atoi(s) + return StringSliceToIntSlice[T](sl) } // ParseStringToInt64 REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ParseStringToInt64(s string) (int64, error) { +func ParseStringToInt[T constraints.SignedInteger](s string) (T, error) { if s == "" { return 0, nil } - return strconv.ParseInt(s, 10, 0) + v, err := strconv.ParseInt(s, 10, 0) + if err != nil { + return 0, err + } + return T(v), nil } // ParseIntToBool REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ParseIntToBool(i int) bool { +func ParseIntToBool[T constraints.SignedInteger](i T) bool { if i == 1 { return true } @@ -295,12 +271,12 @@ func GetStringBodyHTTPResponseJSON(r *http.Response) *string { } // ParseIntOrReturnZero REQUIRE THEM TO DOCUMENT THIS FUNCTION -func ParseIntOrReturnZero(s string) int { +func ParseIntOrReturnZero[T constraints.SignedInteger](s string) T { integer, err := strconv.ParseInt(s, 10, 0) if err != nil { return 0 } - return int(integer) + return T(integer) } // Stringer REQUIRE THEM TO DOCUMENT THIS TYPE diff --git a/lib/main_test.go b/lib/main_test.go index 69be842..b26e7ef 100644 --- a/lib/main_test.go +++ b/lib/main_test.go @@ -148,7 +148,7 @@ func TestGetStringBodyHTTPResponseJSONEncoded(t *testing.T) { } func TestIntegerSliceToStringSlice(t *testing.T) { - strs := ToStringSlice([]int{1, 2, 3}) + strs := IntSliceToStringSlice([]int{1, 2, 3}) assert.Len(t, strs, 3) assert.Equal(t, "1", strs[0]) @@ -157,7 +157,7 @@ func TestIntegerSliceToStringSlice(t *testing.T) { } func TestInteger64SliceToStringSlice(t *testing.T) { - strs := ToStringSlice64([]int64{16, 23, 39}) + strs := IntSliceToStringSlice([]int64{16, 23, 39}) assert.Len(t, strs, 3) assert.Equal(t, "16", strs[0]) @@ -166,7 +166,7 @@ func TestInteger64SliceToStringSlice(t *testing.T) { } func TestToIntSlice(t *testing.T) { - actual := ToIntSlice([]string{"6549", "8523", "a"}) + actual := StringSliceToIntSlice[int]([]string{"6549", "8523", "a"}) assert.Len(t, actual, 2) assert.Equal(t, int(6549), actual[0]) @@ -174,7 +174,7 @@ func TestToIntSlice(t *testing.T) { } func TestToInt64Slice(t *testing.T) { - actual := ToInt64Slice([]string{"654987", "852369", "a"}) + actual := StringSliceToIntSlice[int64]([]string{"654987", "852369", "a"}) assert.Len(t, actual, 2) assert.Equal(t, int64(654987), actual[0]) @@ -202,13 +202,13 @@ func TestStringToStringSlice(t *testing.T) { } func TestStringToIntSlice(t *testing.T) { - actual := StringToIntSlice("[foo,123,bar,,456,a1b2,,,]") + actual := StringToIntSlice[int]("[foo,123,bar,,456,a1b2,,,]") assert.Len(t, actual, 2) assert.Equal(t, 123, actual[0]) assert.Equal(t, 456, actual[1]) - actual = StringToIntSlice("[[foo,123,[bar],,456,,a1b2]") + actual = StringToIntSlice[int]("[[foo,123,[bar],,456,,a1b2]") assert.Len(t, actual, 2) assert.Equal(t, 123, actual[0]) @@ -216,7 +216,7 @@ func TestStringToIntSlice(t *testing.T) { } func TestParseInt(t *testing.T) { - i, err := ParseStringToInt("6549") + i, err := ParseStringToInt[int]("6549") expected := int(6549) @@ -226,20 +226,20 @@ func TestParseInt(t *testing.T) { } func TestParseIntWithEmptyString(t *testing.T) { - i, err := ParseStringToInt("") + i, err := ParseStringToInt[int]("") assert.Equal(t, 0, i) assert.Empty(t, err) } func TestParseIntInvalidString(t *testing.T) { - _, err := ParseStringToInt("invalid") + _, err := ParseStringToInt[int]("invalid") assert.NotEmpty(t, err) } func TestParseInt64(t *testing.T) { - i, err := ParseStringToInt64("456123789123") + i, err := ParseStringToInt[int64]("456123789123") expected := int64(456123789123) @@ -249,14 +249,14 @@ func TestParseInt64(t *testing.T) { } func TestParseInt64WithEmptyString(t *testing.T) { - i, err := ParseStringToInt64("") + i, err := ParseStringToInt[int64]("") assert.Equal(t, int64(0), i) assert.Empty(t, err) } func TestParseInt64InvalidString(t *testing.T) { - _, err := ParseStringToInt64("invalid") + _, err := ParseStringToInt[int64]("invalid") assert.NotEmpty(t, err) } @@ -364,21 +364,21 @@ func TestParseIntOrReturnZero(t *testing.T) { stg := "1" expected := 1 - assert.Equal(t, expected, ParseIntOrReturnZero(stg)) + assert.Equal(t, expected, ParseIntOrReturnZero[int](stg)) } func TestParseIntOrReturnZeroFail(t *testing.T) { stg := "a" expected := 0 - assert.Equal(t, expected, ParseIntOrReturnZero(stg)) + assert.Equal(t, expected, ParseIntOrReturnZero[int](stg)) } func TestParseIntOrReturnZeroWithNumberOnString(t *testing.T) { stg := "a123" expected := 0 - assert.Equal(t, expected, ParseIntOrReturnZero(stg)) + assert.Equal(t, expected, ParseIntOrReturnZero[int](stg)) } func TestIsArray(t *testing.T) {