@@ -318,16 +318,9 @@ func GetE(value interface{}, path string) (Result, error) {
318
318
}
319
319
320
320
default :
321
- start := index
322
- loop:
323
- for ; index < len (path ); index ++ {
324
- switch path [index ] {
325
- case '.' , '#' :
326
- index --
327
- break loop
328
- }
329
- }
330
- sv := path [start :min (index + 1 , len (path ))]
321
+ key , newIndex := parseNextKey (path , index )
322
+ index = newIndex
323
+ sv := key
331
324
v , err := strconv .Atoi (sv )
332
325
digit := err == nil && v >= 0
333
326
switch result .deployment {
@@ -449,16 +442,9 @@ func Get(value interface{}, path string) Result {
449
442
}
450
443
451
444
default :
452
- start := index
453
- loop:
454
- for ; index < len (path ); index ++ {
455
- switch path [index ] {
456
- case '.' , '#' :
457
- index --
458
- break loop
459
- }
460
- }
461
- sv := path [start :min (index + 1 , len (path ))]
445
+ key , newIndex := parseNextKey (path , index )
446
+ index = newIndex
447
+ sv := key
462
448
v , err := strconv .Atoi (sv )
463
449
digit := err == nil && v >= 0
464
450
switch result .deployment {
@@ -508,6 +494,37 @@ func Get(value interface{}, path string) Result {
508
494
return result
509
495
}
510
496
497
+ // 从选择器中解析出下一个要处理的key
498
+ // params:
499
+ // selector: 路径选择器,比如"Foo.Bar.Name",比如"Foo\\.Bar\\.Name"
500
+ // index: 选择器上次消费到的位置
501
+ //
502
+ // returns:
503
+ // string 下一个要解析的key
504
+ // index selector被消费到的位置
505
+ func parseNextKey (selector string , index int ) (string , int ) {
506
+ key := make ([]byte , 0 )
507
+ loop:
508
+ for ; index < len (selector ); index ++ {
509
+ switch selector [index ] {
510
+ case '\\' :
511
+ // 先跳过转义字符
512
+ index ++
513
+ // 转义字符,无论下一个字符是什么都跳过,如果有的话
514
+ if index < len (selector ) {
515
+ key = append (key , selector [index ])
516
+ }
517
+ case '.' , '#' :
518
+ index --
519
+ break loop
520
+ default :
521
+ // 普通字符,当做key的一部分消费掉
522
+ key = append (key , selector [index ])
523
+ }
524
+ }
525
+ return string (key ), index
526
+ }
527
+
511
528
func GetMany (value interface {}, path ... string ) []Result {
512
529
results := make ([]Result , 0 , len (path ))
513
530
for _ , s := range path {
0 commit comments