From 66ae321f0b2f1979774a53d95bd380334697f179 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Wed, 23 Oct 2024 11:36:17 -0700 Subject: [PATCH] Clean up. --- clients/bigquery/converters/converters.go | 18 +++++++++++++++--- clients/bigquery/storagewrite.go | 2 +- lib/typing/ext/time.go | 5 ----- lib/typing/typing.go | 4 ++++ 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/clients/bigquery/converters/converters.go b/clients/bigquery/converters/converters.go index e071e49c7..efe9952e5 100644 --- a/clients/bigquery/converters/converters.go +++ b/clients/bigquery/converters/converters.go @@ -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 @@ -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) } diff --git a/clients/bigquery/storagewrite.go b/clients/bigquery/storagewrite.go index 63605a827..d6933eb98 100644 --- a/clients/bigquery/storagewrite.go +++ b/clients/bigquery/storagewrite.go @@ -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 } diff --git a/lib/typing/ext/time.go b/lib/typing/ext/time.go index d84c2ea5b..93243eb41 100644 --- a/lib/typing/ext/time.go +++ b/lib/typing/ext/time.go @@ -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) -} diff --git a/lib/typing/typing.go b/lib/typing/typing.go index 1441ddaf3..38c71af5d 100644 --- a/lib/typing/typing.go +++ b/lib/typing/typing.go @@ -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 }