github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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 "github.com/juju/mgo/v3/bson" 8 "github.com/juju/version/v2" 9 ) 10 11 type toolsDoc struct { 12 Version version.Binary 13 URL string 14 Size int64 15 SHA256 string 16 } 17 18 // GetBSON returns the structure to be serialized for the tools as a generic 19 // interface. 20 func (t *Tools) GetBSON() (interface{}, error) { 21 if t == nil { 22 return nil, nil 23 } 24 return &toolsDoc{t.Version, t.URL, t.Size, t.SHA256}, nil 25 } 26 27 // SetBSON updates the internal members with the data stored in the bson.Raw 28 // parameter. 29 func (t *Tools) SetBSON(raw bson.Raw) error { 30 if raw.Kind == 10 { 31 // Preserve the nil value in that case. 32 return bson.SetZero 33 } 34 var doc toolsDoc 35 if err := raw.Unmarshal(&doc); err != nil { 36 return err 37 } 38 t.Version = doc.Version 39 t.URL = doc.URL 40 t.Size = doc.Size 41 t.SHA256 = doc.SHA256 42 return nil 43 }