~cloud-green/juju-core/charmstore-sdist

« back to all changes in this revision

Viewing changes to launchpad.net/juju-core/tools/marshal.go

  • Committer: Casey Marshall
  • Date: 2014-03-27 15:59:46 UTC
  • Revision ID: cmars@cmarstech.com-20140327155946-8huorf37g0zwar43
Source distribution of launchpad.net/juju-core created 20140327105939

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package tools
 
5
 
 
6
import (
 
7
        "labix.org/v2/mgo/bson"
 
8
 
 
9
        "launchpad.net/juju-core/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
}