Skip to content

Commit

Permalink
Split ToApiString() / ToString()
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaibbq committed Jan 23, 2024
1 parent 4c3b3f5 commit 2c8592a
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 49 deletions.
20 changes: 0 additions & 20 deletions internal/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,18 +379,6 @@ func CastValue(t types.Type, v Value) (Value, error) {
}
return FloatValue(f64), nil
case types.STRING:
switch v.(type) {
// If this is coming from a date/time, the format is slightly different
// than when just writing the value out as a string.
case DateValue:
return convertTimeValueToStringWithFormat(v, "2006-01-02")
case DatetimeValue:
return convertTimeValueToStringWithFormat(v, "2006-01-02 15:04:05.999999")
case TimeValue:
return convertTimeValueToStringWithFormat(v, "15:04:05.999999")
case TimestampValue:
return convertTimeValueToStringWithFormat(v, "2006-01-02 15:04:05.999999-07")
}
s, err := v.ToString()
if err != nil {
return nil, err
Expand Down Expand Up @@ -525,14 +513,6 @@ func CastValue(t types.Type, v Value) (Value, error) {
return nil, fmt.Errorf("unsupported cast %s value", t.Kind())
}

func convertTimeValueToStringWithFormat(v Value, format string) (Value, error) {
valueTime, err := v.ToTime()
if err != nil {
return nil, err
}
return StringValue(valueTime.UTC().Format(format)), nil
}

func ValueFromGoValue(v interface{}) (Value, error) {
if isNullValue(v) {
return nil, nil
Expand Down
33 changes: 14 additions & 19 deletions internal/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"io"
"reflect"
"time"

"github.com/goccy/go-json"
"github.com/goccy/go-zetasql/types"
"io"
"reflect"
)

type Rows struct {
Expand Down Expand Up @@ -232,68 +230,65 @@ func (r *Rows) assignInterfaceValue(src Value, dst reflect.Value, typ *Type) err
}
dst.Set(reflect.ValueOf(f64))
case types.BYTES:
s, err := src.ToString()
s, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
case types.STRING:
s, err := src.ToString()
s, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
case types.NUMERIC:
s, err := src.ToString()
s, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
case types.BIG_NUMERIC:
s, err := src.ToString()
s, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
case types.DATE:
date, err := src.ToJSON()
date, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(date))
case types.DATETIME:
datetime, err := src.ToJSON()
datetime, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(datetime))
case types.TIME:
time, err := src.ToJSON()
time, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(time))
case types.TIMESTAMP:
t, err := src.ToTime()
t, err := src.ToApiString()
if err != nil {
return err
}
unixmicro := t.UnixMicro()
sec := unixmicro / int64(time.Millisecond)
nsec := unixmicro - sec*int64(time.Millisecond)
dst.Set(reflect.ValueOf(fmt.Sprintf("%d.%d", sec, nsec)))
dst.Set(reflect.ValueOf(t))
case types.INTERVAL:
s, err := src.ToString()
s, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(s))
case types.JSON:
json, err := src.ToJSON()
j, err := src.ToApiString()
if err != nil {
return err
}
dst.Set(reflect.ValueOf(json))
dst.Set(reflect.ValueOf(j))
case types.STRUCT:
s, err := src.ToStruct()
if err != nil {
Expand Down
Loading

0 comments on commit 2c8592a

Please sign in to comment.