From e678dec29e1b1c05a18675fde5a6c38d324f464a Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Wed, 1 May 2024 14:39:05 -0700 Subject: [PATCH] Refactor. --- clients/bigquery/cast.go | 102 +++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/clients/bigquery/cast.go b/clients/bigquery/cast.go index 22cd085c9..9c3454a12 100644 --- a/clients/bigquery/cast.go +++ b/clients/bigquery/cast.go @@ -16,69 +16,69 @@ import ( ) func castColVal(colVal any, colKind columns.Column, additionalDateFmts []string) (any, error) { - if colVal != nil { - switch colKind.KindDetails.Kind { - case typing.EDecimal.Kind: - val, isOk := colVal.(*decimal.Decimal) - if !isOk { - return nil, fmt.Errorf("colVal is not type *decimal.Decimal") - } + if colVal == nil { + return nil, nil + } - return val.Value(), nil - case typing.ETime.Kind: - extTime, err := ext.ParseFromInterface(colVal, additionalDateFmts) - if err != nil { - return nil, fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %w", colVal, err) - } + switch colKind.KindDetails.Kind { + case typing.EDecimal.Kind: + val, isOk := colVal.(*decimal.Decimal) + if !isOk { + return nil, fmt.Errorf("colVal is not type *decimal.Decimal") + } - if colKind.KindDetails.ExtendedTimeDetails == nil { - return nil, fmt.Errorf("column kind details for extended time details is null") - } + return val.Value(), nil + case typing.ETime.Kind: + extTime, err := ext.ParseFromInterface(colVal, additionalDateFmts) + if err != nil { + return nil, fmt.Errorf("failed to cast colVal as time.Time, colVal: %v, err: %w", colVal, err) + } - // We should be using the colKind here since the data types coming from the source may be inconsistent. - switch colKind.KindDetails.ExtendedTimeDetails.Type { - // https://cloud.google.com/bigquery/docs/streaming-data-into-bigquery#sending_datetime_data - case ext.DateTimeKindType: - if extTime.Year() == 0 { - return nil, nil - } - - colVal = extTime.StringUTC(ext.BigQueryDateTimeFormat) - case ext.DateKindType: - if extTime.Year() == 0 { - return nil, nil - } - - colVal = extTime.String(ext.PostgresDateFormat) - case ext.TimeKindType: - colVal = extTime.String(typing.StreamingTimeFormat) - } - case typing.Struct.Kind: - if strings.Contains(fmt.Sprint(colVal), constants.ToastUnavailableValuePlaceholder) { - colVal = fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder) - } + if colKind.KindDetails.ExtendedTimeDetails == nil { + return nil, fmt.Errorf("column kind details for extended time details is null") + } - colValString, isOk := colVal.(string) - if isOk && colValString == "" { - // Empty string is not a valid JSON object, so let's return nil. + // We should be using the colKind here since the data types coming from the source may be inconsistent. + switch colKind.KindDetails.ExtendedTimeDetails.Type { + // https://cloud.google.com/bigquery/docs/streaming-data-into-bigquery#sending_datetime_data + case ext.DateTimeKindType: + if extTime.Year() == 0 { return nil, nil } - case typing.Array.Kind: - var err error - arrayString, err := array.InterfaceToArrayString(colVal, true) - if err != nil { - return nil, err - } - if len(arrayString) == 0 { + return extTime.StringUTC(ext.BigQueryDateTimeFormat), nil + case ext.DateKindType: + if extTime.Year() == 0 { return nil, nil } - return arrayString, nil + return extTime.String(ext.PostgresDateFormat), nil + case ext.TimeKindType: + return extTime.String(typing.StreamingTimeFormat), nil + } + case typing.Struct.Kind: + if strings.Contains(fmt.Sprint(colVal), constants.ToastUnavailableValuePlaceholder) { + return fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder), nil + } + + colValString, isOk := colVal.(string) + if isOk && colValString == "" { + // Empty string is not a valid JSON object, so let's return nil. + return nil, nil + } + case typing.Array.Kind: + var err error + arrayString, err := array.InterfaceToArrayString(colVal, true) + if err != nil { + return nil, err + } + + if len(arrayString) == 0 { + return nil, nil } - return fmt.Sprint(colVal), nil + return arrayString, nil } - return nil, nil + return fmt.Sprint(colVal), nil }