File tree Expand file tree Collapse file tree 2 files changed +26
-12
lines changed Expand file tree Collapse file tree 2 files changed +26
-12
lines changed Original file line number Diff line number Diff line change @@ -18,19 +18,15 @@ import (
18
18
"bytes"
19
19
"errors"
20
20
"fmt"
21
- "strings "
21
+ "strconv "
22
22
23
23
"gopkg.in/yaml.v3"
24
24
)
25
25
26
- const indentation = " "
27
-
28
- // basic escaping, will need to be improved or replaced
29
- func escape (s string ) string {
30
- s = strings .Replace (s , "\n " , "\\ n" , - 1 )
31
- s = strings .Replace (s , "\" " , "\\ \" " , - 1 )
32
- return s
33
- }
26
+ const (
27
+ indentation = " "
28
+ null = "null"
29
+ )
34
30
35
31
type writer struct {
36
32
b bytes.Buffer
@@ -85,15 +81,15 @@ func (w *writer) writeScalar(node *yaml.Node, indent string) {
85
81
}
86
82
switch node .Tag {
87
83
case "!!str" :
88
- w .writeString ("\" " )
89
- w .writeString (escape (node .Value ))
90
- w .writeString ("\" " )
84
+ w .writeString (strconv .Quote (node .Value ))
91
85
case "!!int" :
92
86
w .writeString (node .Value )
93
87
case "!!float" :
94
88
w .writeString (node .Value )
95
89
case "!!bool" :
96
90
w .writeString (node .Value )
91
+ case "!!null" :
92
+ w .writeString (null )
97
93
}
98
94
}
99
95
Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ func TestMarshal(t *testing.T) {
37
37
scalarBoolTestCase (),
38
38
scalarFloatTestCase (),
39
39
scalarIntTestCase (),
40
+ scalarStringTestCase (),
41
+ scalarNullTestCase (),
40
42
sequenceStringArrayTestCase (),
41
43
sequenceBoolArrayTestCase (),
42
44
sequenceFloatArrayTestCase (),
@@ -88,6 +90,22 @@ func scalarFloatTestCase() *MarshalTestCase {
88
90
}
89
91
}
90
92
93
+ func scalarStringTestCase () * MarshalTestCase {
94
+ return & MarshalTestCase {
95
+ Name : "scalar string" ,
96
+ Node : compiler .NewScalarNodeForString ("a\\ b\n c\" " ),
97
+ Expected : "\" a\\ \\ b\\ nc\\ \" \" \n " ,
98
+ }
99
+ }
100
+
101
+ func scalarNullTestCase () * MarshalTestCase {
102
+ return & MarshalTestCase {
103
+ Name : "scalar null" ,
104
+ Node : compiler .NewNullNode (),
105
+ Expected : "null\n " ,
106
+ }
107
+ }
108
+
91
109
func scalarIntTestCase () * MarshalTestCase {
92
110
return & MarshalTestCase {
93
111
Name : "scalar int" ,
You can’t perform that action at this time.
0 commit comments