27 | 27 |
return str
|
28 | 28 |
}
|
29 | 29 |
|
30 | |
func toInfluxRepr(tag string, val interface{}) (string, error) {
|
|
30 |
func toInfluxRepr(tag string, val interface{}, nostatictypes bool) (string, error) {
|
31 | 31 |
switch v := val.(type) {
|
32 | 32 |
case string:
|
33 | 33 |
if len(v) > 64000 {
|
34 | 34 |
return "", fmt.Errorf("%s: string too long (%d characters, max. 64K)", tag, len(v))
|
35 | 35 |
}
|
36 | 36 |
return fmt.Sprintf("%q", v), nil
|
37 | |
case int32, int64, int16, int8, int:
|
38 | |
return fmt.Sprintf("%di", v), nil
|
39 | |
case uint32, uint64, uint16, uint8, uint:
|
|
37 |
case int32, int64, int16, int8, int, uint32, uint64, uint16, uint8, uint:
|
|
38 |
if nostatictypes {
|
|
39 |
return fmt.Sprintf("%d", v), nil
|
|
40 |
}
|
40 | 41 |
return fmt.Sprintf("%di", v), nil
|
41 | 42 |
case float64, float32:
|
42 | 43 |
return fmt.Sprintf("%g", v), nil
|
|
50 | 51 |
}
|
51 | 52 |
|
52 | 53 |
func recordFields(val interface{},
|
53 | |
fieldSet map[string]string) (map[string]string, error) {
|
|
54 |
fieldSet map[string]string, nostatictypes bool) (map[string]string, error) {
|
54 | 55 |
t := reflect.TypeOf(val)
|
55 | 56 |
v := reflect.ValueOf(val)
|
56 | 57 |
|
|
60 | 61 |
if tag == "" {
|
61 | 62 |
continue
|
62 | 63 |
}
|
63 | |
repr, err := toInfluxRepr(tag, v.Field(i).Interface())
|
|
64 |
repr, err := toInfluxRepr(tag, v.Field(i).Interface(), nostatictypes)
|
64 | 65 |
if err != nil {
|
65 | 66 |
return nil, err
|
66 | 67 |
}
|
|
115 | 116 |
|
116 | 117 |
// Encode writes the line protocol representation for a given measurement
|
117 | 118 |
// name, data struct and tag map to the io.Writer specified on encoder creation.
|
118 | |
func (a *Encoder) Encode(prefix string, val interface{},
|
119 | |
tags map[string]string) error {
|
|
119 |
func (a *Encoder) encodeGeneric(prefix string, val interface{},
|
|
120 |
tags map[string]string, nostatictypes bool) error {
|
120 | 121 |
fieldSet := make(map[string]string)
|
121 | |
fieldSet, err := recordFields(val, fieldSet)
|
|
122 |
fieldSet, err := recordFields(val, fieldSet, nostatictypes)
|
122 | 123 |
if err != nil {
|
123 | 124 |
return err
|
124 | 125 |
}
|
125 | 126 |
_, err = a.Writer.Write([]byte(a.formatLineProtocol(prefix, tags, fieldSet)))
|
126 | 127 |
return err
|
|
128 |
}
|
|
129 |
|
|
130 |
// Encode writes the line protocol representation for a given measurement
|
|
131 |
// name, data struct and tag map to the io.Writer specified on encoder creation.
|
|
132 |
func (a *Encoder) Encode(prefix string, val interface{},
|
|
133 |
tags map[string]string) error {
|
|
134 |
return a.encodeGeneric(prefix, val, tags, false)
|
|
135 |
}
|
|
136 |
|
|
137 |
// EncodeWithoutTypes writes the line protocol representation for a given measurement
|
|
138 |
// name, data struct and tag map to the io.Writer specified on encoder creation.
|
|
139 |
// In contrast to Encode(), this method never appends type suffixes to values.
|
|
140 |
func (a *Encoder) EncodeWithoutTypes(prefix string, val interface{},
|
|
141 |
tags map[string]string) error {
|
|
142 |
return a.encodeGeneric(prefix, val, tags, true)
|
127 | 143 |
}
|
128 | 144 |
|
129 | 145 |
// EncodeMap writes the line protocol representation for a given measurement
|