Skip to content

Commit

Permalink
add custom json marshal for FieldRow
Browse files Browse the repository at this point in the history
  • Loading branch information
jaffee committed Oct 24, 2018
1 parent ad26eec commit 671420f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
20 changes: 20 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pilosa

import (
"context"
"encoding/json"
"fmt"
"sort"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 29 additions & 0 deletions executor_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pilosa

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
Expand Down Expand Up @@ -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)
}
}
3 changes: 1 addition & 2 deletions internal/public.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ message Row {
message RowIdentifiers {
repeated uint64 Rows = 1;
repeated string Keys = 2;
//repeated Attr Attrs = 3;
}

message Pair {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 671420f

Please sign in to comment.