Skip to content

Commit

Permalink
Drop MapSlice in favor of general Node support.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Mar 14, 2019
1 parent b145382 commit b804f79
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 79 deletions.
49 changes: 3 additions & 46 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 5 additions & 11 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"

. "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
"gopkg.in/niemeyer/ynext.v3"
)

var marshalIntTest = 123
Expand Down Expand Up @@ -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": "你好"},
Expand Down
9 changes: 0 additions & 9 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b804f79

Please sign in to comment.