Skip to content

Commit 30d30ed

Browse files
committed
fix: support string compatible types in to_string
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
1 parent bdbb290 commit 30d30ed

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/api/api_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ func TestSearch(t *testing.T) {
163163
}},
164164
},
165165
want: 1.0,
166+
}, {
167+
args: args{
168+
expression: "length(@[?to_string(metric.__name__) == 'foo'])",
169+
data: []struct {
170+
Metric map[Label]Label
171+
}{{
172+
Metric: map[Label]Label{
173+
"__name__": "foo",
174+
},
175+
}, {
176+
Metric: map[Label]Label{
177+
"__name__": "bar",
178+
},
179+
}},
180+
},
181+
want: 1.0,
166182
}, {
167183
args: args{
168184
expression: "length(@[?metric.__name__ == 'foo'])",

pkg/functions/functions.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const (
3434
JpAny JpType = "any"
3535
)
3636

37+
var stringType = reflect.TypeFor[string]()
38+
3739
type FunctionEntry struct {
3840
Name string
3941
Arguments []ArgSpec
@@ -695,6 +697,9 @@ func jpfToString(arguments []interface{}) (interface{}, error) {
695697
if v, ok := arguments[0].(string); ok {
696698
return v, nil
697699
}
700+
if argType := reflect.TypeOf(arguments[0]); argType.Kind() == stringType.Kind() {
701+
return reflect.ValueOf(arguments[0]).Convert(stringType).Interface(), nil
702+
}
698703
result, err := json.Marshal(arguments[0])
699704
if err != nil {
700705
return nil, err

0 commit comments

Comments
 (0)