Browse Source

Marshal top-level nils correctly.

Merge rogpeppe's branch '001-allow-nil-marshal'.
Gustavo Niemeyer 11 years ago
parent
commit
b6b591a3c0
2 changed files with 10 additions and 2 deletions
  1. 4 0
      encode.go
  2. 6 2
      encode_test.go

+ 4 - 0
encode.go

@@ -58,6 +58,10 @@ func (e *encoder) must(ok bool) {
 }
 
 func (e *encoder) marshal(tag string, in reflect.Value) {
+	if !in.IsValid() {
+		e.nilv()
+		return
+	}
 	if m, ok := in.Interface().(Marshaler); ok {
 		v, err := m.MarshalYAML()
 		if err != nil {

+ 6 - 2
encode_test.go

@@ -2,12 +2,13 @@ package yaml_test
 
 import (
 	"fmt"
-	. "gopkg.in/check.v1"
-	"gopkg.in/yaml.v2"
 	"math"
 	"strconv"
 	"strings"
 	"time"
+
+	. "gopkg.in/check.v1"
+	"gopkg.in/yaml.v2"
 )
 
 var marshalIntTest = 123
@@ -17,6 +18,9 @@ var marshalTests = []struct {
 	data  string
 }{
 	{
+		nil,
+		"null\n",
+	}, {
 		&struct{}{},
 		"{}\n",
 	}, {