Skip to content

Commit

Permalink
support gob encoding/decoding of/to Maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Banning committed Mar 29, 2018
1 parent 12eb349 commit 593ae43
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Related Packages:
checkxml: github.com/clbanning/checkxml provides functions for validating XML data.
Notes:
2018.03.29: mv.Gob/NewMapGob support gob encoding/decoding of Maps.
2018.03.26: Added mxj/x2j-wrapper sub-package for migrating from legacy x2j package.
2017.02.22: LeafNode paths can use ".N" syntax rather than "[N]" for list member indexing.
2017.02.21: github.com/clbanning/checkxml provides functions for validating XML data.
Expand Down
35 changes: 35 additions & 0 deletions gob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// gob.go - Encode/Decode a Map into a gob object.

package mxj

import (
"bytes"
"encoding/gob"
)

// NewMapGob returns a Map value for a gob object that has been
// encoded from a map[string]interface{} (or compatible type) value.
// It is intended to provide symmetric handling of Maps that have
// been encoded using mv.Gob.
func NewMapGob(gobj []byte) (Map, error) {
m := make(map[string]interface{}, 0)
if len(gobj) == 0 {
return m, nil
}
r := bytes.NewReader(gobj)
dec := gob.NewDecoder(r)
if err := dec.Decode(&m); err != nil {
return m, err
}
return m, nil
}

// Gob returns a gob-encoded value for the Map 'mv'.
func (mv Map) Gob() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err := enc.Encode(map[string]interface{}(mv)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
50 changes: 50 additions & 0 deletions gob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mxj

import (
"bytes"
"encoding/gob"
"fmt"
"testing"
)

var gobData = map[string]interface{}{
"one": 1,
"two": 2.0001,
"three": "tres",
"four": []int{1, 2, 3, 4},
"five": map[string]interface{}{"hi": "there"}}

func TestGobHeader(t *testing.T) {
fmt.Println("\n---------------- gob_test.go ...")
}

func TestNewMapGob(t *testing.T) {
var buf bytes.Buffer
gob.Register(gobData)
enc := gob.NewEncoder(&buf)
if err := enc.Encode(gobData); err != nil {
t.Fatal("enc.Encode err:", err.Error())
}
// decode 'buf' into a Map - map[string]interface{}
m, err := NewMapGob(buf.Bytes())
if err != nil {
t.Fatal("NewMapGob err:", err.Error())
}
fmt.Printf("m: %v\n", m)
}

func TestMapGob(t *testing.T) {
mv := Map(gobData)
g, err := mv.Gob()
if err != nil {
t.Fatal("m.Gob err:", err.Error())
}
// decode 'g' into a map[string]interface{}
m := make(map[string]interface{})
r := bytes.NewReader(g)
dec := gob.NewDecoder(r)
if err := dec.Decode(&m); err != nil {
t.Fatal("dec.Decode err:", err.Error())
}
fmt.Printf("m: %v\n", m)
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ For over a year I've wanted to refactor the XML-to-map[string]interface{} decode

<h4>Notices</h4>

2018.03.29: mv.Gob/NewMapGob support gob encoding/decoding of Maps.
2018.03.26: Added mxj/x2j-wrapper sub-package for migrating from legacy x2j package.
2017.02.22: LeafNode paths can use ".N" syntax rather than "[N]" for list member indexing.
2017.02.10: SetFieldSeparator changes field separator for args in UpdateValuesForPath, ValuesFor... methods.
Expand Down

0 comments on commit 593ae43

Please sign in to comment.