@@ -309,12 +309,17 @@ p := consul.NewConsulIniConfigSourceByName("consul-props", address, root)
309
309
- float32,float64
310
310
- map
311
311
- time.Duration
312
- - 嵌套struct
313
- - map:key只支持string,value支持以上除struct的基本类型
312
+ - struct: 包括嵌套、内嵌、匿名、组合、嵌套/内嵌+匿名
313
+ - map:key只支持string,value支持struct
314
314
315
315
316
+
317
+ ##### Unmarshal struct
318
+
316
319
在struct中规定命名为` _prefix ` 、类型为` string ` 、并且指定了` prefix ` tag, 使用feild ` _prefix ` 的` prefix ` tag作为前缀,将struct feild名称转换后组合成完整的key,并从ConfigSource中获取数据并注入struct实例,feild类型只支持ConfigSource所支持的数据类型(string、int、float、bool、time.Duration)。
317
320
321
+ ##### Unmarshal flat struct
322
+
318
323
``` golang
319
324
320
325
@@ -361,6 +366,88 @@ func main() {
361
366
362
367
```
363
368
369
+ Unmarshal flat struct
370
+
371
+ 根据前缀和key,以struct结构层级进行反序列化,key的层级和结构体一一对应,每一层级的key和结构体field名称一致,切第一个字母位小写或者全部小写并用-分割的风格。
372
+
373
+ ### Unmarshal 内嵌 struct
374
+
375
+ 内嵌结构体会** 忽略** 内嵌结构体名称作为key。比如如下结构体:
376
+
377
+ ``` golang
378
+ type PlatStruct struct {
379
+ StrVal string
380
+ IntVal int
381
+ DurationVal time.Duration
382
+ BoolVal bool
383
+ }
384
+ type OuterStruct struct {
385
+ PlatStruct
386
+ }
387
+ ```
388
+
389
+ 前缀未:ums
390
+
391
+ 那么这个结构体对应的key/value应该是:
392
+
393
+ ```
394
+ ums.strVal=str
395
+ ums.intVal=123
396
+ ums.durationVal=1s
397
+ ums.boolVal=true
398
+ ```
399
+
400
+ ##### Unmarshal 嵌套 struct
401
+
402
+ 嵌套结构体会将嵌套的结构体名称作为key。比如如下结构体:
403
+
404
+ ``` golang
405
+ type OuterStruct struct {
406
+ Inner struct {
407
+ StrVal string
408
+ IntVal int
409
+ DurationVal time.Duration
410
+ BoolVal bool
411
+ }
412
+ }
413
+ ```
414
+
415
+ 那么这个结构体对应的key/value应该是:
416
+
417
+ ```
418
+ ums.inner.strVal=str
419
+ ums.inner.intVal=123
420
+ ums.inner.durationVal=1s
421
+ ums.inner.boolVal=true
422
+ ```
423
+
424
+
425
+ ##### Unmarshal Map
426
+
427
+ ``` golang
428
+
429
+ type PlatStruct struct {
430
+ StrVal string
431
+ IntVal int
432
+ DurationVal time.Duration
433
+ BoolVal bool
434
+ }
435
+ ps := NewMapProperties ()
436
+ ps.Set (" ums.test1.strVal" , STR_VAL )
437
+ ps.Set (" ums.test1.intVal" , INT_VAL_STR )
438
+ ps.Set (" ums.test1.durationVal" , DURATION_VAL_STR )
439
+ ps.Set (" ums.test1.boolVal" , BOOL_VAL_STR )
440
+
441
+ ps.Set (" ums.test2.strVal" , STR_VAL )
442
+ ps.Set (" ums.test2.intVal" , INT_VAL_STR )
443
+ ps.Set (" ums.test2.durationVal" , DURATION_VAL_STR )
444
+ ps.Set (" ums.test2.boolVal" , BOOL_VAL_STR )
445
+
446
+ m := make (map [string ]*PlatStruct, 0 )
447
+ err := Unmarshal (ps, m, " ums" )
448
+
449
+ ```
450
+ 如上代码,以ums作为前缀,test1和test2作为map key,ums.test1和ums.test2后面的key将根据struct进行反序列化,key的层级和结构体一一对应。
364
451
365
452
### 上下文变量表达式(或者占位符)的支持
366
453
0 commit comments