-
Notifications
You must be signed in to change notification settings - Fork 36
/
link.go
124 lines (108 loc) · 3.19 KB
/
link.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package gomaasapi
import (
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
)
type link struct {
id int
mode string
subnet *subnet
ipAddress string
}
// NOTE: not using lowercase L as the receiver as it is a horrible idea.
// Instead using 'k'.
// ID implements Link.
func (k *link) ID() int {
return k.id
}
// Mode implements Link.
func (k *link) Mode() string {
return k.mode
}
// Subnet implements Link.
func (k *link) Subnet() Subnet {
if k.subnet == nil {
return nil
}
return k.subnet
}
// IPAddress implements Link.
func (k *link) IPAddress() string {
return k.ipAddress
}
func readLinks(controllerVersion version.Number, source interface{}) ([]*link, error) {
checker := schema.List(schema.StringMap(schema.Any()))
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "link base schema check failed")
}
valid := coerced.([]interface{})
var deserialisationVersion version.Number
for v := range linkDeserializationFuncs {
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
deserialisationVersion = v
}
}
if deserialisationVersion == version.Zero {
return nil, NewUnsupportedVersionError("no link read func for version %s", controllerVersion)
}
readFunc := linkDeserializationFuncs[deserialisationVersion]
return readLinkList(valid, readFunc)
}
// readLinkList expects the values of the sourceList to be string maps.
func readLinkList(sourceList []interface{}, readFunc linkDeserializationFunc) ([]*link, error) {
result := make([]*link, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, NewDeserializationError("unexpected value for link %d, %T", i, value)
}
link, err := readFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "link %d", i)
}
result = append(result, link)
}
return result, nil
}
type linkDeserializationFunc func(map[string]interface{}) (*link, error)
var linkDeserializationFuncs = map[version.Number]linkDeserializationFunc{
twoDotOh: link_2_0,
}
func link_2_0(source map[string]interface{}) (*link, error) {
fields := schema.Fields{
"id": schema.ForceInt(),
"mode": schema.String(),
"subnet": schema.StringMap(schema.Any()),
"ip_address": schema.String(),
}
defaults := schema.Defaults{
"ip_address": "",
"subnet": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "link 2.0 schema check failed")
}
valid := coerced.(map[string]interface{})
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
var subnet *subnet
if value, ok := valid["subnet"]; ok {
subnet, err = subnet_2_0(value.(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}
}
result := &link{
id: valid["id"].(int),
mode: valid["mode"].(string),
subnet: subnet,
ipAddress: valid["ip_address"].(string),
}
return result, nil
}