Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(793)

Side by Side Diff: server/config/resolver.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Update MultiResolver interface, add test for MultiError. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package config
6
7 import (
8 "github.com/luci/luci-go/common/errors"
9 )
10
11 // Resolver resolves configuration data into a native type.
12 type Resolver interface {
13 // Resolve resolves a single Item.
14 Resolve(it *Item) error
15 }
16
17 // MultiResolver resolves a slice of Item.
18 //
19 // If it resolves into a slice (which it should), it must preserve Item ordering
20 // such that resolved Item "n" appears in the slice at index "n".
21 //
22 // Any individual resolution failures should be
23 type MultiResolver interface {
24 // PrepareMulti indicates that items are about to be loaded, as well as the
25 // number of resolved values. The MultiResolver should allocate and expo rt its
26 // output value.
27 //
28 // The value's contents will be populated in a series of successive
29 // ResolveItemAt calls for indexes between zero and (size-1).
30 PrepareMulti(size int)
31
32 // ResolveItemAt resolves an individual item at the specified index.
33 // PrepareMulti with a size greater than i must be called prior to using
34 // ResolveItemAt.
35 ResolveItemAt(i int, it *Item) error
36 }
37
38 // FormattingResolver is a Resolver that changes the format of its contents.
39 // If a Resolver does this, it self-describes the new format so that it can be
40 // associated with the format later.
41 type FormattingResolver interface {
42 // Format returns the FormatterRegistry key and associated data for this
43 // Resolver.
44 //
45 // An empty format represents no Formatter, meaning that this Resolver o nly
46 // supports the raw config service result.
47 Format() (format, data string)
48 }
49
50 func assertEmptyFormat(it *Item) error {
51 if it.Format != "" {
52 return errors.Reason("unknown format: %(format)q").D("format", i t.Format).Err()
53 }
54 return nil
55 }
56
57 // Bytes is a Resolver that resolves config data into a byte slice.
58 func Bytes(out *[]byte) Resolver { return byteSliceResolver{out} }
59
60 // BytesSlice is a MultiResolver that resolves condig data into a slice of byte
61 // slices.
62 func BytesSlice(out *[][]byte) MultiResolver { return multiByteSliceResolver{out } }
63
64 type byteSliceResolver struct {
65 out *[]byte
66 }
67
68 func (r byteSliceResolver) Resolve(it *Item) error {
69 if err := assertEmptyFormat(it); err != nil {
70 return err
71 }
72 *r.out = []byte(it.Content)
73 return nil
74 }
75
76 type multiByteSliceResolver struct {
77 out *[][]byte
78 }
79
80 func (r multiByteSliceResolver) PrepareMulti(size int) {
81 switch size {
82 case 0:
83 *r.out = nil
84
85 default:
86 *r.out = make([][]byte, size)
87 }
88 }
89
90 func (r multiByteSliceResolver) ResolveItemAt(i int, it *Item) error {
91 if err := assertEmptyFormat(it); err != nil {
92 return err
93 }
94 (*r.out)[i] = []byte(it.Content)
95 return nil
96 }
97
98 // String is a Resolver that resolves config data into a string.
99 func String(out *string) Resolver { return stringResolver{out} }
100
101 // StringSlice is a MultiResolver that resolves config data into a slice of
102 // strings.
103 func StringSlice(out *[]string) MultiResolver { return multiStringResolver{out} }
104
105 type stringResolver struct {
106 out *string
107 }
108
109 func (r stringResolver) Resolve(it *Item) error {
110 if err := assertEmptyFormat(it); err != nil {
111 return err
112 }
113 *r.out = it.Content
114 return nil
115 }
116
117 type multiStringResolver struct {
118 out *[]string
119 }
120
121 func (r multiStringResolver) PrepareMulti(size int) {
122 switch size {
123 case 0:
124 *r.out = nil
125
126 default:
127 *r.out = make([]string, size)
128 }
129 }
130
131 func (r multiStringResolver) ResolveItemAt(i int, it *Item) error {
132 if err := assertEmptyFormat(it); err != nil {
133 return err
134 }
135 (*r.out)[i] = it.Content
136 return nil
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698