Codebase list golang-github-dcso-fluxline / 8f3bdf0
add EncodeWithoutTypes() method Sascha Steinbiss 5 years ago
2 changed file(s) with 41 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
2727 return str
2828 }
2929
30 func toInfluxRepr(tag string, val interface{}) (string, error) {
30 func toInfluxRepr(tag string, val interface{}, nostatictypes bool) (string, error) {
3131 switch v := val.(type) {
3232 case string:
3333 if len(v) > 64000 {
3434 return "", fmt.Errorf("%s: string too long (%d characters, max. 64K)", tag, len(v))
3535 }
3636 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 }
4041 return fmt.Sprintf("%di", v), nil
4142 case float64, float32:
4243 return fmt.Sprintf("%g", v), nil
5051 }
5152
5253 func recordFields(val interface{},
53 fieldSet map[string]string) (map[string]string, error) {
54 fieldSet map[string]string, nostatictypes bool) (map[string]string, error) {
5455 t := reflect.TypeOf(val)
5556 v := reflect.ValueOf(val)
5657
6061 if tag == "" {
6162 continue
6263 }
63 repr, err := toInfluxRepr(tag, v.Field(i).Interface())
64 repr, err := toInfluxRepr(tag, v.Field(i).Interface(), nostatictypes)
6465 if err != nil {
6566 return nil, err
6667 }
115116
116117 // Encode writes the line protocol representation for a given measurement
117118 // 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 {
120121 fieldSet := make(map[string]string)
121 fieldSet, err := recordFields(val, fieldSet)
122 fieldSet, err := recordFields(val, fieldSet, nostatictypes)
122123 if err != nil {
123124 return err
124125 }
125126 _, err = a.Writer.Write([]byte(a.formatLineProtocol(prefix, tags, fieldSet)))
126127 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)
127143 }
128144
129145 // EncodeMap writes the line protocol representation for a given measurement
101101 }
102102 }
103103
104 func TestEncoderNoTypes(t *testing.T) {
105 var b bytes.Buffer
106
107 ile := NewEncoder(&b)
108 tags := make(map[string]string)
109 err := ile.EncodeWithoutTypes("mytool", testStruct, tags)
110 if err != nil {
111 t.Fatal(err)
112 }
113 out := b.String()
114
115 if match, _ := regexp.Match(`^mytool,host=[^,]+ testval=1,testvalue=2,testvalue2=-3,testvalue3=\"foobar\\\"baz\",testvaluebool=false,testvalueflt32=3.1415927,testvalueflt64=1.29e-24,testvaluetime=`, []byte(out)); !match {
116 t.Fatalf("unexpected match content: %s", out)
117 }
118 }
119
104120 func TestEncoderStringTooLongFail(t *testing.T) {
105121 var b bytes.Buffer
106122