-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathzone.go
More file actions
126 lines (109 loc) · 3.4 KB
/
Copy pathzone.go
File metadata and controls
126 lines (109 loc) · 3.4 KB
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
// 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 zone struct {
// Add the controller in when we need to do things with the zone.
// controller Controller
resourceURI string
id int
name string
description string
}
// Name implements Zone.
func (z *zone) Name() string {
return z.name
}
// Description implements Zone.
func (z *zone) Description() string {
return z.description
}
func readZone(controllerVersion version.Number, source any) (*zone, error) {
checker := schema.StringMap(schema.Any())
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "zone base schema check failed")
}
valid := coerced.(map[string]any)
readFunc, err := zoneReadFuncForVersion(controllerVersion)
if err != nil {
return nil, errors.Trace(err)
}
zone, err := readFunc(valid)
if err != nil {
return nil, errors.Trace(err)
}
return zone, nil
}
func readZones(controllerVersion version.Number, source any) ([]*zone, error) {
checker := schema.List(schema.StringMap(schema.Any()))
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "zone base schema check failed")
}
valid := coerced.([]any)
readFunc, err := zoneReadFuncForVersion(controllerVersion)
if err != nil {
return nil, errors.Trace(err)
}
return readZoneList(valid, readFunc)
}
func zoneReadFuncForVersion(controllerVersion version.Number) (zoneDeserializationFunc, error) {
var deserialisationVersion version.Number
for v := range zoneDeserializationFuncs {
if v.Compare(deserialisationVersion) > 0 && v.Compare(controllerVersion) <= 0 {
deserialisationVersion = v
}
}
if deserialisationVersion == version.Zero {
return nil, errors.Errorf("no zone read func for version %s", controllerVersion)
}
return zoneDeserializationFuncs[deserialisationVersion], nil
}
// readZoneList expects the values of the sourceList to be string maps.
func readZoneList(sourceList []any, readFunc zoneDeserializationFunc) ([]*zone, error) {
result := make([]*zone, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]any)
if !ok {
return nil, errors.Errorf("unexpected value for zone %d, %T", i, value)
}
zone, err := readFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "zone %d", i)
}
result = append(result, zone)
}
return result, nil
}
type zoneDeserializationFunc func(map[string]any) (*zone, error)
var zoneDeserializationFuncs = map[version.Number]zoneDeserializationFunc{
twoDotOh: zone_2_0,
}
func zone_2_0(source map[string]any) (*zone, error) {
fields := schema.Fields{
"id": schema.ForceInt(),
"name": schema.String(),
"description": schema.String(),
"resource_uri": schema.String(),
}
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "zone 2.0 schema check failed")
}
valid := coerced.(map[string]any)
// From here we know that the map returned from the schema coercion
// contains fields of the right type.
result := &zone{
id: valid["id"].(int),
name: valid["name"].(string),
description: valid["description"].(string),
resourceURI: valid["resource_uri"].(string),
}
return result, nil
}