Skip to content

Commit

Permalink
Rework error messages, see #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Dec 20, 2023
1 parent 7bbd4f1 commit 0bb1cd5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ext/array/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"

"github.com/ncruces/go-sqlite3"
"github.com/ncruces/go-sqlite3/internal/util"
)

// Register registers the array single-argument, table-valued SQL function.
Expand Down Expand Up @@ -102,7 +103,7 @@ func (c *cursor) Column(ctx *sqlite3.Context, n int) error {
ctx.ResultBlob(v.Bytes())

default:
return fmt.Errorf("array: unsupported element:%.0w %v", sqlite3.MISMATCH, v.Type())
return fmt.Errorf("array: unsupported element:%.0w %v", sqlite3.MISMATCH, util.ReflectType(v))
}
return nil
}
Expand All @@ -120,16 +121,15 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
}

func indexable(v reflect.Value) (reflect.Value, error) {
if v.Kind() == reflect.Slice {
switch v.Kind() {
case reflect.Slice:
return v, nil
}
if v.Kind() == reflect.Array {
case reflect.Array:
return v, nil
}
if v.Kind() == reflect.Pointer {
case reflect.Pointer:
if v := v.Elem(); v.Kind() == reflect.Array {
return v, nil
}
}
return v, fmt.Errorf("array: unsupported argument:%.0w %v", sqlite3.MISMATCH, v)
return v, fmt.Errorf("array: unsupported argument:%.0w %v", sqlite3.MISMATCH, util.ReflectType(v))
}
10 changes: 10 additions & 0 deletions internal/util/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package util

import "reflect"

func ReflectType(v reflect.Value) reflect.Type {
if v.Kind() != reflect.Invalid {
return v.Type()
}
return nil
}
21 changes: 21 additions & 0 deletions internal/util/reflect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import (
"fmt"
"math"
"reflect"
"testing"
)

func TestReflectType(t *testing.T) {
tests := []any{nil, 1, math.Pi, "abc"}
for _, tt := range tests {
t.Run(fmt.Sprint(tt), func(t *testing.T) {
want := fmt.Sprintf("%T", tt)
got := fmt.Sprintf("%v", ReflectType(reflect.ValueOf(tt)))
if got != want {
t.Errorf("ReflectType() = %v, want %v", got, want)
}
})
}
}

0 comments on commit 0bb1cd5

Please sign in to comment.