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