Skip to content

Commit

Permalink
Chore: Render JSON field as string in golden output (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtreece authored Apr 29, 2022
1 parent 255a02a commit ab60be4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
14 changes: 11 additions & 3 deletions data/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,18 @@ func (f *Frame) StringTable(maxFields, maxRows int) (string, error) {
}

val := reflect.Indirect(reflect.ValueOf(v))
if val.IsValid() {
sRow[colIdx] = fmt.Sprintf("%v", val)
} else {
if !val.IsValid() {
sRow[colIdx] = "null"
continue
}

switch {
case f.Fields[colIdx].Type() == FieldTypeJSON:
sRow[colIdx] = fmt.Sprintf("%s", v.(json.RawMessage))
case f.Fields[colIdx].Type() == FieldTypeNullableJSON:
sRow[colIdx] = fmt.Sprintf("%s", *v.(*json.RawMessage))
default:
sRow[colIdx] = fmt.Sprintf("%v", val)
}
}
table.Append(sRow)
Expand Down
18 changes: 18 additions & 0 deletions experimental/golden_response_checker_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package experimental

import (
"encoding/json"
"flag"
"path/filepath"
"testing"
Expand Down Expand Up @@ -77,6 +78,23 @@ func TestGoldenResponseChecker(t *testing.T) {

require.NoError(t, err)
})

t.Run("should render string for JSON fields", func(t *testing.T) {
m := map[string]int{"a": 1, "b": 2}
b, err := json.Marshal(m)
require.NoError(t, err)
r := json.RawMessage(b)
res := &backend.DataResponse{
Frames: data.Frames{
data.NewFrame("JSON frame",
data.NewField("json.RawMessage", nil, []json.RawMessage{r}),
data.NewField("*json.RawMessage", nil, []*json.RawMessage{&r}),
),
}}
goldenFile := filepath.Join("testdata", "frame-json.txt")
err = CheckGoldenDataResponse(goldenFile, res, true)
require.NoError(t, err)
})
}

func TestReadGoldenFile(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions experimental/testdata/frame-json.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
🌟 This was machine generated. Do not edit. 🌟

Frame[0]
Name: JSON frame
Dimensions: 2 Fields by 1 Rows
+-------------------------+--------------------------+
| Name: json.RawMessage | Name: *json.RawMessage |
| Labels: | Labels: |
| Type: []json.RawMessage | Type: []*json.RawMessage |
+-------------------------+--------------------------+
| {"a":1,"b":2} | {"a":1,"b":2} |
+-------------------------+--------------------------+


====== TEST DATA RESPONSE (arrow base64) ======
FRAME=QVJST1cxAAD/////oAEAABAAAAAAAAoADgAMAAsABAAKAAAAFAAAAAAAAAEEAAoADAAAAAgABAAKAAAACAAAAFgAAAACAAAAKAAAAAQAAAD4/v//CAAAAAwAAAAAAAAAAAAAAAUAAAByZWZJZAAAABj///8IAAAAFAAAAAoAAABKU09OIGZyYW1lAAAEAAAAbmFtZQAAAAACAAAAnAAAABgAAAAAABIAGAAUABMAEgAMAAAACAAEABIAAAAUAAAASAAAAEgAAAAAAAQBRAAAAAEAAAAEAAAAgP///wgAAAAcAAAAEAAAACpqc29uLlJhd01lc3NhZ2UAAAAABAAAAG5hbWUAAAAAAAAAAHz///8QAAAAKmpzb24uUmF3TWVzc2FnZQAAEgAYABQAAAATAAwAAAAIAAQAEgAAABQAAABMAAAAUAAAAAAAAARMAAAAAQAAAAwAAAAIAAwACAAEAAgAAAAIAAAAGAAAAA8AAABqc29uLlJhd01lc3NhZ2UABAAAAG5hbWUAAAAAAAAAAAQABAAEAAAADwAAAGpzb24uUmF3TWVzc2FnZQAAAAAA/////9gAAAAUAAAAAAAAAAwAFgAUABMADAAEAAwAAAAwAAAAAAAAABQAAAAAAAADBAAKABgADAAIAAQACgAAABQAAAB4AAAAAQAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAACAAAAAAAAAANAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAgAAAAAAAAAIAAAAAAAAAANAAAAAAAAAAAAAAACAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAADQAAAHsiYSI6MSwiYiI6Mn0AAAAAAAAADQAAAHsiYSI6MSwiYiI6Mn0AAAAQAAAADAAUABIADAAIAAQADAAAABAAAAAsAAAAOAAAAAAABAABAAAAsAEAAAAAAADgAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAoADAAAAAgABAAKAAAACAAAAFgAAAACAAAAKAAAAAQAAAD4/v//CAAAAAwAAAAAAAAAAAAAAAUAAAByZWZJZAAAABj///8IAAAAFAAAAAoAAABKU09OIGZyYW1lAAAEAAAAbmFtZQAAAAACAAAAnAAAABgAAAAAABIAGAAUABMAEgAMAAAACAAEABIAAAAUAAAASAAAAEgAAAAAAAQBRAAAAAEAAAAEAAAAgP///wgAAAAcAAAAEAAAACpqc29uLlJhd01lc3NhZ2UAAAAABAAAAG5hbWUAAAAAAAAAAHz///8QAAAAKmpzb24uUmF3TWVzc2FnZQAAEgAYABQAAAATAAwAAAAIAAQAEgAAABQAAABMAAAAUAAAAAAAAARMAAAAAQAAAAwAAAAIAAwACAAEAAgAAAAIAAAAGAAAAA8AAABqc29uLlJhd01lc3NhZ2UABAAAAG5hbWUAAAAAAAAAAAQABAAEAAAADwAAAGpzb24uUmF3TWVzc2FnZQDIAQAAQVJST1cx

0 comments on commit ab60be4

Please sign in to comment.