Procházet zdrojové kódy

Drop MapSlice in favor of general Node support.

Gustavo Niemeyer před 7 roky
rodič
revize
b804f79fbc
5 změnil soubory, kde provedl 9 přidání a 79 odebrání
  1. 3 46
      decode.go
  2. 0 6
      decode_test.go
  3. 5 11
      encode.go
  4. 1 7
      encode_test.go
  5. 0 9
      yaml.go

+ 3 - 46
decode.go

@@ -338,7 +338,6 @@ type decoder struct {
 
 var (
 	nodeType       = reflect.TypeOf(Node{})
-	mapItemType    = reflect.TypeOf(MapItem{})
 	durationType   = reflect.TypeOf(time.Duration(0))
 	defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
 	ifaceType      = defaultMapType.Elem()
@@ -685,23 +684,12 @@ func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
 	switch out.Kind() {
 	case reflect.Struct:
 		return d.mappingStruct(n, out)
-	case reflect.Slice:
-		return d.mappingSlice(n, out)
 	case reflect.Map:
 		// okay
 	case reflect.Interface:
-		if d.mapType.Kind() == reflect.Map {
-			iface := out
-			out = reflect.MakeMap(d.mapType)
-			iface.Set(out)
-		} else {
-			slicev := reflect.New(d.mapType).Elem()
-			if !d.mappingSlice(n, slicev) {
-				return false
-			}
-			out.Set(slicev)
-			return true
-		}
+		iface := out
+		out = reflect.MakeMap(d.mapType)
+		iface.Set(out)
 	default:
 		d.terror(n, yaml_MAP_TAG, out)
 		return false
@@ -751,37 +739,6 @@ func (d *decoder) setMapIndex(n *Node, out, k, v reflect.Value) {
 	out.SetMapIndex(k, v)
 }
 
-func (d *decoder) mappingSlice(n *Node, out reflect.Value) (good bool) {
-	outt := out.Type()
-	if outt.Elem() != mapItemType {
-		d.terror(n, yaml_MAP_TAG, out)
-		return false
-	}
-
-	mapType := d.mapType
-	d.mapType = outt
-
-	var slice []MapItem
-	var l = len(n.Children)
-	for i := 0; i < l; i += 2 {
-		if isMerge(n.Children[i]) {
-			d.merge(n.Children[i+1], out)
-			continue
-		}
-		item := MapItem{}
-		k := reflect.ValueOf(&item.Key).Elem()
-		if d.unmarshal(n.Children[i], k) {
-			v := reflect.ValueOf(&item.Value).Elem()
-			if d.unmarshal(n.Children[i+1], v) {
-				slice = append(slice, item)
-			}
-		}
-	}
-	out.Set(reflect.ValueOf(slice))
-	d.mapType = mapType
-	return true
-}
-
 func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
 	sinfo, err := getStructInfo(out.Type())
 	if err != nil {

+ 0 - 6
decode_test.go

@@ -551,12 +551,6 @@ var unmarshalTests = []struct {
 		map[string]string{"a": strings.Repeat("\x00", 52)},
 	},
 
-	// Ordered maps.
-	{
-		"{b: 2, a: 1, d: 4, c: 3, sub: {e: 5}}",
-		&yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
-	},
-
 	// Issue #39.
 	{
 		"a:\n b:\n  c: d\n",

+ 5 - 11
encode.go

@@ -73,7 +73,11 @@ func (e *encoder) must(ok bool) {
 
 func (e *encoder) marshalDoc(tag string, in reflect.Value) {
 	e.init()
-	if node, ok := in.Interface().(*Node); ok && node.Kind == DocumentNode {
+	var node *Node
+	if in.IsValid() {
+		node, _ = in.Interface().(*Node)
+	}
+	if node != nil && node.Kind == DocumentNode {
 		e.nodev(in)
 	} else {
 		yaml_document_start_event_initialize(&e.event, nil, nil, true)
@@ -160,16 +164,6 @@ func (e *encoder) mapv(tag string, in reflect.Value) {
 	})
 }
 
-func (e *encoder) itemsv(tag string, in reflect.Value) {
-	e.mappingv(tag, func() {
-		slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
-		for _, item := range slice {
-			e.marshal("", reflect.ValueOf(item.Key))
-			e.marshal("", reflect.ValueOf(item.Value))
-		}
-	})
-}
-
 func (e *encoder) structv(tag string, in reflect.Value) {
 	sinfo, err := getStructInfo(in.Type())
 	if err != nil {

+ 1 - 7
encode_test.go

@@ -12,7 +12,7 @@ import (
 	"os"
 
 	. "gopkg.in/check.v1"
-	"gopkg.in/yaml.v2"
+	"gopkg.in/niemeyer/ynext.v3"
 )
 
 var marshalIntTest = 123
@@ -315,12 +315,6 @@ var marshalTests = []struct {
 		"a: !!binary |\n  " + strings.Repeat("kJCQ", 17) + "kJ\n  CQ\n",
 	},
 
-	// Ordered maps.
-	{
-		&yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
-		"b: 2\na: 1\nd: 4\nc: 3\nsub:\n  e: 5\n",
-	},
-
 	// Encode unicode as utf-8 rather than in escaped form.
 	{
 		map[string]string{"a": "你好"},

+ 0 - 9
yaml.go

@@ -15,15 +15,6 @@ import (
 	"sync"
 )
 
-// MapSlice encodes and decodes as a YAML map.
-// The order of keys is preserved when encoding and decoding.
-type MapSlice []MapItem
-
-// MapItem is an item in a MapSlice.
-type MapItem struct {
-	Key, Value interface{}
-}
-
 // The Unmarshaler interface may be implemented by types to customize their
 // behavior when being unmarshaled from a YAML document. The UnmarshalYAML
 // method receives a function that may be called to unmarshal the original