Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 23, 2024
1 parent 97f05e9 commit 66ae321
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
18 changes: 15 additions & 3 deletions clients/bigquery/converters/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"fmt"
"strconv"

"github.com/artie-labs/transfer/lib/typing"

"github.com/artie-labs/transfer/lib/typing/decimal"
"github.com/artie-labs/transfer/lib/typing/ext"
)

type StringConverter struct{}
type StringConverter struct {
kd typing.KindDetails
}

func NewStringConverter(kd typing.KindDetails) StringConverter {
return StringConverter{kd: kd}
}

func (StringConverter) Convert(value any) (any, error) {
func (s StringConverter) Convert(value any) (any, error) {
switch castedValue := value.(type) {
case string:
return castedValue, nil
Expand All @@ -19,7 +27,11 @@ func (StringConverter) Convert(value any) (any, error) {
case bool:
return fmt.Sprint(castedValue), nil
case *ext.ExtendedTime:
return castedValue.String(""), nil
if err := s.kd.EnsureExtendedTimeDetails(); err != nil {
return nil, err
}

return castedValue.GetTime().Format(s.kd.ExtendedTimeDetails.Format), nil
default:
return nil, fmt.Errorf("expected string/*decimal.Decimal/bool received %T with value %v", value, value)
}
Expand Down
2 changes: 1 addition & 1 deletion clients/bigquery/storagewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func rowToMessage(row map[string]any, columns []columns.Column, messageDescripto

message.Set(field, protoreflect.ValueOfString(decimalValue.String()))
case typing.String.Kind:
val, err := converters.StringConverter{}.Convert(value)
val, err := converters.NewStringConverter(column.KindDetails).Convert(value)
if err != nil {
return nil, err
}
Expand Down
5 changes: 0 additions & 5 deletions lib/typing/ext/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,3 @@ func (e *ExtendedTime) GetTime() time.Time {
func (e *ExtendedTime) GetNestedKind() NestedKind {
return e.nestedKind
}

func (e *ExtendedTime) String(overrideFormat string) string {
format := cmp.Or(overrideFormat, e.nestedKind.Format)
return e.ts.Format(format)
}
4 changes: 4 additions & 0 deletions lib/typing/typing.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (k *KindDetails) EnsureExtendedTimeDetails() error {
return fmt.Errorf("extended time details is not set")
}

if k.ExtendedTimeDetails.Format == "" {
return fmt.Errorf("extended time details format is not set")
}

return nil
}

Expand Down

0 comments on commit 66ae321

Please sign in to comment.