Skip to content

Commit

Permalink
Support AND and OR queries with a more programmer friendly API
Browse files Browse the repository at this point in the history
  • Loading branch information
maxott committed Nov 22, 2021
1 parent 0ec08de commit 7c584bd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
28 changes: 19 additions & 9 deletions cmd/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ func init() {

func cliRecordList(topCmd *kingpin.CmdClause) {
r := &record.ListRequest{Offset: -1, Limit: -1}
var andQueries []string
var orQueries []string
c := topCmd.Command("list", "List some records").Action(func(_ *kingpin.ParseContext) error {
// if rec, err := record.List(r, Adapter()); err != nil {
// return err;
// } else {
// fmt.Printf("List: %+v\n", rec)
// return nil
// }
if pyld, err := record.ListRaw(r, Adapter(), Logger()); err != nil {
rq := r.AndQuery
for _, q := range andQueries {
rq = append(rq, record.NewQueryTermS(q))
}
r.AndQuery = rq

rq = r.OrQuery
for _, q := range orQueries {
rq = append(rq, record.NewQueryTermS(q))
}
r.OrQuery = rq

if pyld, err := record.ListRaw(context.Background(), r, Adapter(), Logger()); err != nil {
return err
} else {
return adapter.ReplyPrinter(pyld, *useYaml)
Expand All @@ -39,9 +47,11 @@ func cliRecordList(topCmd *kingpin.CmdClause) {
c.Flag("aspects", "The aspects for which to retrieve data").
Short('a').
StringVar(&r.Aspects)
c.Flag("query", "Record Name").
c.Flag("and-query", "Record Name").
Short('q').
StringVar(&r.Query)
StringsVar(&andQueries)
c.Flag("or-query", "Record Name").
StringsVar(&orQueries)
c.Flag("offset", "Index of first record retrieved").
Short('o').
IntVar(&r.Offset)
Expand Down
54 changes: 54 additions & 0 deletions pkg/record/record_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package record

import (
"encoding/json"
_ "fmt"
_ "regexp"
"testing"
)

func TestListQuery(t *testing.T) {
qt := QueryTerm{
Path: "foo",
Op: Equal,
Value: "a",
}
p := qt.asUrlQuery()
exp := "foo%3Aa"
if exp != p {
t.Errorf("expected '%s', but got '%s'", exp, p)
}
}

func TestListQuery2(t *testing.T) {
qt := QueryTerm{
Path: "foo",
Op: Equal,
Value: "a:b",
}
p := qt.asUrlQuery()
exp := "foo%3Aa%253Ab"
if exp != p {
t.Errorf("expected '%s', but got '%s'", exp, p)
}
}

func TestListQueryJson(t *testing.T) {
j := `{
"path": "asp.foo",
"op": "=",
"value": "a:b"
}`
qt := QueryTerm{}
err := json.Unmarshal([]byte(j), &qt)
if err != nil {
t.Errorf("while unmarshal query - %v", err)
return
}

p := qt.asUrlQuery()
exp := "asp.foo%3Aa%253Ab"
if exp != p {
t.Errorf("expected '%s', but got '%s'", exp, p)
}
}

0 comments on commit 7c584bd

Please sign in to comment.