-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support gob encoding/decoding of/to Maps
- Loading branch information
Charles Banning
committed
Mar 29, 2018
1 parent
12eb349
commit 593ae43
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters