Przeglądaj źródła

Merge pull request #289 from rliebz/null-fix

Support usage of "~" to indicate null values for pointer types
Roger Peppe 8 lat temu
rodzic
commit
2c9db3558b
2 zmienionych plików z 16 dodań i 1 usunięć
  1. 1 1
      decode.go
  2. 15 0
      decode_test.go

+ 1 - 1
decode.go

@@ -251,7 +251,7 @@ func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
 //
 // If n holds a null value, prepare returns before doing anything.
 func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
-	if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "" && n.implicit) {
+	if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "~" || n.value == "" && n.implicit) {
 		return out, false, false
 	}
 	again := true

+ 15 - 0
decode_test.go

@@ -438,6 +438,9 @@ var unmarshalTests = []struct {
 	{
 		"foo: ''",
 		map[string]*string{"foo": new(string)},
+	}, {
+		"foo: null",
+		map[string]*string{"foo": nil},
 	}, {
 		"foo: null",
 		map[string]string{"foo": ""},
@@ -446,6 +449,18 @@ var unmarshalTests = []struct {
 		map[string]interface{}{"foo": nil},
 	},
 
+	// Support for ~
+	{
+		"foo: ~",
+		map[string]*string{"foo": nil},
+	}, {
+		"foo: ~",
+		map[string]string{"foo": ""},
+	}, {
+		"foo: ~",
+		map[string]interface{}{"foo": nil},
+	},
+
 	// Ignored field
 	{
 		"a: 1\nb: 2\n",