Skip to content

Commit

Permalink
Renamed pivot to pivot_rows, add json type
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrode committed May 10, 2022
1 parent a33b2f0 commit 6f666e2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
[
Expand Down
12 changes: 9 additions & 3 deletions pkg/lib/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/template/template_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6f666e2

Please sign in to comment.