diff --git a/lib/go/jsonframer/CHANGELOG.md b/lib/go/jsonframer/CHANGELOG.md index 5a71b90..51eb7c2 100644 --- a/lib/go/jsonframer/CHANGELOG.md +++ b/lib/go/jsonframer/CHANGELOG.md @@ -1,5 +1,11 @@ # @grafana/infinity-jsonframer +## 1.1.1 + +### Patch Changes + +- Added numeric frame type + ## 1.1.0 - removed sqlite3 framer diff --git a/lib/go/jsonframer/json_test.go b/lib/go/jsonframer/json_test.go index c261e47..10b5f07 100644 --- a/lib/go/jsonframer/json_test.go +++ b/lib/go/jsonframer/json_test.go @@ -300,7 +300,7 @@ func TestJsonStringToMultiFrame(t *testing.T) { {"name": "bar", "age": 3} ],[ {"name": "foo", "salary": 2430 }, - {"name": "foo", "salary": 3000 } + {"name": "bar", "salary": 3000 } ] ]`, jsonframer.FramerOptions{}) require.Nil(t, err) @@ -328,6 +328,44 @@ func TestJsonStringToMultiFrame(t *testing.T) { require.NotNil(t, frames) experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "regular-object", &backend.DataResponse{Frames: frames}, true) }) + t.Run("numeric frame", func(t *testing.T) { + t.Run("array of arrays", func(t *testing.T) { + frames, err := jsonframer.ToFrames(`[ + [ + {"name": "foo", "age": 2}, + {"name": "bar", "age": 3} + ],[ + {"name": "foo", "salary": 2430 }, + {"name": "bar", "salary": 3000 } + ] + ]`, jsonframer.FramerOptions{FrameFormat: jsonframer.FrameFormatNumeric}) + require.Nil(t, err) + require.NotNil(t, frames) + experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "array-of-array-numeric", &backend.DataResponse{Frames: frames}, true) + }) + t.Run("array of objects", func(t *testing.T) { + frames, err := jsonframer.ToFrames(`[ + { "name": "foo", "age": 2, "salary": 2430 }, + { "name": "bar", "age": 3, "salary": 3000 } + ]`, jsonframer.FramerOptions{FrameFormat: jsonframer.FrameFormatNumeric}) + require.Nil(t, err) + require.NotNil(t, frames) + experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "array-of-objects-numeric", &backend.DataResponse{Frames: frames}, true) + }) + t.Run("array of items", func(t *testing.T) { + frames, err := jsonframer.ToFrames(`["foo","bar"]`, jsonframer.FramerOptions{FrameName: "result", FrameFormat: jsonframer.FrameFormatNumeric}) + require.Nil(t, err) + require.NotNil(t, frames) + experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "array-of-items-numeric", &backend.DataResponse{Frames: frames}, true) + }) + t.Run("regular object", func(t *testing.T) { + frames, err := jsonframer.ToFrames(`{"name": "foo", "age": 2, "salary": 2430}`, jsonframer.FramerOptions{FrameFormat: jsonframer.FrameFormatNumeric}) + require.Nil(t, err) + require.NotNil(t, frames) + experimental.CheckGoldenJSONResponse(t, "testdata/multiframer", "regular-object-numeric", &backend.DataResponse{Frames: frames}, true) + }) + + }) } func TestAzureFrame(t *testing.T) { diff --git a/lib/go/jsonframer/jsonframer.go b/lib/go/jsonframer/jsonframer.go index 8edf402..87bc7f7 100644 --- a/lib/go/jsonframer/jsonframer.go +++ b/lib/go/jsonframer/jsonframer.go @@ -23,6 +23,7 @@ type FrameFormat string const ( FrameFormatTable FrameFormat = "table" FrameFormatTimeSeries FrameFormat = "timeseries" + FrameFormatNumeric FrameFormat = "numeric" ) type FramerOptions struct { @@ -105,7 +106,6 @@ func ToFrames(jsonString string, options FramerOptions) (frames []*data.Frame, e return frames, err } if frame != nil { - if options.FrameFormat == FrameFormatTimeSeries && frame.TimeSeriesSchema().Type == data.TimeSeriesTypeLong { frame, err = data.LongToWide(frame, nil) if err != nil { @@ -164,7 +164,6 @@ func GetRootData(jsonString string, rootSelector string) (string, error) { return string(r2), nil } return jsonString, nil - } func getColumnValuesFromResponseString(responseString string, columns []ColumnSelector) (string, error) { @@ -230,11 +229,19 @@ func getFrameFromResponseString(responseString string, options FramerOptions) (f TimeFormat: c.TimeFormat, }) } - return gframer.ToDataFrame(out, gframer.FramerOptions{ + frame, err = gframer.ToDataFrame(out, gframer.FramerOptions{ FrameName: options.FrameName, Columns: columns, OverrideColumns: overrides, }) + if options.FrameFormat == FrameFormatNumeric { + if frame.Meta == nil { + frame.Meta = &data.FrameMeta{} + } + frame.Meta.Type = data.FrameTypeNumericLong + frame.Meta.TypeVersion = data.FrameTypeVersion{0, 1} + } + return frame, err } func convertFieldValueType(input interface{}, _ ColumnSelector) interface{} { diff --git a/lib/go/jsonframer/package.json b/lib/go/jsonframer/package.json index 4e07cb9..a5686f1 100644 --- a/lib/go/jsonframer/package.json +++ b/lib/go/jsonframer/package.json @@ -1,7 +1,7 @@ { "name": "@grafana/infinity-jsonframer", "private": true, - "version": "1.1.0", + "version": "1.1.1", "scripts": { "tidy": "go mod tidy", "test:backend": "go test -v ./..." diff --git a/lib/go/jsonframer/testdata/multiframer/array-of-array-numeric.jsonc b/lib/go/jsonframer/testdata/multiframer/array-of-array-numeric.jsonc new file mode 100644 index 0000000..461f28b --- /dev/null +++ b/lib/go/jsonframer/testdata/multiframer/array-of-array-numeric.jsonc @@ -0,0 +1,129 @@ +// 🌟 This was machine generated. Do not edit. 🌟 +// +// Frame[0] { +// "type": "numeric-long", +// "typeVersion": [ +// 0, +// 1 +// ] +// } +// Name: +// Dimensions: 2 Fields by 2 Rows +// +------------------+-----------------+ +// | Name: age | Name: name | +// | Labels: | Labels: | +// | Type: []*float64 | Type: []*string | +// +------------------+-----------------+ +// | 2 | foo | +// | 3 | bar | +// +------------------+-----------------+ +// +// +// +// Frame[1] { +// "type": "numeric-long", +// "typeVersion": [ +// 0, +// 1 +// ] +// } +// Name: +// Dimensions: 2 Fields by 2 Rows +// +-----------------+------------------+ +// | Name: name | Name: salary | +// | Labels: | Labels: | +// | Type: []*string | Type: []*float64 | +// +-----------------+------------------+ +// | foo | 2430 | +// | bar | 3000 | +// +-----------------+------------------+ +// +// +// 🌟 This was machine generated. Do not edit. 🌟 +{ + "status": 200, + "frames": [ + { + "schema": { + "meta": { + "type": "numeric-long", + "typeVersion": [ + 0, + 1 + ] + }, + "fields": [ + { + "name": "age", + "type": "number", + "typeInfo": { + "frame": "float64", + "nullable": true + } + }, + { + "name": "name", + "type": "string", + "typeInfo": { + "frame": "string", + "nullable": true + } + } + ] + }, + "data": { + "values": [ + [ + 2, + 3 + ], + [ + "foo", + "bar" + ] + ] + } + }, + { + "schema": { + "meta": { + "type": "numeric-long", + "typeVersion": [ + 0, + 1 + ] + }, + "fields": [ + { + "name": "name", + "type": "string", + "typeInfo": { + "frame": "string", + "nullable": true + } + }, + { + "name": "salary", + "type": "number", + "typeInfo": { + "frame": "float64", + "nullable": true + } + } + ] + }, + "data": { + "values": [ + [ + "foo", + "bar" + ], + [ + 2430, + 3000 + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/lib/go/jsonframer/testdata/multiframer/array-of-array.jsonc b/lib/go/jsonframer/testdata/multiframer/array-of-array.jsonc index 702f0cd..a1af385 100644 --- a/lib/go/jsonframer/testdata/multiframer/array-of-array.jsonc +++ b/lib/go/jsonframer/testdata/multiframer/array-of-array.jsonc @@ -23,7 +23,7 @@ // | Type: []*string | Type: []*float64 | // +-----------------+------------------+ // | foo | 2430 | -// | foo | 3000 | +// | bar | 3000 | // +-----------------+------------------+ // // @@ -90,7 +90,7 @@ "values": [ [ "foo", - "foo" + "bar" ], [ 2430, diff --git a/lib/go/jsonframer/testdata/multiframer/array-of-items-numeric.jsonc b/lib/go/jsonframer/testdata/multiframer/array-of-items-numeric.jsonc new file mode 100644 index 0000000..6d03ca6 --- /dev/null +++ b/lib/go/jsonframer/testdata/multiframer/array-of-items-numeric.jsonc @@ -0,0 +1,57 @@ +// 🌟 This was machine generated. Do not edit. 🌟 +// +// Frame[0] { +// "type": "numeric-long", +// "typeVersion": [ +// 0, +// 1 +// ] +// } +// Name: result +// Dimensions: 1 Fields by 2 Rows +// +-----------------+ +// | Name: result | +// | Labels: | +// | Type: []*string | +// +-----------------+ +// | foo | +// | bar | +// +-----------------+ +// +// +// 🌟 This was machine generated. Do not edit. 🌟 +{ + "status": 200, + "frames": [ + { + "schema": { + "name": "result", + "meta": { + "type": "numeric-long", + "typeVersion": [ + 0, + 1 + ] + }, + "fields": [ + { + "name": "result", + "type": "string", + "typeInfo": { + "frame": "string", + "nullable": true + } + } + ] + }, + "data": { + "values": [ + [ + "foo", + "bar" + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/lib/go/jsonframer/testdata/multiframer/array-of-objects-numeric.jsonc b/lib/go/jsonframer/testdata/multiframer/array-of-objects-numeric.jsonc new file mode 100644 index 0000000..8f436b9 --- /dev/null +++ b/lib/go/jsonframer/testdata/multiframer/array-of-objects-numeric.jsonc @@ -0,0 +1,80 @@ +// 🌟 This was machine generated. Do not edit. 🌟 +// +// Frame[0] { +// "type": "numeric-long", +// "typeVersion": [ +// 0, +// 1 +// ] +// } +// Name: +// Dimensions: 3 Fields by 2 Rows +// +------------------+-----------------+------------------+ +// | Name: age | Name: name | Name: salary | +// | Labels: | Labels: | Labels: | +// | Type: []*float64 | Type: []*string | Type: []*float64 | +// +------------------+-----------------+------------------+ +// | 2 | foo | 2430 | +// | 3 | bar | 3000 | +// +------------------+-----------------+------------------+ +// +// +// 🌟 This was machine generated. Do not edit. 🌟 +{ + "status": 200, + "frames": [ + { + "schema": { + "meta": { + "type": "numeric-long", + "typeVersion": [ + 0, + 1 + ] + }, + "fields": [ + { + "name": "age", + "type": "number", + "typeInfo": { + "frame": "float64", + "nullable": true + } + }, + { + "name": "name", + "type": "string", + "typeInfo": { + "frame": "string", + "nullable": true + } + }, + { + "name": "salary", + "type": "number", + "typeInfo": { + "frame": "float64", + "nullable": true + } + } + ] + }, + "data": { + "values": [ + [ + 2, + 3 + ], + [ + "foo", + "bar" + ], + [ + 2430, + 3000 + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/lib/go/jsonframer/testdata/multiframer/regular-object-numeric.jsonc b/lib/go/jsonframer/testdata/multiframer/regular-object-numeric.jsonc new file mode 100644 index 0000000..0ba029a --- /dev/null +++ b/lib/go/jsonframer/testdata/multiframer/regular-object-numeric.jsonc @@ -0,0 +1,76 @@ +// 🌟 This was machine generated. Do not edit. 🌟 +// +// Frame[0] { +// "type": "numeric-long", +// "typeVersion": [ +// 0, +// 1 +// ] +// } +// Name: +// Dimensions: 3 Fields by 1 Rows +// +------------------+-----------------+------------------+ +// | Name: age | Name: name | Name: salary | +// | Labels: | Labels: | Labels: | +// | Type: []*float64 | Type: []*string | Type: []*float64 | +// +------------------+-----------------+------------------+ +// | 2 | foo | 2430 | +// +------------------+-----------------+------------------+ +// +// +// 🌟 This was machine generated. Do not edit. 🌟 +{ + "status": 200, + "frames": [ + { + "schema": { + "meta": { + "type": "numeric-long", + "typeVersion": [ + 0, + 1 + ] + }, + "fields": [ + { + "name": "age", + "type": "number", + "typeInfo": { + "frame": "float64", + "nullable": true + } + }, + { + "name": "name", + "type": "string", + "typeInfo": { + "frame": "string", + "nullable": true + } + }, + { + "name": "salary", + "type": "number", + "typeInfo": { + "frame": "float64", + "nullable": true + } + } + ] + }, + "data": { + "values": [ + [ + 2 + ], + [ + "foo" + ], + [ + 2430 + ] + ] + } + } + ] +} \ No newline at end of file