From 6b9257fe564f41505d71e2a9a0e50d72c2b02c80 Mon Sep 17 00:00:00 2001 From: Mithrandie Date: Mon, 10 Jul 2017 19:36:44 +0900 Subject: [PATCH 1/2] Fix output encoding bugs. --- lib/query/encode.go | 15 +++++++++++++-- lib/query/encode_test.go | 15 +++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/query/encode.go b/lib/query/encode.go index 22b1a6d5..70e2a315 100644 --- a/lib/query/encode.go +++ b/lib/query/encode.go @@ -9,6 +9,7 @@ import ( "github.com/mithrandie/csvq/lib/cmd" "github.com/mithrandie/csvq/lib/parser" + "github.com/mithrandie/csvq/lib/ternary" ) var fullWidthTable = &unicode.RangeTable{ @@ -280,7 +281,12 @@ func formatCSVCell(c Cell) string { case parser.Boolean: s = strconv.FormatBool(primary.(parser.Boolean).Bool()) case parser.Ternary: - s = strconv.FormatBool(primary.(parser.Ternary).Bool()) + t := primary.(parser.Ternary) + if t.Ternary() == ternary.UNKNOWN { + s = "" + } else { + s = strconv.FormatBool(t.Bool()) + } case parser.Datetime: s = quote(escapeCSVString(primary.(parser.Datetime).Format())) case parser.Null: @@ -323,7 +329,12 @@ func formatJsonCell(c Cell) string { case parser.Boolean: s = strconv.FormatBool(primary.(parser.Boolean).Bool()) case parser.Ternary: - s = strconv.FormatBool(primary.(parser.Ternary).Bool()) + t := primary.(parser.Ternary) + if t.Ternary() == ternary.UNKNOWN { + s = "null" + } else { + s = strconv.FormatBool(t.Bool()) + } case parser.Datetime: s = quote(escapeJsonString(primary.(parser.Datetime).Format())) case parser.Null: diff --git a/lib/query/encode_test.go b/lib/query/encode_test.go index a0d018f3..3b0c0932 100644 --- a/lib/query/encode_test.go +++ b/lib/query/encode_test.go @@ -65,12 +65,14 @@ var encodeViewTests = []struct { Header: NewHeaderWithoutId("test", []string{"c1", "c2\nsecond line", "c3"}), Records: []Record{ NewRecordWithoutId([]parser.Primary{parser.NewInteger(-1), parser.NewTernary(ternary.UNKNOWN), parser.NewBoolean(true)}), + NewRecordWithoutId([]parser.Primary{parser.NewInteger(-1), parser.NewTernary(ternary.FALSE), parser.NewBoolean(true)}), NewRecordWithoutId([]parser.Primary{parser.NewFloat(2.0123), parser.NewDatetimeFromString("2016-02-01T16:00:00.123456-07:00"), parser.NewString("abcdef")}), NewRecordWithoutId([]parser.Primary{parser.NewInteger(34567890), parser.NewString(" abcdefghijklmnopqrstuvwxyzabcdefg\nhi\"jk\n"), parser.NewNull()}), }, }, Format: cmd.CSV, Result: "\"c1\",\"c2\nsecond line\",\"c3\"\n" + + "-1,,true\n" + "-1,false,true\n" + "2.0123,\"2016-02-01 16:00:00.123456\",\"abcdef\"\n" + "34567890,\" abcdefghijklmnopqrstuvwxyzabcdefg\nhi\"\"jk\n\",", @@ -88,7 +90,7 @@ var encodeViewTests = []struct { Format: cmd.TSV, WriteDelimiter: '\t', Result: "\"c1\"\t\"c2\nsecond line\"\t\"c3\"\n" + - "-1\tfalse\ttrue\n" + + "-1\t\ttrue\n" + "2.0123\t\"2016-02-01 16:00:00.123456\"\t\"abcdef\"\n" + "34567890\t\" abcdefghijklmnopqrstuvwxyzabcdefg\nhi\"\"jk\n\"\t", }, @@ -104,7 +106,7 @@ var encodeViewTests = []struct { }, Format: cmd.CSV, WithoutHeader: true, - Result: "-1,false,true\n" + + Result: "-1,,true\n" + "2.0123,\"2016-02-01 16:00:00.123456\",\"abcdef\"\n" + "34567890,\" abcdefghijklmnopqrstuvwxyzabcdefg\nhi\"\"jk\n\",", }, @@ -121,7 +123,7 @@ var encodeViewTests = []struct { Format: cmd.CSV, LineBreak: cmd.CRLF, Result: "\"c1\",\"c2\r\nsecond line\",\"c3\"\r\n" + - "-1,false,true\r\n" + + "-1,,true\r\n" + "2.0123,\"2016-02-01 16:00:00.123456\",\"abcdef\"\r\n" + "34567890,\" abcdefghijklmnopqrstuvwxyzabcdefg\r\nhi\"\"jk\r\n\",", }, @@ -131,12 +133,18 @@ var encodeViewTests = []struct { Header: NewHeaderWithoutId("test", []string{"c1", "c2\nsecond line", "c3"}), Records: []Record{ NewRecordWithoutId([]parser.Primary{parser.NewInteger(-1), parser.NewTernary(ternary.UNKNOWN), parser.NewBoolean(true)}), + NewRecordWithoutId([]parser.Primary{parser.NewInteger(-1), parser.NewTernary(ternary.FALSE), parser.NewBoolean(true)}), NewRecordWithoutId([]parser.Primary{parser.NewFloat(2.0123), parser.NewDatetimeFromString("2016-02-01T16:00:00.123456-07:00"), parser.NewString("abcdef")}), NewRecordWithoutId([]parser.Primary{parser.NewInteger(34567890), parser.NewString(" abc\\defghi/jklmn\topqrstuvwxyzabcdefg\nhi\"jk\n"), parser.NewNull()}), }, }, Format: cmd.JSON, Result: "[" + + "{" + + "\"c1\":-1," + + "\"c2\\nsecond line\":null," + + "\"c3\":true" + + "}," + "{" + "\"c1\":-1," + "\"c2\\nsecond line\":false," + @@ -190,7 +198,6 @@ func TestEncodeView(t *testing.T) { } if s != v.Result { t.Errorf("%s, result = %q, want %q", v.Name, s, v.Result) - t.Log(s) } } } From 93a8d472cf1f9f9b6934f49d1fb060462e33c05a Mon Sep 17 00:00:00 2001 From: Mithrandie Date: Mon, 10 Jul 2017 19:37:28 +0900 Subject: [PATCH 2/2] Update version for Release v0.2.10 --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 1bf97543..ac338636 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( "github.com/urfave/cli" ) -var version = "v0.2.9" +var version = "v0.2.10" func main() { cli.AppHelpTemplate = appHHelpTemplate