FormatString returns the string representation of a CEL value. It is used to implement the %s specifier in the (string).format() extension function.
(arg ref.Val, locale string)
| 208 | // |
| 209 | // It is used to implement the %s specifier in the (string).format() extension function. |
| 210 | func FormatString(arg ref.Val, locale string) (string, error) { |
| 211 | switch arg.Type() { |
| 212 | case types.ListType: |
| 213 | return formatList(arg, locale) |
| 214 | case types.MapType: |
| 215 | return formatMap(arg, locale) |
| 216 | case types.IntType, types.UintType, types.DoubleType, |
| 217 | types.BoolType, types.StringType, types.TimestampType, types.BytesType, types.DurationType, types.TypeType: |
| 218 | argStrVal := arg.ConvertToType(types.StringType) |
| 219 | argStr, ok := argStrVal.Value().(string) |
| 220 | if !ok { |
| 221 | return "", fmt.Errorf("could not convert argument %q to string", argStrVal) |
| 222 | } |
| 223 | return argStr, nil |
| 224 | case types.NullType: |
| 225 | return "null", nil |
| 226 | default: |
| 227 | return "", stringFormatError(runtimeID, arg.Type().TypeName()) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | func formatDecimal(arg ref.Val, locale string) (string, error) { |
| 232 | switch arg.Type() { |
no test coverage detected