@@ -198,6 +198,84 @@ plugins:
198
198
assert .Equal (t , "defname" , bobheaders .ArrayEntry (1 ).GetString ("name" ))
199
199
}
200
200
201
+ func TestNestedArrays (t * testing.T ) {
202
+ defer RootConfigReset ()
203
+
204
+ namespacesRoot := RootSection ("namespaces" )
205
+ predefined := namespacesRoot .SubArray ("predefined" )
206
+ predefined .AddKnownKey ("name" )
207
+ predefined .AddKnownKey ("key1" , "default value" )
208
+ tlsConfigs := predefined .SubArray ("tlsConfigs" )
209
+ tlsConfigs .SetDefault ("testdefault" , "test" )
210
+ tlsConfigs .AddKnownKey ("name" )
211
+ tlsConfigs .SubSection ("tls" ).AddKnownKey ("enabled" )
212
+ tlsConfigs .SubSection ("tls" ).SetDefault ("testdefault" , "test" )
213
+ viper .SetConfigType ("yaml" )
214
+ err := viper .ReadConfig (strings .NewReader (`
215
+ namespaces:
216
+ predefined:
217
+ - name: myns
218
+ tlsConfigs:
219
+ - name: myconfig
220
+ tls:
221
+ enabled: true
222
+ ` ))
223
+ assert .NoError (t , err )
224
+ assert .Equal (t , 1 , predefined .ArraySize ())
225
+ assert .Equal (t , 0 , RootArray ("nonexistent" ).ArraySize ())
226
+ ns := predefined .ArrayEntry (0 )
227
+ assert .Equal (t , "myns" , ns .GetString ("name" ))
228
+ assert .Equal (t , "default value" , ns .GetString ("key1" ))
229
+ nsTLSConfigs := ns .SubArray ("tlsConfigs" )
230
+ assert .Equal (t , 1 , nsTLSConfigs .ArraySize ())
231
+ tlsConfig := nsTLSConfigs .ArrayEntry (0 )
232
+ assert .Equal (t , "myconfig" , tlsConfig .GetString ("name" ))
233
+ assert .Equal (t , "test" , tlsConfig .GetString ("testdefault" ))
234
+ assert .Equal (t , true , tlsConfig .SubSection ("tls" ).GetBool ("enabled" ))
235
+ assert .Equal (t , "test" , tlsConfig .SubSection ("tls" ).GetString ("testdefault" ))
236
+ }
237
+ func TestNestedArraysObjectDefault (t * testing.T ) {
238
+ defer RootConfigReset ()
239
+
240
+ namespacesRoot := RootSection ("namespaces" )
241
+ predefined := namespacesRoot .SubArray ("predefined" )
242
+ predefined .AddKnownKey ("name" )
243
+ predefined .AddKnownKey ("key1" , "default value" )
244
+ newSection := predefined .SubSection ("new" )
245
+ tlsConfigs := newSection .SubArray ("tlsConfigs" )
246
+ tlsConfigs .SetDefault ("testdefault" , "test" )
247
+ tlsConfigs .AddKnownKey ("name" )
248
+ tlsConfigs .SubSection ("tls" ).AddKnownKey ("enabled" )
249
+ tlsConfigs .SubSection ("tls" ).SetDefault ("testdefault" , "test" )
250
+ viper .SetConfigType ("yaml" )
251
+ // thing.stuff[].watsit.hoojars[].mydefault
252
+
253
+ err := viper .ReadConfig (strings .NewReader (`
254
+ namespaces:
255
+ predefined:
256
+ - name: myns
257
+ new:
258
+ tlsConfigs:
259
+ - name: myconfig
260
+ tls:
261
+ enabled: true
262
+ ` ))
263
+ assert .NoError (t , err )
264
+ assert .Equal (t , 1 , predefined .ArraySize ())
265
+ assert .Equal (t , 0 , RootArray ("nonexistent" ).ArraySize ())
266
+ ns := predefined .ArrayEntry (0 )
267
+ assert .Equal (t , "myns" , ns .GetString ("name" ))
268
+ assert .Equal (t , "default value" , ns .GetString ("key1" ))
269
+ section := ns .SubSection ("new" )
270
+ nsTLSConfigs := section .SubArray ("tlsConfigs" )
271
+ assert .Equal (t , 1 , nsTLSConfigs .ArraySize ())
272
+ tlsConfig := nsTLSConfigs .ArrayEntry (0 )
273
+ assert .Equal (t , "myconfig" , tlsConfig .GetString ("name" ))
274
+ assert .Equal (t , "test" , tlsConfig .GetString ("testdefault" ))
275
+ assert .Equal (t , true , tlsConfig .SubSection ("tls" ).GetBool ("enabled" ))
276
+ assert .Equal (t , "test" , tlsConfig .SubSection ("tls" ).GetString ("testdefault" ))
277
+ }
278
+
201
279
func TestMapOfAdminOverridePlugins (t * testing.T ) {
202
280
defer RootConfigReset ()
203
281
0 commit comments