Skip to content

Commit

Permalink
add sum function (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov authored Jul 15, 2020
1 parent 79a017d commit fc927c2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected].0
go get github.com/spyzhov/ajson/cmd/[email protected].1
```

Usage:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/ajson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spyzhov/ajson"
)

var version = "v0.4.0"
var version = "v0.4.1"

func usage() {
text := ``
Expand Down
18 changes: 18 additions & 0 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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) {
Expand Down

0 comments on commit fc927c2

Please sign in to comment.