forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.go
81 lines (76 loc) · 1.7 KB
/
union.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package pipeline
import (
"encoding/json"
"fmt"
)
// Takes the union of all of its parents.
// The union is just a simple pass through.
// Each data points received from each parent is passed onto children nodes
// without modification.
//
// Example:
// var logins = stream
// |from()
// .measurement('logins')
// var logouts = stream
// |from()
// .measurement('logouts')
// var frontpage = stream
// |from()
// .measurement('frontpage')
// // Union all user actions into a single stream
// logins
// |union(logouts, frontpage)
// .rename('user_actions')
// ...
//
type UnionNode struct {
chainnode `json:"-"`
// The new name of the stream.
// If empty the name of the left node
// (i.e. `leftNode.union(otherNode1, otherNode2)`) is used.
Rename string `json:"rename"`
}
func newUnionNode(e EdgeType, nodes []Node) *UnionNode {
u := &UnionNode{
chainnode: newBasicChainNode("union", e, e),
}
for _, n := range nodes {
n.linkChild(u)
}
return u
}
// MarshalJSON converts UnionNode to JSON
func (n *UnionNode) MarshalJSON() ([]byte, error) {
type Alias UnionNode
var raw = &struct {
TypeOf
*Alias
}{
TypeOf: TypeOf{
Type: "union",
ID: n.ID(),
},
Alias: (*Alias)(n),
}
return json.Marshal(raw)
}
// UnmarshalJSON converts JSON to an UnionNode
func (n *UnionNode) UnmarshalJSON(data []byte) error {
type Alias UnionNode
var raw = &struct {
TypeOf
*Alias
}{
Alias: (*Alias)(n),
}
err := json.Unmarshal(data, raw)
if err != nil {
return err
}
if raw.Type != "union" {
return fmt.Errorf("error unmarshaling node %d of type %s as UnionNode", raw.ID, raw.Type)
}
n.setID(raw.ID)
return nil
}