chore(common.starlark): Print nonconvertible data (#15508)

This commit is contained in:
Sven Rebhan 2024-06-28 11:07:32 -04:00 committed by GitHub
parent e2a86257db
commit f40578fc16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package starlark
import ( import (
"errors" "errors"
"fmt"
"reflect" "reflect"
"strings" "strings"
@ -259,7 +260,7 @@ func asStarlarkValue(value interface{}) (starlark.Value, error) {
return starlark.Bool(v.Bool()), nil return starlark.Bool(v.Bool()), nil
} }
return starlark.None, errors.New("invalid type") return nil, fmt.Errorf("invalid type %T", value)
} }
// AsGoValue converts a starlark.Value to a field value. // AsGoValue converts a starlark.Value to a field value.
@ -270,7 +271,7 @@ func asGoValue(value interface{}) (interface{}, error) {
case starlark.Int: case starlark.Int:
n, ok := v.Int64() n, ok := v.Int64()
if !ok { if !ok {
return nil, errors.New("cannot represent integer as int64") return nil, fmt.Errorf("cannot represent integer %v as int64", v)
} }
return n, nil return n, nil
case starlark.String: case starlark.String:
@ -279,7 +280,7 @@ func asGoValue(value interface{}) (interface{}, error) {
return bool(v), nil return bool(v), nil
} }
return nil, errors.New("invalid starlark type") return nil, fmt.Errorf("invalid starlark type %T", value)
} }
// ToFields converts a starlark.Value to a map of values. // ToFields converts a starlark.Value to a map of values.

View File

@ -243,6 +243,20 @@ def apply(metric):
return metric return metric
``` ```
**What does `cannot represent integer ...` mean?**
The error occurs if an integer value in starlark exceeds the signed 64 bit
integer limit. This can occur if you are summing up large values in a starlark
integer value or convert an unsigned 64 bit integer to starlark and then create
a new metric field from it.
This is due to the fact that integer values in starlark are *always* signed and
can grow beyond the 64-bit size. Therefore converting the value back fails in
the cases mentioned above.
As a workaround you can either clip the field value at the signed 64-bit limit
or return the value as a floating-point number.
### Examples ### Examples
- [drop string fields](testdata/drop_string_fields.star) - Drop fields containing string values. - [drop string fields](testdata/drop_string_fields.star) - Drop fields containing string values.

View File

@ -2572,7 +2572,7 @@ def apply(metric):
for _, m := range tt.input { for _, m := range tt.input {
err = plugin.Add(m, &acc) err = plugin.Add(m, &acc)
if tt.expectedErrorStr != "" { if tt.expectedErrorStr != "" {
require.EqualError(t, err, tt.expectedErrorStr) require.ErrorContains(t, err, tt.expectedErrorStr)
} else { } else {
require.NoError(t, err) require.NoError(t, err)
} }