Skip to content

Commit

Permalink
Support bool and number values when not passed as strings (#59)
Browse files Browse the repository at this point in the history
Fixes a bug where boolean and numeric values had to be passed to the
importer as strings eg. `my_boolean_field: 'true'` rather than the type
directly eg. `my_boolean_field: true`
  • Loading branch information
kelseymills authored Aug 21, 2023
2 parents d76cabd + a79e09d commit d56f5cb
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion output/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ func MarshalEntries(ctx context.Context, output *Output, entries []source.Entry)

binding.ArrayValue = &arrayValue
} else {
literal, err := expr.Eval[string](ctx, prg, entry)
literal, err := evaluateEntryWithAttributeType(ctx, prg, entry, attributeByID[attributeID])

if err != nil {
continue eachAttribute
}
Expand All @@ -232,3 +233,44 @@ func MarshalEntries(ctx context.Context, output *Output, entries []source.Entry)

return catalogEntryModels, nil
}

func evaluateEntryWithAttributeType(ctx context.Context, prg cel.Program, entry map[string]any, attribute *Attribute) (string, error) {
var literal string

// If we have an attribute type of type Bool or Number, we can try to evaluate the program against the scope
// with the appropriate type.
// If that fails, we'll fall back to a string literal since we accept passing a boolean or numeric value
// as a string literal.
if attribute != nil && attribute.Type.Valid {
switch attribute.Type.String {
case "Bool":
literal, _ = evaluateEntryWithType[bool](ctx, prg, entry)
if literal != "" {
return literal, nil
}
case "Number":
// Number accepts float or int, so we'll try to evaluate as a float first.
literal, _ = evaluateEntryWithType[float64](ctx, prg, entry)
if literal != "" {
return literal, nil
}
literal, _ = evaluateEntryWithType[int64](ctx, prg, entry)
if literal != "" {
return literal, nil
}
}
}

// If we have an attribute type of type String, or we failed to evaluate the program against the scope
// with the appropriate type, we'll try to evaluate as a string literal.
return evaluateEntryWithType[string](ctx, prg, entry)
}

func evaluateEntryWithType[ReturnType any](ctx context.Context, prg cel.Program, entry map[string]any) (string, error) {
literal, err := expr.Eval[ReturnType](ctx, prg, entry)
if err != nil {
return "", err
}

return fmt.Sprintf("%v", literal), nil
}

0 comments on commit d56f5cb

Please sign in to comment.