github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/tools/marshal.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package tools
     5  
     6  import (
     7  	"gopkg.in/mgo.v2/bson"
     8  
     9  	"github.com/juju/juju/version"
    10  )
    11  
    12  type toolsDoc struct {
    13  	Version version.Binary
    14  	URL     string
    15  	Size    int64
    16  	SHA256  string
    17  }
    18  
    19  // GetBSON returns the structure to be serialized for the tools as a generic
    20  // interface.
    21  func (t *Tools) GetBSON() (interface{}, error) {
    22  	if t == nil {
    23  		return nil, nil
    24  	}
    25  	return &toolsDoc{t.Version, t.URL, t.Size, t.SHA256}, nil
    26  }
    27  
    28  // SetBSON updates the internal members with the data stored in the bson.Raw
    29  // parameter.
    30  func (t *Tools) SetBSON(raw bson.Raw) error {
    31  	if raw.Kind == 10 {
    32  		// Preserve the nil value in that case.
    33  		return bson.SetZero
    34  	}
    35  	var doc toolsDoc
    36  	if err := raw.Unmarshal(&doc); err != nil {
    37  		return err
    38  	}
    39  	t.Version = doc.Version
    40  	t.URL = doc.URL
    41  	t.Size = doc.Size
    42  	t.SHA256 = doc.SHA256
    43  	return nil
    44  }