Skip to content

Commit

Permalink
testutils/db: handle col type bool on export
Browse files Browse the repository at this point in the history
Since we are retrieving any type from the db rows.Scan we should handle
the possible returned types for a bool col type. Currently handle
booleans, int64 0 or 1, uint8[] of chars making a string of value "true"
or "false".
  • Loading branch information
sgotti committed Oct 11, 2023
1 parent 1326c5d commit af30a55
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/testutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,24 @@ func (c *DBContext) Export(ctx context.Context, tables []string, w io.Writer) er
return errors.WithStack(err)
}
switch colType {
case ColTypeBool:
switch vt := v.(type) {
case bool:
data.Values[col] = vt
case int64:
if vt != 0 && vt != 1 {
return errors.Errorf("unknown type int64 value %d for bool column type", vt)
}
data.Values[col] = vt != 0
case []uint8:
bv, err := strconv.ParseBool(string(vt))
if err != nil {
return errors.WithStack(err)
}
data.Values[col] = bv
default:
return errors.Errorf("unknown type %T for bool column type", v)
}
case ColTypeJSON:
var vj any
if err := json.Unmarshal(v.([]byte), &vj); err != nil {
Expand Down

0 comments on commit af30a55

Please sign in to comment.