Skip to content

Commit

Permalink
Accept x/y pair slices, interleave results of union-selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Mar 30, 2018
1 parent 152033f commit d8f478e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 1.1.8
VERSION = 1.1.9

APP := jp
PACKAGES := $(shell go list -f {{.Dir}} ./...)
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/jp/hist2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion cmd/jp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 35 additions & 5 deletions cmd/jp/split.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
12 changes: 7 additions & 5 deletions pkg/jsonpath/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d8f478e

Please sign in to comment.