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
Jason Wilder 2015-07-06 16:03:03 -06:00
parent 736f1c142e
commit 4d511571c5
2 changed files with 24 additions and 1 deletions

View File

@ -1098,7 +1098,11 @@ func (p Fields) MarshalBinary() []byte {
case nil: case nil:
// skip // skip
default: 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, ',') b = append(b, ',')
} }

View File

@ -1078,3 +1078,22 @@ func TestNewPointEscaped(t *testing.T) {
t.Errorf("NewPoint().String() mismatch.\ngot %v\nexp %v", pt.String(), exp) 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)
}
}