more cleanup on client.Point. testing for json.Marshal(client.Point)

pull/1868/head
Cory LaNou 2015-03-06 12:04:26 -07:00
parent 4f5ad7399e
commit d334bec35a
3 changed files with 65 additions and 16 deletions

View File

@ -249,27 +249,30 @@ func (t Timestamp) MarshalJSON() ([]byte, error) {
// Point defines the fields that will be written to the database
type Point struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Timestamp time.Time `json:"timestamp"`
Fields map[string]interface{} `json:"fields"`
Precision string `json:"precision"`
Name string
Tags map[string]string
Timestamp time.Time
Fields map[string]interface{}
Precision string
}
// MarshalJSON will format the time in RFC3339Nano
// Precision is also ignored as it is only used for writing, not reading
// Or another way to say it is we always send back in nanosecond precision
func (p *Point) MarshalJSON() ([]byte, error) {
point := struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Timestamp string `json:"timestamp"`
Tags map[string]string `json:"tags,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Fields map[string]interface{} `json:"fields"`
Precision string `json:"precision"`
}{
Name: p.Name,
Tags: p.Tags,
Timestamp: p.Timestamp.UTC().Format(time.RFC3339Nano),
Fields: p.Fields,
Precision: p.Precision,
Name: p.Name,
Tags: p.Tags,
Fields: p.Fields,
}
// Let it omit empty if it's really zero
if !p.Timestamp.IsZero() {
point.Timestamp = p.Timestamp.UTC().Format(time.RFC3339Nano)
}
return json.Marshal(&point)
}

View File

@ -303,6 +303,52 @@ func TestPoint_UnmarshalRFC(t *testing.T) {
}
}
func TestPoint_MarshalOmitempty(t *testing.T) {
now := time.Now().UTC()
tests := []struct {
name string
point client.Point
now time.Time
expected string
}{
{
name: "all empty",
point: client.Point{Name: "cpu", Fields: map[string]interface{}{"value": 1.1}},
now: now,
expected: `{"name":"cpu","fields":{"value":1.1}}`,
},
{
name: "with time",
point: client.Point{Name: "cpu", Fields: map[string]interface{}{"value": 1.1}, Timestamp: now},
now: now,
expected: fmt.Sprintf(`{"name":"cpu","timestamp":"%s","fields":{"value":1.1}}`, now.Format(time.RFC3339Nano)),
},
{
name: "with tags",
point: client.Point{Name: "cpu", Fields: map[string]interface{}{"value": 1.1}, Tags: map[string]string{"foo": "bar"}},
now: now,
expected: `{"name":"cpu","tags":{"foo":"bar"},"fields":{"value":1.1}}`,
},
{
name: "with precision",
point: client.Point{Name: "cpu", Fields: map[string]interface{}{"value": 1.1}, Precision: "ms"},
now: now,
expected: `{"name":"cpu","fields":{"value":1.1}}`,
},
}
for _, test := range tests {
t.Logf("testing %q\n", test.name)
b, err := json.Marshal(&test.point)
if err != nil {
t.Fatalf("unexpected error. exptected: %v, actual: %v", nil, err)
}
if test.expected != string(b) {
t.Fatalf("Unexpected result. expected: %v, actual: %v", test.expected, string(b))
}
}
}
func TestEpochToTime(t *testing.T) {
now := time.Now()

View File

@ -60,11 +60,11 @@ func TestNormalizeBatchPoints(t *testing.T) {
t.Logf("running test %q", test.name)
p, e := influxdb.NormalizeBatchPoints(test.bp)
if test.err == "" && e != nil {
t.Error("unexpected error %s", e)
t.Errorf("unexpected error %v", e)
} else if test.err != "" && e == nil {
t.Error("expected error %s, got <nil>", test.err)
t.Errorf("expected error %s, got <nil>", test.err)
} else if e != nil && test.err != e.Error() {
t.Error("unexpected error. expected: %s, got %s", test.err, e)
t.Errorf("unexpected error. expected: %s, got %v", test.err, e)
}
if !reflect.DeepEqual(p, test.p) {
t.Logf("expected: %+v", test.p)