Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Jun 13, 2024
1 parent 94434ec commit faf9d6c
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions clients/bigquery/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,21 @@ func castColVal(colVal any, colKind columns.Column, additionalDateFmts []string)
return fmt.Sprint(colVal), nil
}

// EncodeStructToJSONString takes a struct as either a string or Go object and encodes it into a JSON string.
// Structs from relational and Mongo are different.
// MongoDB will return the native objects back such as `map[string]any{"hello": "world"}`
// Relational will return a string representation of the struct such as `{"hello": "world"}`
func EncodeStructToJSONString(value any) (string, error) {
if strings.Contains(fmt.Sprint(value), constants.ToastUnavailableValuePlaceholder) {
return fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder), nil
}

// Structs from relational and Mongo are different.
// MongoDB will return the native objects back such as `map[string]any{"hello": "world"}`
// Relational will return a string representation of the struct such as `{"hello": "world"}`
if colValString, isOk := value.(string); isOk {
if colValString == "" {
return "", nil
if strings.Contains(colValString, constants.ToastUnavailableValuePlaceholder) {
return fmt.Sprintf(`{"key":"%s"}`, constants.ToastUnavailableValuePlaceholder), nil
}

return colValString, nil
}

colValBytes, err := json.Marshal(value)
if err != nil {
return "", fmt.Errorf("failed to marshal colVal: %w", err)
}

return string(colValBytes), nil
}

0 comments on commit faf9d6c

Please sign in to comment.