Skip to content

Commit

Permalink
Merge pull request #7 from yukithm/fix/issue-6
Browse files Browse the repository at this point in the history
Fix a problem when the key contains whitespaces (issue #6)
  • Loading branch information
yukithm authored Oct 1, 2020
2 parents 4bd1834 + f7f18b0 commit 5e2469f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.12.x
- 1.15.x
- tip
sudo: false
env:
Expand Down
42 changes: 42 additions & 0 deletions csv_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package json2csv_test

import (
"bytes"
"testing"

"github.com/yukithm/json2csv"
)

func TestKeyWithTrailingSpace(t *testing.T) {
b := &bytes.Buffer{}
wr := json2csv.NewCSVWriter(b)
responses := []map[string]interface{}{
{
" A": 1,
"B ": "foo",
"C ": "FOO",
},
{
" A": 2,
"B ": "bar",
"C ": "BAR",
},
}

csvContent, err := json2csv.JSON2CSV(responses) // csvContent seems to be complete!
if err != nil {
t.Fatal(err)
}
wr.WriteCSV(csvContent)
wr.Flush()

got := b.String()
want := `/ A,/B ,/C
1,foo,FOO
2,bar,BAR
`

if got != want {
t.Errorf("Expected %v, but %v", want, got)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/yukithm/json2csv

go 1.12
go 1.15

require (
github.com/mitchellh/gox v1.0.1
Expand Down
1 change: 0 additions & 1 deletion jsonpointer/jsonpointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type JSONPointer []Token

// New parses a pointer string and creates a new JSONPointer.
func New(pointer string) (JSONPointer, error) {
pointer = strings.TrimSpace(pointer)
if pointer == "" {
return JSONPointer{}, nil
}
Expand Down
27 changes: 26 additions & 1 deletion jsonpointer/jsonpointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var testNewCases = []struct {
{`/foo~1bar`, []Token{`foo/bar`}, ``},
{`/foo/bar`, []Token{`foo`, `bar`}, ``},
{`/foo/0/bar`, []Token{`foo`, `0`, `bar`}, ``},
{`/foo `, []Token{`foo `}, ``},
{`/ foo`, []Token{` foo`}, ``},
{`/ foo `, []Token{` foo `}, ``},
{`/foo / bar `, []Token{`foo `, ` bar `}, ``},
{`/`, []Token{""}, ``}, // empty string key
{`//`, []Token{"", ""}, ``}, // empty string key
{``, []Token{}, ``}, // whole content (root)
Expand Down Expand Up @@ -70,6 +74,11 @@ var testAppendCases = []struct {
{`/foo~1bar`, `append`, `/foo~1bar/append`},
{`/foo/bar`, `append`, `/foo/bar/append`},
{`/foo/0/bar`, `append`, `/foo/0/bar/append`},
{`/foo`, `append `, `/foo/append `},
{`/foo`, ` append`, `/foo/ append`},
{`/foo`, ` append `, `/foo/ append `},
{`/foo `, `append`, `/foo /append`},
{`/ foo`, `append`, `/ foo/append`},
{`/`, `append`, `//append`},
{`//`, `append`, `///append`},
{``, `append`, `/append`},
Expand Down Expand Up @@ -99,6 +108,9 @@ var testPopCases = []struct {
{`/foo~1bar`, `foo/bar`, ``},
{`/foo/bar`, `bar`, `/foo`},
{`/foo/0/bar`, `bar`, `/foo/0`},
{`/ foo `, ` foo `, ``},
{`/foo/ bar `, ` bar `, `/foo`},
{`/ foo / bar `, ` bar `, `/ foo `},
{`/`, ``, ``},
{`//`, ``, `/`},
{``, ``, ``},
Expand Down Expand Up @@ -150,6 +162,8 @@ var testStringsCases = []struct {
{`/foo~1bar`, []string{`foo/bar`}},
{`/foo/bar`, []string{`foo`, `bar`}},
{`/foo/0/bar`, []string{`foo`, `0`, `bar`}},
{`/ foo `, []string{` foo `}},
{`/ foo / bar `, []string{` foo `, ` bar `}},
{`/`, []string{""}}, // empty string key
{`//`, []string{"", ""}}, // empty string key
{``, []string{}}, // whole content (root)
Expand Down Expand Up @@ -177,6 +191,8 @@ var testEscapedStringsCases = []struct {
{`/foo~1bar`, []string{`foo~1bar`}},
{`/foo/bar`, []string{`foo`, `bar`}},
{`/foo/0/bar`, []string{`foo`, `0`, `bar`}},
{`/ foo `, []string{` foo `}},
{`/ foo / bar `, []string{` foo `, ` bar `}},
{`/`, []string{""}}, // empty string key
{`//`, []string{"", ""}}, // empty string key
{``, []string{}}, // whole content (root)
Expand Down Expand Up @@ -204,6 +220,8 @@ var testStringCases = []struct {
{`/foo~1bar`, `/foo~1bar`},
{`/foo/bar`, `/foo/bar`},
{`/foo/0/bar`, `/foo/0/bar`},
{`/ foo `, `/ foo `},
{`/ foo / bar `, `/ foo / bar `},
{`/`, `/`}, // empty string key
{`//`, `//`}, // empty string key
{``, ``}, // whole content (root)
Expand Down Expand Up @@ -232,6 +250,9 @@ var testDotNotationCases = []struct {
{`/foo~1bar`, `foo/bar`, `foo/bar`},
{`/foo/bar`, `foo.bar`, `foo.bar`},
{`/foo/0/bar`, `foo.0.bar`, `foo[0].bar`},
{`/ foo `, ` foo `, ` foo `},
{`/ foo / bar `, ` foo . bar `, ` foo . bar `},
{`/ foo /0/ bar `, ` foo .0. bar `, ` foo [0]. bar `},
{`/`, ``, ``}, // empty string key
{`//`, `.`, `.`}, // empty string key
{``, ``, ``}, // whole content (root)
Expand Down Expand Up @@ -263,7 +284,10 @@ var testGetJSON = `{
},
"foo/bar": 1.23,
"bar": true,
"baz": null
"baz": null,
" foo ": {
" bar ": 456
}
}`
var testGetCases = []struct {
pointer string
Expand All @@ -278,6 +302,7 @@ var testGetCases = []struct {
{`/bar`, true, ``},
{`/baz`, nil, ``},
{`/boo`, nil, `Invalid JSON Pointer "/boo"`},
{`/ foo / bar `, 456.0, ``},
}

func TestGet(t *testing.T) {
Expand Down

0 comments on commit 5e2469f

Please sign in to comment.