Skip to content

Commit

Permalink
Print results in tabulated format (#351)
Browse files Browse the repository at this point in the history
* Print results in tabulated format

* Address code review comments

* Add unit test

* Reduce lines

* Escape input

* Rename variable

* Update README

* Handle nil values correctly

* Add test coverage
  • Loading branch information
jianli authored and MaxJTRice committed Jul 25, 2018
1 parent aa32211 commit 1852e75
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## v3.1.1 (unreleased)
- Print CLI results in tabulated format (#351)

## v3.1.0 (2018-07-12)
- Add functionality to not invalidate cache when upsert (#341)
- Add query (read/range) support to CLI (#342)
Expand Down
4 changes: 1 addition & 3 deletions cmd/dosa/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ func (c *QueryCmd) doQueryOp(f func(ShellQueryClient, context.Context, []*queryO
return err
}

printResult(results)

return nil
return printResults(results)
}

// QueryRead holds the options for 'query read'
Expand Down
2 changes: 1 addition & 1 deletion cmd/dosa/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestQuery_Range_Happy(t *testing.T) {
assert.Len(t, columnConditions, 1)
assert.Len(t, columnConditions["int64key"], 1)
assert.Equal(t, []string{"strkey", "int64key"}, minimumFields)
}).Return([]map[string]dosa.FieldValue{}, "", nil)
}).Return([]map[string]dosa.FieldValue{{"key": "value"}}, "", nil)

mc.EXPECT().Shutdown().Return(nil)

Expand Down
54 changes: 45 additions & 9 deletions cmd/dosa/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ package main

import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -114,16 +117,49 @@ func strToFieldValue(t dosa.Type, s string) (dosa.FieldValue, error) {
}
}

func printResult(res []map[string]dosa.FieldValue) {
if len(res) == 0 {
fmt.Println("Error: not found")
return
func getFields(results []map[string]dosa.FieldValue) []string {
fieldSet := make(map[string]bool)
for _, result := range results {
for field := range result {
fieldSet[field] = true
}
}
var fields []string
for field := range fieldSet {
fields = append(fields, field)
}
sort.Strings(fields)
return fields
}

func printResults(results []map[string]dosa.FieldValue) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', tabwriter.Debug|tabwriter.StripEscape)
if len(results) == 0 {
return errors.New("Empty results")
}
fields := getFields(results)
if _, err := fmt.Fprintln(w, strings.Join(fields, "\t")); err != nil {
return errors.WithStack(err)
}
fmt.Println("Results:")
for _, fieldValues := range res {
for field, value := range fieldValues {
fmt.Printf("%s: %v\n", field, reflect.Indirect(reflect.ValueOf(value)))
values := make([]string, len(fields))
for _, result := range results {
for idx, field := range fields {
var value string
if _, ok := result[field]; ok {
value = fmt.Sprintf(
"%s%v%s",
[]byte{tabwriter.Escape},
reflect.Indirect(reflect.ValueOf(result[field])),
[]byte{tabwriter.Escape},
)
} else {
value = "nil"
}
values[idx] = value
}
if _, err := fmt.Fprintln(w, strings.Join(values, "\t")); err != nil {
return errors.WithStack(err)
}
fmt.Printf("\n")
}
return w.Flush()
}
25 changes: 25 additions & 0 deletions cmd/dosa/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ func TestSetQueryFieldValues(t *testing.T) {
assert.Contains(t, err.Error(), "is not a valid field for")
}

func TestGetfields(t *testing.T) {
fields := []map[string]dosa.FieldValue{
{"id": dosa.FieldValue(int32(1)), "name": dosa.FieldValue("foo")},
{"name": dosa.FieldValue("bar"), "address": dosa.FieldValue("bar")},
}
assert.Equal(t, getFields(fields), []string{"address", "id", "name"})
}

func TestPrintResults(t *testing.T) {
results := []map[string]dosa.FieldValue{
{
"id": dosa.FieldValue(int32(1)),
"uuid": dosa.FieldValue(dosa.UUID("3e4befa0-69d3-11e8-95b0-d55aa227a290")),
},
}
err := printResults(results)
assert.NoError(t, err)
}

func TestPrintResultsEmpty(t *testing.T) {
results := []map[string]dosa.FieldValue{}
err := printResults(results)
assert.Error(t, err)
}

func TestStrToFieldValue(t *testing.T) {
tStr := "2018-06-11T13:03:01Z"
ts, _ := time.Parse(time.RFC3339, tStr)
Expand Down

0 comments on commit 1852e75

Please sign in to comment.