forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.go
67 lines (59 loc) · 1.51 KB
/
default.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
package pipeline
import "fmt"
// Defaults fields and tags on data points.
//
// Example:
// stream
// |default()
// .field('value', 0.0)
// .tag('host', '')
//
// The above example will set the field `value` to float64(0) if it does not already exist
// It will also set the tag `host` to string("") if it does not already exist.
//
// Available Statistics:
//
// * fields_defaulted -- number of fields that were missing
// * tags_defaulted -- number of tags that were missing
//
type DefaultNode struct {
chainnode
// Set of fields to default
// tick:ignore
Fields map[string]interface{} `tick:"Field"`
// Set of tags to default
Tags map[string]string `tick:"Tag"`
}
func newDefaultNode(e EdgeType) *DefaultNode {
n := &DefaultNode{
chainnode: newBasicChainNode("default", e, e),
Fields: make(map[string]interface{}),
Tags: make(map[string]string),
}
return n
}
// Define a field default.
// tick:property
func (n *DefaultNode) Field(name string, value interface{}) *DefaultNode {
n.Fields[name] = value
return n
}
// Define a tag default.
// tick:property
func (n *DefaultNode) Tag(name string, value string) *DefaultNode {
n.Tags[name] = value
return n
}
func (n *DefaultNode) validate() error {
for field, value := range n.Fields {
switch value.(type) {
case float64:
case int64:
case bool:
case string:
default:
return fmt.Errorf("unsupported type %T for field %q, field default values must be float,int,string or bool", value, field)
}
}
return nil
}