@@ -100,13 +100,15 @@ type ChangeEvent struct {
100
100
// Retrieved holds the result of a call to the Retrieve method of a Provider object.
101
101
type Retrieved struct {
102
102
rawConf any
103
+ errorHint error
103
104
closeFunc CloseFunc
104
105
105
106
stringRepresentation string
106
107
isSetString bool
107
108
}
108
109
109
110
type retrievedSettings struct {
111
+ errorHint error
110
112
stringRepresentation string
111
113
isSetString bool
112
114
closeFunc CloseFunc
@@ -138,6 +140,12 @@ func withStringRepresentation(stringRepresentation string) RetrievedOption {
138
140
})
139
141
}
140
142
143
+ func withErrorHint (errorHint error ) RetrievedOption {
144
+ return retrievedOptionFunc (func (settings * retrievedSettings ) {
145
+ settings .errorHint = errorHint
146
+ })
147
+ }
148
+
141
149
// NewRetrievedFromYAML returns a new Retrieved instance that contains the deserialized data from the yaml bytes.
142
150
// * yamlBytes the yaml bytes that will be deserialized.
143
151
// * opts specifies options associated with this Retrieved value, such as CloseFunc.
@@ -146,7 +154,10 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...RetrievedOption) (*Retrieved
146
154
if err := yaml .Unmarshal (yamlBytes , & rawConf ); err != nil {
147
155
// If the string is not valid YAML, we try to use it verbatim as a string.
148
156
strRep := string (yamlBytes )
149
- return NewRetrieved (strRep , append (opts , withStringRepresentation (strRep ))... )
157
+ return NewRetrieved (strRep , append (opts ,
158
+ withStringRepresentation (strRep ),
159
+ withErrorHint (fmt .Errorf ("assuming string type since contents are not valid YAML: %w" , err )),
160
+ )... )
150
161
}
151
162
152
163
switch rawConf .(type ) {
@@ -175,6 +186,7 @@ func NewRetrieved(rawConf any, opts ...RetrievedOption) (*Retrieved, error) {
175
186
}
176
187
return & Retrieved {
177
188
rawConf : rawConf ,
189
+ errorHint : set .errorHint ,
178
190
closeFunc : set .closeFunc ,
179
191
stringRepresentation : set .stringRepresentation ,
180
192
isSetString : set .isSetString ,
@@ -188,6 +200,9 @@ func (r *Retrieved) AsConf() (*Conf, error) {
188
200
}
189
201
val , ok := r .rawConf .(map [string ]any )
190
202
if ! ok {
203
+ if r .errorHint != nil {
204
+ return nil , fmt .Errorf ("retrieved value (type=%T) cannot be used as a Conf: %w" , r .rawConf , r .errorHint )
205
+ }
191
206
return nil , fmt .Errorf ("retrieved value (type=%T) cannot be used as a Conf" , r .rawConf )
192
207
}
193
208
return NewFromStringMap (val ), nil
0 commit comments