Skip to content

Commit

Permalink
Merge pull request #4 from cipherstash/fix/add-types-to-convert-to-st…
Browse files Browse the repository at this point in the history
…ring

Add types to convert to string
  • Loading branch information
fimac authored Oct 31, 2024
2 parents 2e6e71b + 422db1e commit 7f1ffc5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
24 changes: 22 additions & 2 deletions goeql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ package goeql
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)

// TableColumn represents the table and column an encrypted value belongs to
Expand Down Expand Up @@ -240,12 +242,30 @@ func ToEncryptedColumn(value any, table string, column string, queryType any) (E
}

func convertToString(value any) (string, error) {
// Check for slice types
val := reflect.ValueOf(value)
// reflect.Slice will return true if it is a slice
if val.Kind() == reflect.Slice {
strSlice := make([]string, val.Len())
for i := 0; i < val.Len(); i++ {
elem, err := convertToString(val.Index(i).Interface())
if err != nil {
return "", err
}
strSlice[i] = elem
}
return "[" + strings.Join(strSlice, ", ") + "]", nil
}
switch v := value.(type) {
case fmt.Stringer:
return v.String(), nil
case string:
return v, nil
case int:
case int, int8, int16, int32, int64:
return fmt.Sprintf("%d", v), nil
case uint, uint8, uint16, uint32, uint64, uintptr:
return fmt.Sprintf("%d", v), nil
case float64:
case float32, float64:
return fmt.Sprintf("%f", v), nil
case map[string]any:
jsonData, err := json.Marshal(v)
Expand Down
29 changes: 27 additions & 2 deletions goeql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ func TestToEncryptedColumn(t *testing.T) {

// Test convertToString Function
func TestConvertToString(t *testing.T) {
// the ptr value for unitptr will change between tests
// set a mock value to use to confirm the convertToString function works on this uintptr.
var mockPtr uintptr = 1374390189136
tests := []struct {
value interface{}
expectedStr string
Expand All @@ -414,9 +417,31 @@ func TestConvertToString(t *testing.T) {
{value: "test_string", expectedStr: "test_string", expectError: false},
{value: 123, expectedStr: "123", expectError: false},
{value: 123.456, expectedStr: "123.456000", expectError: false},
{value: int(1), expectedStr: "1", expectError: false},
{value: int8(-128), expectedStr: "-128", expectError: false},
{value: int8(127), expectedStr: "127", expectError: false},
{value: int16(-32768), expectedStr: "-32768", expectError: false},
{value: int16(32767), expectedStr: "32767", expectError: false},
{value: int32(-2147483648), expectedStr: "-2147483648", expectError: false},
{value: int32(2147483647), expectedStr: "2147483647", expectError: false},
{value: int64(-9223372036854775808), expectedStr: "-9223372036854775808", expectError: false},
{value: int64(9223372036854775807), expectedStr: "9223372036854775807", expectError: false},
{value: uint(1), expectedStr: "1", expectError: false},
{value: uint8(255), expectedStr: "255", expectError: false},
{value: uint16(65535), expectedStr: "65535", expectError: false},
{value: uint32(4294967295), expectedStr: "4294967295", expectError: false},
{value: uint64(18446744073709551615), expectedStr: "18446744073709551615", expectError: false},
{value: float32(123.456), expectedStr: "123.456001", expectError: false},
{value: float32(-10.543), expectedStr: "-10.543000", expectError: false},
{value: float64(3.1425), expectedStr: "3.142500", expectError: false},
{value: float64(-2.7182), expectedStr: "-2.718200", expectError: false},
{value: mockPtr, expectedStr: "1374390189136", expectError: false}, //uinttpr type
{value: true, expectedStr: "true", expectError: false},
{value: map[string]interface{}{"key": "value"}, expectedStr: `{"key":"value"}`, expectError: false},
{value: []int{1, 2, 3}, expectedStr: "", expectError: true}, // Unsupported type
{value: []int{1, 2, 3}, expectedStr: "[1, 2, 3]", expectError: false},
{value: []float64{1.1, 2.2, 3.3}, expectedStr: "[1.100000, 2.200000, 3.300000]", expectError: false},
{value: []string{"hello", "world"}, expectedStr: "[hello, world]", expectError: false},
{value: []bool{true, false, true}, expectedStr: "[true, false, true]", expectError: false},
}

for _, tt := range tests {
Expand All @@ -429,7 +454,7 @@ func TestConvertToString(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error for value: %v, error: %v", tt.value, err)
} else if str != tt.expectedStr {
t.Errorf("Expected '%s', got '%s' for value: %v", tt.expectedStr, str, tt.value)
t.Errorf("Expected '%s', got '%s' for value: %v for type: '%s'", tt.expectedStr, str, tt.value, reflect.TypeOf(tt.value))
}
}
}
Expand Down

0 comments on commit 7f1ffc5

Please # to comment.