-
Notifications
You must be signed in to change notification settings - Fork 23
/
payload.go
144 lines (124 loc) · 3.44 KB
/
payload.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
136
137
138
139
140
141
142
143
144
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"github.com/juju/errors"
"github.com/juju/schema"
)
// Payload represents a charm payload for a unit.
type Payload interface {
Name() string
Type() string
RawID() string
State() string
Labels() []string
}
type payloads struct {
Version int `yaml:"version"`
Payloads_ []*payload `yaml:"payloads"`
}
type payload struct {
Name_ string `yaml:"name"`
Type_ string `yaml:"type"`
RawID_ string `yaml:"raw-id"`
State_ string `yaml:"state"`
Labels_ []string `yaml:"labels,omitempty"`
}
// Name implements Payload.
func (p *payload) Name() string {
return p.Name_
}
// Type implements Payload.
func (p *payload) Type() string {
return p.Type_
}
// RawID implements Payload.
func (p *payload) RawID() string {
return p.RawID_
}
// State implements Payload.
func (p *payload) State() string {
return p.State_
}
// Labels implements Payload.
func (p *payload) Labels() []string {
return p.Labels_
}
// PayloadArgs is an argument struct used to create a
// new internal payload type that supports the Payload interface.
type PayloadArgs struct {
Name string
Type string
RawID string
State string
Labels []string
}
func newPayload(args PayloadArgs) *payload {
return &payload{
Name_: args.Name,
Type_: args.Type,
RawID_: args.RawID,
State_: args.State,
Labels_: args.Labels,
}
}
func importPayloads(source map[string]interface{}) ([]*payload, error) {
checker := versionedChecker("payloads")
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "payloads version schema check failed")
}
valid := coerced.(map[string]interface{})
version := int(valid["version"].(int64))
importFunc, ok := payloadDeserializationFuncs[version]
if !ok {
return nil, errors.NotValidf("version %d", version)
}
sourceList := valid["payloads"].([]interface{})
return importPayloadList(sourceList, importFunc)
}
func importPayloadList(sourceList []interface{}, importFunc payloadDeserializationFunc) ([]*payload, error) {
result := make([]*payload, 0, len(sourceList))
for i, value := range sourceList {
source, ok := value.(map[string]interface{})
if !ok {
return nil, errors.Errorf("unexpected value for payload %d, %T", i, value)
}
payload, err := importFunc(source)
if err != nil {
return nil, errors.Annotatef(err, "payload %d", i)
}
result = append(result, payload)
}
return result, nil
}
type payloadDeserializationFunc func(map[string]interface{}) (*payload, error)
var payloadDeserializationFuncs = map[int]payloadDeserializationFunc{
1: importPayloadV1,
}
func importPayloadV1(source map[string]interface{}) (*payload, error) {
fields := schema.Fields{
"name": schema.String(),
"type": schema.String(),
"raw-id": schema.String(),
"state": schema.String(),
"labels": schema.List(schema.String()),
}
// Some values don't have to be there.
defaults := schema.Defaults{
"labels": schema.Omit,
}
checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, errors.Annotatef(err, "payload v1 schema check failed")
}
valid := coerced.(map[string]interface{})
return &payload{
Name_: valid["name"].(string),
Type_: valid["type"].(string),
RawID_: valid["raw-id"].(string),
State_: valid["state"].(string),
Labels_: convertToStringSlice(valid["labels"]),
}, nil
}