add custom json marshal for FieldRow

This commit is contained in:
Matt Jaffee 2018-10-24 17:11:45 -05:00
parent ad26eeca1b
commit 671420f31d
No known key found for this signature in database
GPG key ID: 08A3DFFF987B11BF
3 changed files with 50 additions and 2 deletions

View file

@ -16,6 +16,7 @@ package pilosa
import (
"context"
"encoding/json"
"fmt"
"sort"
"time"
@ -909,6 +910,25 @@ type FieldRow struct {
RowKey string `json:"rowKey,omitempty"`
}
func (fr FieldRow) MarshalJSON() ([]byte, error) {
if fr.RowKey != "" {
return json.Marshal(struct {
Field string `json:"field"`
RowKey string `json:"rowKey"`
}{
Field: fr.Field,
RowKey: fr.RowKey,
})
}
return json.Marshal(struct {
Field string `json:"field"`
RowID uint64 `json:"rowID"`
}{
Field: fr.Field,
RowID: fr.RowID,
})
}
func (fr FieldRow) String() string {
return fmt.Sprintf("%s.%d", fr.Field, fr.RowID)
}

View file

@ -1,6 +1,7 @@
package pilosa
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
@ -193,3 +194,31 @@ func TestFilterWithRows(t *testing.T) {
}
}
func TestFieldRowMarshalJSON(t *testing.T) {
fr := FieldRow{
Field: "blah",
RowID: 0,
RowKey: "ha",
}
b, err := json.Marshal(fr)
if err != nil {
t.Fatalf("marshalling fieldrow: %v", err)
}
if string(b) != `{"field":"blah","rowKey":"ha"}` {
t.Fatalf("unexpected json: %s", b)
}
fr = FieldRow{
Field: "blah",
RowID: 2,
RowKey: "",
}
b, err = json.Marshal(fr)
if err != nil {
t.Fatalf("marshalling fieldrow: %v", err)
}
if string(b) != `{"field":"blah","rowID":2}` {
t.Fatalf("unexpected json: %s", b)
}
}

View file

@ -11,7 +11,6 @@ message Row {
message RowIdentifiers {
repeated uint64 Rows = 1;
repeated string Keys = 2;
//repeated Attr Attrs = 3;
}
message Pair {
@ -81,7 +80,7 @@ message QueryResult {
uint64 N = 2;
repeated Pair Pairs = 3;
bool Changed = 4;
ValCount ValCount = 5;
ValCount ValCount = 5;
repeated uint64 RowIDs = 7;
repeated GroupCount GroupCounts = 8;
RowIdentifiers RowIdentifiers = 9;