Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot get value of pointer value #27

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package query

import (
"fmt"
"reflect"
"strings"

"github.com/Moranilt/http-utils/validators"
Expand Down Expand Up @@ -238,6 +239,16 @@ func buildComparison(fieldName string, value any, comparison string) string {
if isEmpty(fieldName) {
panic("fieldName cannot be empty")
}
v := reflect.ValueOf(value)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
value = nil
} else if v.IsValid() {
value = v.Elem().Interface()
} else {
panic("not supported type. Supports only numbers, string, bool, nil")
}
}
switch value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%s %s %d", fieldName, comparison, value)
Expand All @@ -253,7 +264,7 @@ func buildComparison(fieldName string, value any, comparison string) string {
case nil:
return fmt.Sprintf("%s %s NULL", fieldName, comparison)
default:
panic("not supported type. Supports only numbers, string, bool")
panic("not supported type. Supports only numbers, string, bool, nil")
}
}

Expand Down
10 changes: 10 additions & 0 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ var tests = []testItem{
},
expected: "SELECT * FROM users WHERE name IS NULL",
},
{
name: "using IS NULL where name is pointer string",
callback: func(t *testing.T) string {
var name *string
query := New("SELECT * FROM users")
query.Where().IS("name", name)
return query.String()
},
expected: "SELECT * FROM users WHERE name IS NULL",
},
{
name: "using IS NULL and OR",
callback: func(t *testing.T) string {
Expand Down
Loading