File tree Expand file tree Collapse file tree 10 files changed +173
-34
lines changed
Expand file tree Collapse file tree 10 files changed +173
-34
lines changed Original file line number Diff line number Diff line change 11package formatters
22
33import (
4- "fmt "
4+ "encoding/json "
55
6- "github.com/nytlabs/mxj"
76 "github.com/stilvoid/please/util"
87)
98
109func formatJSON (in interface {}) (string , error ) {
1110 in = util .ForceStringKeys (in )
1211
13- inMap , ok := in .( map [ string ] interface {} )
12+ bytes , err := json . MarshalIndent ( in , "" , " " )
1413
15- if ! ok {
16- return fmt .Sprintf ("\" %s\" " , in ), nil
17- }
18-
19- m := mxj .Map (inMap )
20-
21- bytes , err := m .JsonIndent ("" , " " )
22-
23- if err != nil {
24- return "" , err
25- }
26-
27- return string (bytes ), nil
14+ return string (bytes ), err
2815}
Original file line number Diff line number Diff line change 1+ package formatters
2+
3+ import "testing"
4+
5+ func TestJSON (t * testing.T ) {
6+ expecteds := []string {
7+ "123" ,
8+ "456.789" ,
9+ "\" abc\" " ,
10+ "true" ,
11+ "false" ,
12+ "null" ,
13+ "[\n 123,\n \" abc\" \n ]" ,
14+ "{\n \" foo\" : \" bar\" \n }" ,
15+ "{\n \" 123\" : [\n \" baz\" ,\n \" quux\" \n ]\n }" ,
16+ "{\n \" true\" : {\n \" null\" : null\n }\n }" ,
17+ "[\n 456,\n \" def\" ,\n {\n \" 3\" : 4\n }\n ]" ,
18+ }
19+
20+ if len (expecteds ) != len (testCases ) {
21+ t .Fatalf ("insufficient test cases implemented" )
22+ }
23+
24+ for i , expected := range expecteds {
25+ testCase := testCases [i ]
26+
27+ actual , err := formatJSON (testCase )
28+
29+ if err != nil {
30+ t .Errorf ("unexpected error: %v" , err )
31+ }
32+
33+ if actual != expected {
34+ t .Errorf ("unexpected '%v', want '%v'" , actual , expected )
35+ }
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package formatters
2+
3+ var testCases = []interface {}{
4+ 123 ,
5+ 456.789 ,
6+ "abc" ,
7+ true ,
8+ false ,
9+ nil ,
10+ []interface {}{123 , "abc" },
11+ map [interface {}]interface {}{ // A map
12+ "foo" : "bar" , // To a value
13+ },
14+ map [interface {}]interface {}{ // A map
15+ 123 : []interface {}{"baz" , "quux" }, // To an array
16+ },
17+ map [interface {}]interface {}{ // A map
18+ true : map [interface {}]interface {}{ // To another map
19+ nil : nil ,
20+ },
21+ },
22+ []interface {}{ // An array
23+ 456 , // Of values
24+ "def" ,
25+ map [interface {}]interface {}{ // With a map
26+ 3 : 4 ,
27+ },
28+ },
29+ }
Original file line number Diff line number Diff line change 11package formatters
22
33import (
4+ "reflect"
5+
46 "github.com/clbanning/anyxml"
57 "github.com/stilvoid/please/util"
68)
79
810func formatXML (in interface {}) (string , error ) {
911 in = util .ForceStringKeys (in )
1012
13+ if reflect .TypeOf (in ) == nil {
14+ in = ""
15+ }
16+
1117 bytes , err := anyxml .XmlIndent (in , "" , " " )
1218
1319 if err != nil {
Original file line number Diff line number Diff line change 1+ package formatters
2+
3+ import "testing"
4+
5+ func TestXML (t * testing.T ) {
6+ expecteds := []string {
7+ "<doc>123</doc>" ,
8+ "<doc>456.789</doc>" ,
9+ "<doc>abc</doc>" ,
10+ "<doc>true</doc>" ,
11+ "<doc>false</doc>" ,
12+ "<doc></doc>" ,
13+ "<doc>\n <element>123</element>\n <element>abc</element>\n </doc>" ,
14+ "<foo>bar</foo>" ,
15+ "<doc>\n <123>baz</123>\n <123>quux</123>\n </doc>" ,
16+ "<true>\n <null/>\n </true>" ,
17+ "<doc>\n <element>456</element>\n <element>def</element>\n <3>4</3>\n </doc>" ,
18+ }
19+
20+ if len (expecteds ) != len (testCases ) {
21+ //t.Fatalf("insufficient test cases implemented")
22+ }
23+
24+ for i , expected := range expecteds {
25+ testCase := testCases [i ]
26+
27+ actual , err := formatXML (testCase )
28+
29+ if err != nil {
30+ t .Errorf ("unexpected error: %v" , err )
31+ }
32+
33+ if actual != expected {
34+ t .Errorf ("unexpected '%v', want '%v'" , actual , expected )
35+ }
36+ }
37+ }
Original file line number Diff line number Diff line change 11package formatters
22
3- import (
4- "github.com/stilvoid/please/util"
5- "gopkg.in/yaml.v2"
6- )
3+ import "gopkg.in/yaml.v2"
74
85func formatYAML (in interface {}) (string , error ) {
9- in = util .ForceStringKeys (in )
10-
116 bytes , err := yaml .Marshal (in )
127
13- if err != nil {
14- return "" , err
15- }
16-
17- return string (bytes ), nil
8+ // We strip off the trailing newline that yaml.v2 seems to insist on
9+ return string (bytes [:len (bytes )- 1 ]), err
1810}
Original file line number Diff line number Diff line change 1+ package formatters
2+
3+ import "testing"
4+
5+ func TestYAML (t * testing.T ) {
6+ expecteds := []string {
7+ "123" ,
8+ "456.789" ,
9+ "abc" ,
10+ "true" ,
11+ "false" ,
12+ "null" ,
13+ "- 123\n - abc" ,
14+ "foo: bar" ,
15+ "123:\n - baz\n - quux" ,
16+ "true:\n null: null" ,
17+ "- 456\n - def\n - 3: 4" ,
18+ }
19+
20+ if len (expecteds ) != len (testCases ) {
21+ t .Fatalf ("insufficient test cases implemented" )
22+ }
23+
24+ for i , expected := range expecteds {
25+ testCase := testCases [i ]
26+
27+ actual , err := formatYAML (testCase )
28+
29+ if err != nil {
30+ t .Errorf ("unexpected error: %v" , err )
31+ }
32+
33+ if actual != expected {
34+ t .Errorf ("unexpected '%v', want '%v'" , actual , expected )
35+ }
36+ }
37+ }
Original file line number Diff line number Diff line change @@ -12,17 +12,22 @@ func TestFilter(t *testing.T) {
1212 "top" : "I am the top" ,
1313 "bottom" : map [int ]interface {}{
1414 16 : "left" ,
15- 13 : []string {
15+ 13 : []interface {} {
1616 "right 1" ,
17- "right 2" ,
17+ map [string ]interface {}{
18+ "deeper" : "we go" ,
19+ },
1820 },
1921 },
2022 }
2123
2224 cases := map [string ]interface {}{
2325 "top" : "I am the top" ,
24- "bottom.16" : "left" ,
25- "bottom.13.1" : "right 2" ,
26+ "bottom" : input ["bottom" ],
27+ "bottom.16" : input ["bottom" ].(map [int ]interface {})[16 ],
28+ "bottom.13" : input ["bottom" ].(map [int ]interface {})[13 ],
29+ "bottom.13.0" : input ["bottom" ].(map [int ]interface {})[13 ].([]interface {})[0 ],
30+ "bottom.13.1" : input ["bottom" ].(map [int ]interface {})[13 ].([]interface {})[1 ],
2631 }
2732
2833 for path , expected := range cases {
@@ -32,7 +37,7 @@ func TestFilter(t *testing.T) {
3237 t .Fail ()
3338 }
3439
35- if expected != actual {
40+ if ! reflect . DeepEqual ( actual , expected ) {
3641 t .Errorf ("case failed: %v vs %v" , expected , actual )
3742 }
3843 }
Original file line number Diff line number Diff line change @@ -14,8 +14,15 @@ func ForceStringKeys(in interface{}) interface{} {
1414 case reflect .Map :
1515 newMap := make (map [string ]interface {}, val .Len ())
1616
17+ var stringKey string
18+
1719 for _ , key := range val .MapKeys () {
18- stringKey := fmt .Sprint (key .Interface ())
20+ if reflect .TypeOf (key .Interface ()) == nil {
21+ stringKey = "null"
22+ } else {
23+ stringKey = fmt .Sprint (key .Interface ())
24+ }
25+
1926 newMap [stringKey ] = ForceStringKeys (val .MapIndex (key ).Interface ())
2027 }
2128
Original file line number Diff line number Diff line change 66)
77
88func TestForceStringKeys (t * testing.T ) {
9- input := map [int ]interface {}{
9+ input := map [interface {} ]interface {}{
1010 13 : []interface {}{
1111 "foo" ,
1212 map [int ]interface {}{
@@ -18,6 +18,7 @@ func TestForceStringKeys(t *testing.T) {
1818 1 : []interface {}{"foo" , "bar" },
1919 2 : "two" ,
2020 },
21+ nil : "derf" ,
2122 }
2223
2324 expected := map [string ]interface {}{
@@ -32,6 +33,7 @@ func TestForceStringKeys(t *testing.T) {
3233 "1" : []interface {}{"foo" , "bar" },
3334 "2" : "two" ,
3435 },
36+ "null" : "derf" ,
3537 }
3638
3739 actual := ForceStringKeys (input )
You can’t perform that action at this time.
0 commit comments