From fc927c29b23d3941a034b2e88c5f1dfbe88ff8bf Mon Sep 17 00:00:00 2001 From: Stepan Pyzhov <32341341+spyzhov@users.noreply.github.com> Date: Wed, 15 Jul 2020 02:30:48 +0200 Subject: [PATCH] add sum function (#32) --- README.md | 3 ++- cmd/ajson/main.go | 2 +- math.go | 18 ++++++++++++++++++ math_test.go | 25 +++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3bdf8d7..a777b2a 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ func main() { You can download `ajson` cli from the [release page](https://github.com/spyzhov/ajson/releases), or install from the source: ```shell script -go get github.com/spyzhov/ajson/cmd/ajson@v0.4.0 +go get github.com/spyzhov/ajson/cmd/ajson@v0.4.1 ``` Usage: @@ -327,6 +327,7 @@ Package has several predefined functions. roundtoeven math.RoundToEven integers, floats sin math.Sin integers, floats sinh math.Sinh integers, floats + sum Sum array of integers or floats sqrt math.Sqrt integers, floats tan math.Tan integers, floats tanh math.Tanh integers, floats diff --git a/cmd/ajson/main.go b/cmd/ajson/main.go index 7c01097..448244a 100644 --- a/cmd/ajson/main.go +++ b/cmd/ajson/main.go @@ -12,7 +12,7 @@ import ( "github.com/spyzhov/ajson" ) -var version = "v0.4.0" +var version = "v0.4.1" func usage() { text := `` diff --git a/math.go b/math.go index 794719f..afd3aee 100644 --- a/math.go +++ b/math.go @@ -373,6 +373,24 @@ var ( } return valueNode(nil, "avg", Null, nil), nil }, + "sum": func(node *Node) (result *Node, err error) { + if node.isContainer() { + sum := float64(0) + if node.Size() == 0 { + return valueNode(nil, "sum", Numeric, sum), nil + } + var value float64 + for _, temp := range node.Inheritors() { + value, err = temp.GetNumeric() + if err != nil { + return nil, err + } + sum += value + } + return valueNode(nil, "sum", Numeric, sum), nil + } + return valueNode(nil, "sum", Null, nil), nil + }, "not": func(node *Node) (result *Node, err error) { if value, err := boolean(node); err != nil { return nil, err diff --git a/math_test.go b/math_test.go index 92a0740..e8e66bc 100644 --- a/math_test.go +++ b/math_test.go @@ -395,6 +395,7 @@ func TestFunctions2(t *testing.T) { {name: "length numeric", fname: "length", value: NumericNode("", 123), result: NumericNode("", 1)}, {name: "length bool", fname: "length", value: BoolNode("", false), result: NumericNode("", 1)}, {name: "length null", fname: "length", value: NullNode(""), result: NumericNode("", 1)}, + {name: "avg error 1", fname: "avg", value: ArrayNode("test", []*Node{ valueNode(nil, "", Numeric, "foo"), valueNode(nil, "", Numeric, "foo"), @@ -418,6 +419,30 @@ func TestFunctions2(t *testing.T) { "e": NumericNode("", 3), }), result: NumericNode("", 2)}, {name: "avg array blank", fname: "avg", value: ArrayNode("test", []*Node{}), result: NumericNode("", 0)}, + + {name: "sum error 1", fname: "sum", value: ArrayNode("test", []*Node{ + valueNode(nil, "", Numeric, "foo"), + valueNode(nil, "", Numeric, "foo"), + valueNode(nil, "", Numeric, "foo"), + }), fail: true}, + {name: "sum error 2", fname: "sum", value: _e, fail: false, result: NullNode("")}, + {name: "sum array 1", fname: "sum", value: ArrayNode("test", []*Node{ + NumericNode("", 1), + NumericNode("", 1), + NumericNode("", 1), + NumericNode("", 1), + }), result: NumericNode("", 4)}, + {name: "sum array 2", fname: "sum", value: ArrayNode("test", []*Node{ + NumericNode("", 1), + NumericNode("", 2), + NumericNode("", 3), + }), result: NumericNode("", 6)}, + {name: "sum object", fname: "sum", value: ObjectNode("test", map[string]*Node{ + "q": NumericNode("", 1), + "w": NumericNode("", 2), + "e": NumericNode("", 3), + }), result: NumericNode("", 6)}, + {name: "sum array blank", fname: "sum", value: ArrayNode("test", []*Node{}), result: NumericNode("", 0)}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) {