Skip to content

Commit

Permalink
[postgres] Reorder ParseValue cases (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Mar 11, 2024
1 parent fd5e47c commit ded87b8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
69 changes: 34 additions & 35 deletions lib/postgres/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,20 @@ func ParseValue(colKind schema.DataType, value any) (any, error) {
}

switch colKind {
case schema.Geometry, schema.Geography:
valString, isOk := value.(string)
if !isOk {
return nil, fmt.Errorf("value: %v not of string type for geometry / geography", value)
}

geometry, err := ToGeography([]byte(valString))
if err != nil {
return nil, fmt.Errorf("failed to parse geometry / geography: %w", err)
}

return geometry, nil
case schema.Point:
valString, isOk := value.(string)
if !isOk {
return nil, fmt.Errorf("value: %v not of string type for POINT", value)
}

point, err := ToPoint(valString)
if err != nil {
return nil, fmt.Errorf("failed to parse POINT: %w", err)
}

return point.ToMap(), nil

case schema.Bit:
// This will be 0 (false) or 1 (true)
valString, isOk := value.(string)
if isOk {
return valString == "1", nil
}
return nil, fmt.Errorf("value: %v not of string type for bit", value)
case schema.JSON:
// Debezium sends JSON as a JSON string
byteSlice, isByteSlice := value.([]byte)
if !isByteSlice {
return nil, fmt.Errorf("value: %v not of []byte type for JSON", value)
case schema.UserDefinedText:
stringSlice, isOk := value.(string)
if !isOk {
return nil, fmt.Errorf("value: %v not of slice type", value)
}

return string(byteSlice), nil
return stringSlice, nil
case schema.Numeric, schema.VariableNumeric:
stringVal, isStringVal := value.(string)
if isStringVal {
Expand Down Expand Up @@ -101,6 +75,14 @@ func ParseValue(colKind schema.DataType, value any) (any, error) {
}

return _uuid.String(), nil
case schema.JSON:
// Debezium sends JSON as a JSON string
byteSlice, isByteSlice := value.([]byte)
if !isByteSlice {
return nil, fmt.Errorf("value: %v not of []byte type for JSON", value)
}

return string(byteSlice), nil
case schema.HStore:
var val pgtype.Hstore
err := val.Scan(value)
Expand All @@ -116,13 +98,30 @@ func ParseValue(colKind schema.DataType, value any) (any, error) {
}

return jsonMap, nil
case schema.UserDefinedText:
stringSlice, isOk := value.(string)
case schema.Point:
valString, isOk := value.(string)
if !isOk {
return nil, fmt.Errorf("value: %v not of slice type", value)
return nil, fmt.Errorf("value: %v not of string type for POINT", value)
}

return stringSlice, nil
point, err := ToPoint(valString)
if err != nil {
return nil, fmt.Errorf("failed to parse POINT: %w", err)
}

return point.ToMap(), nil
case schema.Geometry, schema.Geography:
valString, isOk := value.(string)
if !isOk {
return nil, fmt.Errorf("value: %v not of string type for geometry / geography", value)
}

geometry, err := ToGeography([]byte(valString))
if err != nil {
return nil, fmt.Errorf("failed to parse geometry / geography: %w", err)
}

return geometry, nil
default:
return value, nil
}
Expand Down
2 changes: 1 addition & 1 deletion lib/postgres/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
Bytea
Inet
Text
UserDefinedText
Interval
Array
HStore
Expand All @@ -31,7 +32,6 @@ const (
Int32
Int64
UUID
UserDefinedText
JSON
Timestamp
Time
Expand Down

0 comments on commit ded87b8

Please sign in to comment.