From d8f478e6cf5d4246ad58512769faa2a89267f06e Mon Sep 17 00:00:00 2001 From: Sergey Grebenshchikov Date: Fri, 30 Mar 2018 22:56:37 +0200 Subject: Accept x/y pair slices, interleave results of union-selection. --- Makefile | 2 +- README.md | 12 ++++++------ cmd/jp/hist2d.go | 2 +- cmd/jp/main.go | 5 ++++- cmd/jp/split.go | 40 +++++++++++++++++++++++++++++++++++----- pkg/jsonpath/jsonpath.go | 12 +++++++----- 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index afc7af4..e32eb2e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 1.1.8 +VERSION = 1.1.9 APP := jp PACKAGES := $(shell go list -f {{.Dir}} ./...) diff --git a/README.md b/README.md index 97a5890..45c4f42 100644 --- a/README.md +++ b/README.md @@ -39,16 +39,16 @@ Or [download the binary](https://github.com/sgreben/jp/releases/latest) from the ```bash # Linux -curl -LO https://github.com/sgreben/jp/releases/download/1.1.8/jp_1.1.8_linux_x86_64.zip -unzip jp_1.1.8_linux_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.9/jp_1.1.9_linux_x86_64.zip +unzip jp_1.1.9_linux_x86_64.zip # OS X -curl -LO https://github.com/sgreben/jp/releases/download/1.1.8/jp_1.1.8_osx_x86_64.zip -unzip jp_1.1.8_osx_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.9/jp_1.1.9_osx_x86_64.zip +unzip jp_1.1.9_osx_x86_64.zip # Windows -curl -LO https://github.com/sgreben/jp/releases/download/1.1.8/jp_1.1.8_windows_x86_64.zip -unzip jp_1.1.8_windows_x86_64.zip +curl -LO https://github.com/sgreben/jp/releases/download/1.1.9/jp_1.1.9_windows_x86_64.zip +unzip jp_1.1.9_windows_x86_64.zip ``` ## Use it diff --git a/cmd/jp/hist2d.go b/cmd/jp/hist2d.go index 1af6a69..d80af59 100644 --- a/cmd/jp/hist2d.go +++ b/cmd/jp/hist2d.go @@ -9,7 +9,7 @@ import ( "github.com/sgreben/jp/pkg/plot" ) -func hist2DData(xv []reflect.Value, yv []reflect.Value, nbins uint) (heatmap *data.Heatmap) { +func hist2DData(xv, yv []reflect.Value, nbins uint) (heatmap *data.Heatmap) { var x, y []float64 for i := range xv { if xv[i].IsValid() && xv[i].CanInterface() { diff --git a/cmd/jp/main.go b/cmd/jp/main.go index 4f26ecb..6cc9898 100644 --- a/cmd/jp/main.go +++ b/cmd/jp/main.go @@ -97,17 +97,20 @@ func init() { var err error xPattern = jsonpath.New("x") + xPattern.AllowMissingKeys(true) err = xPattern.Parse(fmt.Sprintf("{%s}", config.X)) if err != nil { log.Fatal(err) } yPattern = jsonpath.New("y") + yPattern.AllowMissingKeys(true) err = yPattern.Parse(fmt.Sprintf("{%s}", config.Y)) if err != nil { log.Fatal(err) } - if config.XY != "" { + if config.XY != "" || (config.X == "" && config.Y == "") { xyPattern = jsonpath.New("xy") + xyPattern.AllowMissingKeys(true) err = xyPattern.Parse(fmt.Sprintf("{%s}", config.XY)) if err != nil { log.Fatal(err) diff --git a/cmd/jp/split.go b/cmd/jp/split.go index efe59c9..acca93c 100644 --- a/cmd/jp/split.go +++ b/cmd/jp/split.go @@ -1,11 +1,34 @@ package main -import "reflect" +import ( + "reflect" +) + +var indexableKind = map[reflect.Kind]bool{ + reflect.Slice: true, + reflect.Array: true, +} func flatten(in [][]reflect.Value) (out []reflect.Value) { for _, a := range in { - for i := range a { - out = append(out, a[i]) + for _, v := range a { + if indexableKind[v.Kind()] { + sub := make([]reflect.Value, v.Len()) + for j := 0; j < v.Len(); j++ { + sub = append(sub, v.Index((j))) + } + out = append(out, flatten([][]reflect.Value{sub})...) + continue + } + if v.IsValid() && v.CanInterface() { + if sub, ok := v.Interface().([]interface{}); ok { + for i := range sub { + out = append(out, reflect.ValueOf(sub[i])) + } + continue + } + } + out = append(out, v) } } return @@ -14,7 +37,14 @@ func flatten(in [][]reflect.Value) (out []reflect.Value) { func split(in [][]reflect.Value) (x, y []reflect.Value) { flat := flatten(in) n := len(flat) - x = flat[:n/2] - y = flat[n/2:] + x = make([]reflect.Value, 0, n/2) + y = make([]reflect.Value, 0, n/2) + for i := range flat { + if i&1 == 0 { + x = append(x, flat[i]) + } else { + y = append(y, flat[i]) + } + } return } diff --git a/pkg/jsonpath/jsonpath.go b/pkg/jsonpath/jsonpath.go index 6b11660..af18607 100644 --- a/pkg/jsonpath/jsonpath.go +++ b/pkg/jsonpath/jsonpath.go @@ -253,12 +253,14 @@ func (j *JSONPath) evalArray(input []reflect.Value, node *ArrayNode) ([]reflect. // evalUnion evaluates UnionNode func (j *JSONPath) evalUnion(input []reflect.Value, node *UnionNode) ([]reflect.Value, error) { result := []reflect.Value{} - for _, listNode := range node.Nodes { - temp, err := j.evalList(input, listNode) - if err != nil { - return input, err + for _, inputValue := range input { + for _, listNode := range node.Nodes { + temp, err := j.evalList([]reflect.Value{inputValue}, listNode) + if err != nil { + return input, err + } + result = append(result, temp...) } - result = append(result, temp...) } return result, nil } -- cgit v1.2.3