Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ func TestSearch(t *testing.T) {
}},
},
want: 1.0,
}, {
args: args{
expression: "length(@[?to_string(metric.__name__) == 'foo'])",
data: []struct {
Metric map[Label]Label
}{{
Metric: map[Label]Label{
"__name__": "foo",
},
}, {
Metric: map[Label]Label{
"__name__": "bar",
},
}},
},
want: 1.0,
}, {
args: args{
expression: "length(@[?metric.__name__ == 'foo'])",
Expand Down
5 changes: 5 additions & 0 deletions pkg/functions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
JpAny JpType = "any"
)

var stringType = reflect.TypeFor[string]()

type FunctionEntry struct {
Name string
Arguments []ArgSpec
Expand Down Expand Up @@ -695,6 +697,9 @@ func jpfToString(arguments []interface{}) (interface{}, error) {
if v, ok := arguments[0].(string); ok {
return v, nil
}
if argType := reflect.TypeOf(arguments[0]); argType.Kind() == stringType.Kind() {
return reflect.ValueOf(arguments[0]).Convert(stringType).Interface(), nil
}
result, err := json.Marshal(arguments[0])
if err != nil {
return nil, err
Expand Down