Skip to content

Commit

Permalink
Cleanups and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Mar 13, 2024
1 parent 10ff76d commit 8f9173c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
22 changes: 0 additions & 22 deletions builq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package builq

import (
"errors"
"reflect"
"strings"
)

Expand Down Expand Up @@ -134,21 +133,6 @@ func (b *Builder) build() (_ string, _ []any) {
return q, resArgs
}

func (b *Builder) asSlice(v any) []any {
value := reflect.ValueOf(v)

if value.Kind() != reflect.Slice {
b.setErr(errNonSliceArgument)
return nil
}

res := make([]any, value.Len())
for i := 0; i < value.Len(); i++ {
res[i] = value.Index(i).Interface()
}
return res
}

var (
// errTooFewArguments passed to [Builder.Addf] method.
errTooFewArguments = errors.New("too few arguments")
Expand All @@ -171,9 +155,3 @@ var (
// errNonNumericArg expected number for %d but got something else.
errNonNumericArg = errors.New("expected numeric argument")
)

func (b *Builder) setErr(err error) {
if b.err == nil {
b.err = err
}
}
11 changes: 11 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ func ExampleNew() {
panic(err)
}

debug := q.DebugBuild()

fmt.Println("query:")
fmt.Println(query)
fmt.Println("args:")
fmt.Println(args)
fmt.Println("debug:")
fmt.Println(debug)

// Output:
//
Expand All @@ -55,6 +59,10 @@ func ExampleNew() {
//
// args:
// [42 root]
// debug:
// SELECT foo, bar FROM 'users'
// WHERE active IS TRUE
// AND user_id = 42 OR user = 'root'
}

func ExampleBuilder() {
Expand Down Expand Up @@ -113,12 +121,14 @@ func ExampleBuilder_DebugBuild() {
cols := builq.Columns{"foo", "bar"}

ts := time.Date(2009, time.November, 10, 12, 13, 15, 16, time.UTC)
d := 4 * time.Second

var sb builq.Builder
sb.Addf("SELECT %s FROM table", cols)
sb.Addf("WHERE id = %$", 123)
sb.Addf("OR id = %$ + %d", "42", 69.069)
sb.Addf("XOR created_at = %$", ts)
sb.Addf("MORE offset = %$", d)
sb.Addf("MAYBE IN arr = %$", []int{1, 2, 3})

fmt.Printf("debug:\n%v", sb.DebugBuild())
Expand All @@ -129,6 +139,7 @@ func ExampleBuilder_DebugBuild() {
// WHERE id = 123
// OR id = '42' + 69.069
// XOR created_at = '2009-11-10 12:13:15:999999'
// MORE offset = '4s'
// MAYBE IN arr = '[1 2 3]'
}

Expand Down
22 changes: 22 additions & 0 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package builq

import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -155,6 +156,27 @@ func (b *Builder) writeDebug(sb *strings.Builder, arg any) {
}
}

func (b *Builder) asSlice(v any) []any {
value := reflect.ValueOf(v)

if value.Kind() != reflect.Slice {
b.setErr(errNonSliceArgument)
return nil
}

res := make([]any, value.Len())
for i := 0; i < value.Len(); i++ {
res[i] = value.Index(i).Interface()
}
return res
}

func (b *Builder) setErr(err error) {
if b.err == nil {
b.err = err
}
}

func (b *Builder) assertNumber(v any) {
switch v.(type) {
case int, int8, int16, int32, int64,
Expand Down

0 comments on commit 8f9173c

Please sign in to comment.