github.com/jgadling/terraform@v0.3.8-0.20150227214559-abd68c2c87bc/config/module/tree_gob.go (about)

     1  package module
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/gob"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  )
     9  
    10  func (t *Tree) GobDecode(bs []byte) error {
    11  	t.lock.Lock()
    12  	defer t.lock.Unlock()
    13  
    14  	// Decode the gob data
    15  	var data treeGob
    16  	dec := gob.NewDecoder(bytes.NewReader(bs))
    17  	if err := dec.Decode(&data); err != nil {
    18  		return err
    19  	}
    20  
    21  	// Set the fields
    22  	t.name = data.Name
    23  	t.config = data.Config
    24  	t.children = data.Children
    25  
    26  	return nil
    27  }
    28  
    29  func (t *Tree) GobEncode() ([]byte, error) {
    30  	data := &treeGob{
    31  		Config:   t.config,
    32  		Children: t.children,
    33  		Name:     t.name,
    34  	}
    35  
    36  	var buf bytes.Buffer
    37  	enc := gob.NewEncoder(&buf)
    38  	if err := enc.Encode(data); err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return buf.Bytes(), nil
    43  }
    44  
    45  // treeGob is used as a structure to Gob encode a tree.
    46  //
    47  // This structure is private so it can't be referenced but the fields are
    48  // public, allowing Gob to properly encode this. When we decode this, we are
    49  // able to turn it into a Tree.
    50  type treeGob struct {
    51  	Config   *config.Config
    52  	Children map[string]*Tree
    53  	Name     string
    54  }