github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/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  	t.path = data.Path
    26  
    27  	return nil
    28  }
    29  
    30  func (t *Tree) GobEncode() ([]byte, error) {
    31  	data := &treeGob{
    32  		Config:   t.config,
    33  		Children: t.children,
    34  		Name:     t.name,
    35  		Path:     t.path,
    36  	}
    37  
    38  	var buf bytes.Buffer
    39  	enc := gob.NewEncoder(&buf)
    40  	if err := enc.Encode(data); err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return buf.Bytes(), nil
    45  }
    46  
    47  // treeGob is used as a structure to Gob encode a tree.
    48  //
    49  // This structure is private so it can't be referenced but the fields are
    50  // public, allowing Gob to properly encode this. When we decode this, we are
    51  // able to turn it into a Tree.
    52  type treeGob struct {
    53  	Config   *config.Config
    54  	Children map[string]*Tree
    55  	Name     string
    56  	Path     []string
    57  }