Skip to content

Commit

Permalink
fix(gen): adding missing base types to the patch gen method (#90)
Browse files Browse the repository at this point in the history
* adding missing base types to the patch gen method

* Adding test to cover new changes
  • Loading branch information
Jacobbrewer1 authored Jan 18, 2025
1 parent 57d4790 commit 891b43f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
8 changes: 6 additions & 2 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func (s *SQLPatch) patchGen(resource any) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s.args = append(s.args, val.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
s.args = append(s.args, val.Uint())
case reflect.Float32, reflect.Float64:
s.args = append(s.args, val.Float())
case reflect.Complex64, reflect.Complex128:
s.args = append(s.args, val.Complex())
case reflect.String:
s.args = append(s.args, val.String())
case reflect.Bool:
Expand All @@ -118,8 +124,6 @@ func (s *SQLPatch) patchGen(resource any) {
boolArg = 1
}
s.args = append(s.args, boolArg)
case reflect.Float32, reflect.Float64:
s.args = append(s.args, val.Float())
default:
// This is intentionally a panic as this is a programming error and should be fixed by the developer
panic(fmt.Sprintf("unsupported type: %s", val.Kind()))
Expand Down
58 changes: 58 additions & 0 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,64 @@ func (s *newSQLPatchSuite) TestNewSQLPatch_Success() {
s.Equal([]any{int64(1), "test"}, patch.args)
}

func (s *newSQLPatchSuite) TestPatchGen_AllTypes() {
type testObj struct {
IntVal int
Int8Val int8
Int16Val int16
Int32Val int32
Int64Val int64
UintVal uint
Uint8Val uint8
Uint16Val uint16
Uint32Val uint32
Uint64Val uint64
UintptrVal uintptr
Float32Val float32
Float64Val float64
Complex64Val complex64
Complex128Val complex128
StringVal string
BoolVal bool
}

obj := testObj{
IntVal: 1,
Int8Val: 2,
Int16Val: 3,
Int32Val: 4,
Int64Val: 5,
UintVal: 6,
Uint8Val: 7,
Uint16Val: 8,
Uint32Val: 9,
Uint64Val: 10,
UintptrVal: 11,
Float32Val: 12.34,
Float64Val: 56.78,
Complex64Val: complex(1, 2),
Complex128Val: complex(3, 4),
StringVal: "test",
BoolVal: true,
}

patch := NewSQLPatch(obj)

expectedFields := []string{
"IntVal = ?", "Int8Val = ?", "Int16Val = ?", "Int32Val = ?", "Int64Val = ?",
"UintVal = ?", "Uint8Val = ?", "Uint16Val = ?", "Uint32Val = ?", "Uint64Val = ?", "UintptrVal = ?",
"Float32Val = ?", "Float64Val = ?", "Complex64Val = ?", "Complex128Val = ?", "StringVal = ?", "BoolVal = ?",
}
expectedArgs := []any{
int64(1), int64(2), int64(3), int64(4), int64(5),
uint64(6), uint64(7), uint64(8), uint64(9), uint64(10), uint64(11),
12.34000015258789, 56.78, complex(1, 2), complex(3, 4), "test", 1,
}

s.Equal(expectedFields, patch.fields)
s.Equal(expectedArgs, patch.args)
}

func (s *newSQLPatchSuite) TestNewSQLPatch_Success_MultipleTags() {
type testObj struct {
Id *int `db:"id_tag,pk"`
Expand Down

0 comments on commit 891b43f

Please sign in to comment.