diff --git a/README.md b/README.md index 49157c0..670facc 100644 --- a/README.md +++ b/README.md @@ -1306,7 +1306,7 @@ Absolute path of file at `some/path/myfile.cpp`: {{ file_path "../myfile.tmpl" }} ``` -## `pivot` "keyColumn" "typeColumn" [input] +## `pivot_rows` "keyColumn" "typeColumn" [input] Read a CSV map and turn rows into columns and columns into rows. @@ -1325,7 +1325,7 @@ As a convention the data columns need to be named `1`, `2`, ... Allowed types ar * number (JSON type number) * float64 -Calling ```pivot("key","type",(file_csv "file.csv" ','))``` returns +Calling ```pivot_rows("key","type",(file_csv "file.csv" ','))``` returns ```json [ diff --git a/pkg/lib/template/template_funcs.go b/pkg/lib/template/template_funcs.go index a5c3a55..a018cec 100644 --- a/pkg/lib/template/template_funcs.go +++ b/pkg/lib/template/template_funcs.go @@ -68,8 +68,8 @@ func rowsToMap(keyCol, valCol string, rows []map[string]interface{}) (retMap map return } -// pivot turns rows into columns and columns into rows -func pivot(key, typ string, rows []map[string]any) (sheet []map[string]any, err error) { +// pivotRows turns rows into columns and columns into rows +func pivotRows(key, typ string, rows []map[string]any) (sheet []map[string]any, err error) { getStr := func(data any) (s string) { s, _ = data.(string) @@ -83,7 +83,7 @@ func pivot(key, typ string, rows []map[string]any) (sheet []map[string]any, err continue } switch sheetType { - case "string", "int64", "float64", "number": + case "string", "int64", "float64", "number", "json": // supported default: return nil, errors.Errorf("type %q not supported", sheetType) @@ -109,6 +109,12 @@ func pivot(key, typ string, rows []map[string]any) (sheet []map[string]any, err switch sheetType { case "string": sheetRow[sheetKey] = v + case "json": + var i interface{} + err = json.Unmarshal([]byte(v), &i) + if err == nil { + sheetRow[sheetKey] = i + } case "int64": sheetRow[sheetKey], _ = strconv.ParseInt(v, 10, 64) case "float64": diff --git a/pkg/lib/template/template_funcs_test.go b/pkg/lib/template/template_funcs_test.go index 009ee74..ca61f99 100644 --- a/pkg/lib/template/template_funcs_test.go +++ b/pkg/lib/template/template_funcs_test.go @@ -315,7 +315,7 @@ func TestPivot(t *testing.T) { }, } - dataP, err := pivot("key", "type", data) + dataP, err := pivotRows("key", "type", data) if !assert.NoError(t, err) { return } diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 640a43b..13c76d4 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -232,7 +232,7 @@ func (loader *Loader) Render( "rows_to_map": func(keyColumn, valueColumn string, rowsInput interface{}) (map[string]interface{}, error) { return rowsToMap(keyColumn, valueColumn, getRowsFromInput(rowsInput)) }, - "pivot": pivot, + "pivot_rows": pivotRows, "group_map_rows": func(groupColumn string, rowsInput interface{}) (map[string][]map[string]interface{}, error) { grouped_rows := make(map[string][]map[string]interface{}, 1000) rows := getRowsFromInput(rowsInput)