mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
add custom json marshal for FieldRow
This commit is contained in:
parent
ad26eeca1b
commit
671420f31d
3 changed files with 50 additions and 2 deletions
20
executor.go
20
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue