/
address.go
135 lines (115 loc) · 3.11 KB
/
address.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
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// Address represents an IP Address of some form.
type Address interface {
Value() string
Type() string
Scope() string
Origin() string
}
// AddressArgs is an argument struct used to create a new internal address
// type that supports the Address interface.
type AddressArgs struct {
Value string
Type string
Scope string
Origin string
}
func newAddress(args AddressArgs) *address {
return &address{
Version: 1,
Value_: args.Value,
Type_: args.Type,
Scope_: args.Scope,
Origin_: args.Origin,
}
}
// address represents an IP Address of some form.
type address struct {
Version int `yaml:"version"`
Value_ string `yaml:"value"`
Type_ string `yaml:"type"`
Scope_ string `yaml:"scope,omitempty"`
Origin_ string `yaml:"origin,omitempty"`
}
// Value implements Address.
func (a *address) Value() string {
return a.Value_
}
// Type implements Address.
func (a *address) Type() string {
return a.Type_
}
// Scope implements Address.
func (a *address) Scope() string {
return a.Scope_
}
// Origin implements Address.
func (a *address) Origin() string {
return a.Origin_
}
func importAddresses(sourceList []interface{}) ([]*address, error) {
var result []*address
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for address %d, %T", i, value)
}
addr, err := importAddress(source)
if err != nil {
return nil, errors.Trace(err)
}
result = append(result, addr)
}
return result, nil
}
// importAddress constructs a new Address from a map representing a serialised
// Address instance.
func importAddress(source map[string]interface{}) (*address, error) {
version, err := getVersion(source)
if err != nil {
return nil, errors.Annotate(err, "address version schema check failed")
}
importFunc, ok := addressDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
return importFunc(source)
}
type addressDeserializationFunc func(map[string]interface{}) (*address, error)
var addressDeserializationFuncs = map[int]addressDeserializationFunc{
1: importAddressV1,
}
func importAddressV1(source map[string]interface{}) (*address, error) {
fields := schema.Fields{
"value": schema.String(),
"type": schema.String(),
"scope": schema.String(),
"origin": schema.String(),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"scope": "",
"origin": "",
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "address v1 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.
return &address{
Version: 1,
Value_: valid["value"].(string),
Type_: valid["type"].(string),
Scope_: valid["scope"].(string),
Origin_: valid["origin"].(string),
}, nil
}