diff --git a/executor.go b/executor.go index 82d2135ab..7d71088ba 100644 --- a/executor.go +++ b/executor.go @@ -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) } diff --git a/executor_internal_test.go b/executor_internal_test.go index 93ba86743..629d704db 100644 --- a/executor_internal_test.go +++ b/executor_internal_test.go @@ -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) + } +} diff --git a/internal/public.proto b/internal/public.proto index a102f1088..229fcfadd 100644 --- a/internal/public.proto +++ b/internal/public.proto @@ -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;