From 840ede8f512cd83ed1706831ba5d506e8bab0fa8 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 19 Sep 2022 13:48:12 +0100 Subject: [PATCH] add some new features to go-jsonata [xia] --- README.md | 6 +++--- callable.go | 6 +++--- callable_test.go | 4 ++-- env.go | 18 +++++++++++++++--- error.go | 2 +- eval.go | 8 ++++---- eval_test.go | 6 +++--- example_eval_test.go | 2 +- example_exts_test.go | 2 +- go.mod | 2 +- jlib/aggregate.go | 4 ++-- jlib/array.go | 2 +- jlib/boolean.go | 2 +- jlib/date.go | 4 ++-- jlib/date_test.go | 4 ++-- jlib/hof.go | 2 +- jlib/jlib.go | 2 +- jlib/number.go | 2 +- jlib/number_test.go | 4 ++-- jlib/object.go | 2 +- jlib/object_test.go | 4 ++-- jlib/string.go | 4 ++-- jlib/string_test.go | 4 ++-- jlib/unescape.go | 18 ++++++++++++++++++ jparse/jparse_test.go | 2 +- jsonata-server/README.md | 2 +- jsonata-server/bench.go | 2 +- jsonata-server/exts.go | 4 ++-- jsonata-server/main.go | 6 ++---- jsonata-test/main.go | 4 ++-- jsonata.go | 6 +++--- jsonata_test.go | 4 ++-- 32 files changed, 86 insertions(+), 58 deletions(-) create mode 100644 jlib/unescape.go diff --git a/README.md b/README.md index aaa8abf..7e4d61c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ It currently has feature parity with jsonata-js 1.5.4. As well as a most of the ## Install - go get github.com/blues/jsonata-go + go get github.com/xiatechs/jsonata-go ## Usage @@ -17,7 +17,7 @@ import ( "fmt" "log" - jsonata "github.com/blues/jsonata-go" + jsonata "github.com/xiatechs/jsonata-go" ) const jsonString = ` @@ -56,7 +56,7 @@ func main() { ## JSONata Server A locally hosted version of [JSONata Exerciser](http://try.jsonata.org/) -for testing is [available here](https://github.com/blues/jsonata-go/jsonata-server). +for testing is [available here](https://github.com/xiatechs/jsonata-go/jsonata-server). ## JSONata tests A CLI tool for running jsonata-go against the [JSONata test suite](https://github.com/jsonata-js/jsonata/tree/master/test/test-suite) is [available here](./jsonata-test). diff --git a/callable.go b/callable.go index ec08501..0a78e1c 100644 --- a/callable.go +++ b/callable.go @@ -11,9 +11,9 @@ import ( "regexp" "strings" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" ) type callableName struct { diff --git a/callable_test.go b/callable_test.go index 75062bc..cf7a96b 100644 --- a/callable_test.go +++ b/callable_test.go @@ -13,8 +13,8 @@ import ( "strings" "testing" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" ) var ( diff --git a/env.go b/env.go index bbbefc5..e64b7c0 100644 --- a/env.go +++ b/env.go @@ -11,9 +11,9 @@ import ( "strings" "unicode/utf8" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" ) type environment struct { @@ -67,6 +67,18 @@ var ( var baseEnv = initBaseEnv(map[string]Extension{ + /* + EXTENDED START + */ + "unescape": { + Func: jlib.Unescape, + UndefinedHandler: defaultUndefinedHandler, + EvalContextHandler: defaultContextHandler, + }, + /* + EXTENDED END + */ + // String functions "string": { diff --git a/error.go b/error.go index f5d383c..3688ebf 100644 --- a/error.go +++ b/error.go @@ -9,7 +9,7 @@ import ( "fmt" "regexp" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) // ErrUndefined is returned by the evaluation methods when diff --git a/eval.go b/eval.go index 0ae1989..e4d026b 100644 --- a/eval.go +++ b/eval.go @@ -10,10 +10,10 @@ import ( "reflect" "sort" - "github.com/blues/jsonata-go/config" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/config" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" "github.com/shopspring/decimal" ) diff --git a/eval_test.go b/eval_test.go index 80c9378..019a1d5 100644 --- a/eval_test.go +++ b/eval_test.go @@ -11,9 +11,9 @@ import ( "strings" "testing" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" ) type evalTestCase struct { diff --git a/example_eval_test.go b/example_eval_test.go index fd35592..fc24a41 100644 --- a/example_eval_test.go +++ b/example_eval_test.go @@ -9,7 +9,7 @@ import ( "fmt" "log" - jsonata "github.com/blues/jsonata-go" + jsonata "github.com/xiatechs/jsonata-go" ) const jsonString = ` diff --git a/example_exts_test.go b/example_exts_test.go index 2c02e18..7a1bd05 100644 --- a/example_exts_test.go +++ b/example_exts_test.go @@ -9,7 +9,7 @@ import ( "log" "strings" - jsonata "github.com/blues/jsonata-go" + jsonata "github.com/xiatechs/jsonata-go" ) // diff --git a/go.mod b/go.mod index 4ec3f7b..d4de1f2 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/blues/jsonata-go +module github.com/xiatechs/jsonata-go go 1.16 diff --git a/jlib/aggregate.go b/jlib/aggregate.go index 8dc0e67..9c94706 100644 --- a/jlib/aggregate.go +++ b/jlib/aggregate.go @@ -8,8 +8,8 @@ import ( "fmt" "reflect" - "github.com/blues/jsonata-go/config" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/config" + "github.com/xiatechs/jsonata-go/jtypes" "github.com/shopspring/decimal" ) diff --git a/jlib/array.go b/jlib/array.go index f16f711..8c1f734 100644 --- a/jlib/array.go +++ b/jlib/array.go @@ -10,7 +10,7 @@ import ( "reflect" "sort" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) // Count (golint) diff --git a/jlib/boolean.go b/jlib/boolean.go index b4325f4..067e2f9 100644 --- a/jlib/boolean.go +++ b/jlib/boolean.go @@ -7,7 +7,7 @@ package jlib import ( "reflect" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) // Boolean (golint) diff --git a/jlib/date.go b/jlib/date.go index 70a9a2c..c5ee7d4 100644 --- a/jlib/date.go +++ b/jlib/date.go @@ -10,8 +10,8 @@ import ( "strconv" "time" - "github.com/blues/jsonata-go/jlib/jxpath" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib/jxpath" + "github.com/xiatechs/jsonata-go/jtypes" ) // 2006-01-02T15:04:05.000Z07:00 diff --git a/jlib/date_test.go b/jlib/date_test.go index 7c42c62..540fcb9 100644 --- a/jlib/date_test.go +++ b/jlib/date_test.go @@ -9,8 +9,8 @@ import ( "testing" "time" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jtypes" ) func TestFromMillis(t *testing.T) { diff --git a/jlib/hof.go b/jlib/hof.go index 6452a3b..b7b07c4 100644 --- a/jlib/hof.go +++ b/jlib/hof.go @@ -8,7 +8,7 @@ import ( "fmt" "reflect" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) // Map (golint) diff --git a/jlib/jlib.go b/jlib/jlib.go index 644a044..027a047 100644 --- a/jlib/jlib.go +++ b/jlib/jlib.go @@ -11,7 +11,7 @@ import ( "reflect" "time" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) func init() { diff --git a/jlib/number.go b/jlib/number.go index 8cb0e46..9cc62b0 100644 --- a/jlib/number.go +++ b/jlib/number.go @@ -13,7 +13,7 @@ import ( "strconv" "strings" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) var reNumber = regexp.MustCompile(`^-?(([0-9]+))(\.[0-9]+)?([Ee][-+]?[0-9]+)?$`) diff --git a/jlib/number_test.go b/jlib/number_test.go index 51b422f..478855f 100644 --- a/jlib/number_test.go +++ b/jlib/number_test.go @@ -8,8 +8,8 @@ import ( "fmt" "testing" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jtypes" ) func TestRound(t *testing.T) { diff --git a/jlib/object.go b/jlib/object.go index ca43f8d..36284ab 100644 --- a/jlib/object.go +++ b/jlib/object.go @@ -8,7 +8,7 @@ import ( "fmt" "reflect" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jtypes" ) // typeInterfaceMap is the reflect.Type for map[string]interface{}. diff --git a/jlib/object_test.go b/jlib/object_test.go index e2e8dc7..3e97ece 100644 --- a/jlib/object_test.go +++ b/jlib/object_test.go @@ -12,8 +12,8 @@ import ( "strings" "testing" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jtypes" ) type eachTest struct { diff --git a/jlib/string.go b/jlib/string.go index cd49c6f..8764825 100644 --- a/jlib/string.go +++ b/jlib/string.go @@ -17,8 +17,8 @@ import ( "strings" "unicode/utf8" - "github.com/blues/jsonata-go/jlib/jxpath" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib/jxpath" + "github.com/xiatechs/jsonata-go/jtypes" ) // String converts a JSONata value to a string. Values that are diff --git a/jlib/string_test.go b/jlib/string_test.go index 21a91c6..5fcb544 100644 --- a/jlib/string_test.go +++ b/jlib/string_test.go @@ -13,8 +13,8 @@ import ( "testing" "unicode/utf8" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jtypes" ) var typereplaceCallable = reflect.TypeOf((*replaceCallable)(nil)).Elem() diff --git a/jlib/unescape.go b/jlib/unescape.go new file mode 100644 index 0000000..5597a49 --- /dev/null +++ b/jlib/unescape.go @@ -0,0 +1,18 @@ +package jlib + +import ( + "fmt" + "encoding/json" +) + +// Unescape a string into JSON - simple but powerful +func Unescape(input string) (interface{}, error) { + var output interface{} + + err := json.Unmarshal([]byte(input), &output) + if err != nil { + return output, fmt.Errorf("unescape json unmarshal error: %v", err) + } + + return output, nil +} \ No newline at end of file diff --git a/jparse/jparse_test.go b/jparse/jparse_test.go index f0874e5..a1967ad 100644 --- a/jparse/jparse_test.go +++ b/jparse/jparse_test.go @@ -12,7 +12,7 @@ import ( "testing" "unicode/utf8" - "github.com/blues/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jparse" ) type testCase struct { diff --git a/jsonata-server/README.md b/jsonata-server/README.md index 4c67f00..3ef953e 100644 --- a/jsonata-server/README.md +++ b/jsonata-server/README.md @@ -5,7 +5,7 @@ for testing [jsonata-go](https://github.com/blues/jsonata). ## Install - go install github.com/blues/jsonata-go/jsonata-server + go install github.com/xiatechs/jsonata-go/jsonata-server ## Usage diff --git a/jsonata-server/bench.go b/jsonata-server/bench.go index 925ecfc..1a0a19f 100644 --- a/jsonata-server/bench.go +++ b/jsonata-server/bench.go @@ -10,7 +10,7 @@ import ( "encoding/json" - jsonata "github.com/blues/jsonata-go" + jsonata "github.com/xiatechs/jsonata-go" ) var ( diff --git a/jsonata-server/exts.go b/jsonata-server/exts.go index 117beb6..3382a2a 100644 --- a/jsonata-server/exts.go +++ b/jsonata-server/exts.go @@ -5,8 +5,8 @@ package main import ( - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jtypes" ) // Default format for dates: e.g. 2006-01-02 15:04 MST diff --git a/jsonata-server/main.go b/jsonata-server/main.go index 73ebea0..33f3098 100644 --- a/jsonata-server/main.go +++ b/jsonata-server/main.go @@ -14,8 +14,8 @@ import ( _ "net/http/pprof" "strings" - jsonata "github.com/blues/jsonata-go" - "github.com/blues/jsonata-go/jtypes" + jsonata "github.com/xiatechs/jsonata-go" + "github.com/xiatechs/jsonata-go/jtypes" ) func init() { @@ -52,7 +52,6 @@ func main() { } func evaluate(w http.ResponseWriter, r *http.Request) { - input := strings.TrimSpace(r.FormValue("json")) if input == "" { http.Error(w, "Input is empty", http.StatusBadRequest) @@ -78,7 +77,6 @@ func evaluate(w http.ResponseWriter, r *http.Request) { } func eval(input, expression string) (b []byte, status int, err error) { - defer func() { if r := recover(); r != nil { b = nil diff --git a/jsonata-test/main.go b/jsonata-test/main.go index 937e07e..12078ab 100644 --- a/jsonata-test/main.go +++ b/jsonata-test/main.go @@ -12,8 +12,8 @@ import ( "regexp" "strings" - jsonata "github.com/blues/jsonata-go" - types "github.com/blues/jsonata-go/jtypes" + jsonata "github.com/xiatechs/jsonata-go" + types "github.com/xiatechs/jsonata-go/jtypes" ) type testCase struct { diff --git a/jsonata.go b/jsonata.go index 58dd1f5..1422fb8 100644 --- a/jsonata.go +++ b/jsonata.go @@ -13,9 +13,9 @@ import ( "time" "unicode" - "github.com/blues/jsonata-go/jlib" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jlib" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" ) var ( diff --git a/jsonata_test.go b/jsonata_test.go index e967ebe..140754c 100644 --- a/jsonata_test.go +++ b/jsonata_test.go @@ -19,8 +19,8 @@ import ( "time" "unicode/utf8" - "github.com/blues/jsonata-go/jparse" - "github.com/blues/jsonata-go/jtypes" + "github.com/xiatechs/jsonata-go/jparse" + "github.com/xiatechs/jsonata-go/jtypes" ) type testCase struct {