Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed May 1, 2024
1 parent 05c6df2 commit e678dec
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions clients/bigquery/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e678dec

Please sign in to comment.