mirror of https://github.com/hashicorp/terraform
parent
672bf58337
commit
718fb42f4b
@ -0,0 +1,54 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
)
|
||||
|
||||
func (t *Tree) GobDecode(bs []byte) error {
|
||||
t.lock.Lock()
|
||||
defer t.lock.Unlock()
|
||||
|
||||
// Decode the gob data
|
||||
var data treeGob
|
||||
dec := gob.NewDecoder(bytes.NewReader(bs))
|
||||
if err := dec.Decode(&data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set the fields
|
||||
t.name = data.Name
|
||||
t.config = data.Config
|
||||
t.children = data.Children
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Tree) GobEncode() ([]byte, error) {
|
||||
data := &treeGob{
|
||||
Config: t.config,
|
||||
Children: t.children,
|
||||
Name: t.name,
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
if err := enc.Encode(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// treeGob is used as a structure to Gob encode a tree.
|
||||
//
|
||||
// This structure is private so it can't be referenced but the fields are
|
||||
// public, allowing Gob to properly encode this. When we decode this, we are
|
||||
// able to turn it into a Tree.
|
||||
type treeGob struct {
|
||||
Config *config.Config
|
||||
Children map[string]*Tree
|
||||
Name string
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTreeEncodeDecodeGob(t *testing.T) {
|
||||
storage := testStorage(t)
|
||||
tree := NewTree("", testConfig(t, "basic"))
|
||||
|
||||
// This should get things
|
||||
if err := tree.Load(storage, GetModeGet); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Encode it.
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
if err := enc.Encode(tree); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
dec := gob.NewDecoder(&buf)
|
||||
var actual Tree
|
||||
if err := dec.Decode(&actual); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actualStr := strings.TrimSpace(actual.String())
|
||||
expectedStr := strings.TrimSpace(tree.String())
|
||||
if actualStr != expectedStr {
|
||||
t.Fatalf("\n%s\n\nexpected:\n\n%s", actualStr, expectedStr)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue