From 216348fbdb64b0f8f756830388d093bc3f37a9e6 Mon Sep 17 00:00:00 2001 From: Yuki Date: Thu, 1 Oct 2020 17:00:21 +0900 Subject: [PATCH 1/2] Fix a problem when the key contains whitespaces (issue #6) Preserve whitespaces around the JSON key when processing JSON Pointer. --- csv_writer_test.go | 42 +++++++++++++++++++++++++++++++++ jsonpointer/jsonpointer.go | 1 - jsonpointer/jsonpointer_test.go | 27 ++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 csv_writer_test.go diff --git a/csv_writer_test.go b/csv_writer_test.go new file mode 100644 index 0000000..6cf09ec --- /dev/null +++ b/csv_writer_test.go @@ -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) + } +} diff --git a/jsonpointer/jsonpointer.go b/jsonpointer/jsonpointer.go index def7690..14e6fb1 100644 --- a/jsonpointer/jsonpointer.go +++ b/jsonpointer/jsonpointer.go @@ -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 } diff --git a/jsonpointer/jsonpointer_test.go b/jsonpointer/jsonpointer_test.go index cb6c94c..ab6905b 100644 --- a/jsonpointer/jsonpointer_test.go +++ b/jsonpointer/jsonpointer_test.go @@ -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) @@ -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`}, @@ -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 `}, {`/`, ``, ``}, {`//`, ``, `/`}, {``, ``, ``}, @@ -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) @@ -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) @@ -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) @@ -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) @@ -263,7 +284,10 @@ var testGetJSON = `{ }, "foo/bar": 1.23, "bar": true, - "baz": null + "baz": null, + " foo ": { + " bar ": 456 + } }` var testGetCases = []struct { pointer string @@ -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) { From f7f18b0994dfe76b10f7097744ec6fb6766b464f Mon Sep 17 00:00:00 2001 From: Yuki Date: Thu, 1 Oct 2020 17:17:52 +0900 Subject: [PATCH 2/2] Update go version --- .travis.yml | 2 +- go.mod | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9da66f2..3760860 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.12.x + - 1.15.x - tip sudo: false env: diff --git a/go.mod b/go.mod index 3f58eb4..85e4019 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/yukithm/json2csv -go 1.12 +go 1.15 require ( github.com/mitchellh/gox v1.0.1