/
resolver.go
148 lines (125 loc) · 4.05 KB
/
resolver.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
145
146
147
148
// Copyright 2016 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cfgclient
import (
"github.com/TriggerMail/luci-go/common/errors"
"github.com/TriggerMail/luci-go/config"
)
// Resolver resolves configuration data into a native type.
type Resolver interface {
// Resolve resolves a single Item.
Resolve(it *config.Config) error
}
// MultiResolver resolves a slice of Item.
//
// If it resolves into a slice (which it should), it must preserve Item ordering
// such that resolved Item "n" appears in the slice at index "n".
//
// Any individual resolution failures should be
type MultiResolver interface {
// PrepareMulti indicates that items are about to be loaded, as well as the
// number of resolved values. The MultiResolver should allocate and export its
// output value.
//
// The value's contents will be populated in a series of successive
// ResolveItemAt calls for indexes between zero and (size-1).
PrepareMulti(size int)
// ResolveItemAt resolves an individual item at the specified index.
// PrepareMulti with a size greater than i must be called prior to using
// ResolveItemAt.
ResolveItemAt(i int, it *config.Config) error
}
// FormattingResolver is a Resolver that changes the format of its contents.
// If a Resolver does this, it self-describes the new format so that it can be
// associated with the format later.
type FormattingResolver interface {
// Format returns the FormatterRegistry key and associated data for this
// Resolver.
//
// An empty format represents no Formatter, meaning that this Resolver only
// supports the raw config service result.
Format() config.FormatSpec
}
func assertEmptyFormat(it *config.Config) error {
if !it.FormatSpec.Unformatted() {
return errors.Reason("unknown format: %q", it.FormatSpec.Formatter).Err()
}
return nil
}
// Bytes is a Resolver that resolves config data into a byte slice.
func Bytes(out *[]byte) Resolver { return byteSliceResolver{out} }
// BytesSlice is a MultiResolver that resolves condig data into a slice of byte
// slices.
func BytesSlice(out *[][]byte) MultiResolver { return multiByteSliceResolver{out} }
type byteSliceResolver struct {
out *[]byte
}
func (r byteSliceResolver) Resolve(it *config.Config) error {
if err := assertEmptyFormat(it); err != nil {
return err
}
*r.out = []byte(it.Content)
return nil
}
type multiByteSliceResolver struct {
out *[][]byte
}
func (r multiByteSliceResolver) PrepareMulti(size int) {
switch size {
case 0:
*r.out = nil
default:
*r.out = make([][]byte, size)
}
}
func (r multiByteSliceResolver) ResolveItemAt(i int, it *config.Config) error {
if err := assertEmptyFormat(it); err != nil {
return err
}
(*r.out)[i] = []byte(it.Content)
return nil
}
// String is a Resolver that resolves config data into a string.
func String(out *string) Resolver { return stringResolver{out} }
// StringSlice is a MultiResolver that resolves config data into a slice of
// strings.
func StringSlice(out *[]string) MultiResolver { return multiStringResolver{out} }
type stringResolver struct {
out *string
}
func (r stringResolver) Resolve(it *config.Config) error {
if err := assertEmptyFormat(it); err != nil {
return err
}
*r.out = it.Content
return nil
}
type multiStringResolver struct {
out *[]string
}
func (r multiStringResolver) PrepareMulti(size int) {
switch size {
case 0:
*r.out = nil
default:
*r.out = make([]string, size)
}
}
func (r multiStringResolver) ResolveItemAt(i int, it *config.Config) error {
if err := assertEmptyFormat(it); err != nil {
return err
}
(*r.out)[i] = it.Content
return nil
}