Serialized unknown types as strings
When creating a point manually, the field values are interface{} which allows unsupported types to be passed in. Previously, the code would panic. It will now default to string representation of the value if it's not a known type.pull/3248/head
parent
736f1c142e
commit
4d511571c5
|
@ -1098,7 +1098,11 @@ func (p Fields) MarshalBinary() []byte {
|
|||
case nil:
|
||||
// skip
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown type: %T", v))
|
||||
// Can't determine the type, so convert to string
|
||||
b = append(b, '"')
|
||||
b = append(b, []byte(escapeQuoteString(fmt.Sprintf("%v", v)))...)
|
||||
b = append(b, '"')
|
||||
|
||||
}
|
||||
b = append(b, ',')
|
||||
}
|
||||
|
|
|
@ -1078,3 +1078,22 @@ func TestNewPointEscaped(t *testing.T) {
|
|||
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPointUnhandledType(t *testing.T) {
|
||||
// nil value
|
||||
pt := NewPoint("cpu", nil, Fields{"value": nil}, time.Unix(0, 0))
|
||||
if exp := `cpu value= 0`; pt.String() != exp {
|
||||
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
|
||||
}
|
||||
|
||||
// unsupported type gets stored as string
|
||||
now := time.Unix(0, 0)
|
||||
pt = NewPoint("cpu", nil, Fields{"value": now}, time.Unix(0, 0))
|
||||
if exp := `cpu value="1969-12-31 17:00:00 -0700 MST" 0`; pt.String() != exp {
|
||||
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
|
||||
}
|
||||
|
||||
if exp := "1969-12-31 17:00:00 -0700 MST"; pt.Fields()["value"] != exp {
|
||||
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue