@@ -2482,32 +2482,33 @@ func TestPathCorrection(t *testing.T) {
2482
2482
want string
2483
2483
skip bool
2484
2484
}{{
2485
- // // TODO: structural cycle.
2486
- // input: `
2487
- // a: b: {
2488
- // c: d: b
2489
- // }
2490
- // `,
2491
- // lookup: func(i *Instance) Value {
2492
- // _, a := i.Lookup("a", "b", "c", "d").Expr()
2493
- // return a[0].Lookup("b", "c", "d")
2494
- // },
2495
- // want: "a.b ",
2496
- // }, {
2485
+ input : `
2486
+ a: b: {
2487
+ c: d: b
2488
+ }
2489
+ ` ,
2490
+ lookup : func ( i * Instance ) Value {
2491
+ op , a := i . Lookup ( "a" , "b" , "c" , "d" ). Expr ()
2492
+ _ = op
2493
+ return a [0 ] // structural cycle errors.
2494
+ },
2495
+ want : "a" ,
2496
+ }, {
2497
2497
2498
2498
// TODO: embedding: have field operators.
2499
- // input: `
2500
- // a: {
2501
- // c: 3
2502
- // {x: c}
2503
- // }
2504
- // `,
2505
- // lookup: func(i *Instance) Value {
2506
- // _, a := i.Lookup("a").Expr()
2507
- // return a[1].Lookup("x")
2508
- // },
2509
- // want: "a.c",
2510
- // }, {
2499
+ input : `
2500
+ a: {
2501
+ {x: c}
2502
+ c: 3
2503
+ }
2504
+ ` ,
2505
+ lookup : func (i * Instance ) Value {
2506
+ op , a := i .Lookup ("a" ).Expr ()
2507
+ _ = op
2508
+ return a [0 ].Lookup ("x" )
2509
+ },
2510
+ want : "a.c" ,
2511
+ }, {
2511
2512
2512
2513
// TODO: implement proper Elem()
2513
2514
input : `
@@ -2557,46 +2558,45 @@ func TestPathCorrection(t *testing.T) {
2557
2558
return a [0 ]
2558
2559
},
2559
2560
want : "#T" ,
2560
- // }, {
2561
- // input: `
2562
- // #a: {
2563
- // #T: {b: 3}
2564
- // close({}) | close({c: #T}) | close({d: string})
2565
- // }
2566
- // `,
2567
- // lookup: func(i *Instance) Value {
2568
- // f, _ := i.LookupField("#a")
2569
- // _, a := f.Value.Expr() // &
2570
- // _, a = a[1].Expr() // |
2571
- // return a[1].Lookup("c")
2572
- // },
2573
- // want: "#a.#T",
2574
- }, {
2575
- // TODO: iterate over Definitions
2576
- // input: `
2577
- // package foo
2578
-
2579
- // #Struct: {
2580
- // #T: int
2581
-
2582
- // {b?: #T}
2583
- // }`,
2584
- // want: "#Struct.#T",
2585
- // lookup: func(inst *Instance) Value {
2586
- // // Locate Struct
2587
- // i, _ := inst.Value().Fields(Definitions(true))
2588
- // if !i.Next() {
2589
- // t.Fatal("no fields")
2590
- // }
2591
- // // Locate b
2592
- // i, _ = i.Value().Fields(Definitions(true), Optional(true))
2593
- // if !(i.Next() && i.Next()) {
2594
- // t.Fatal("no fields")
2595
- // }
2596
- // v := i.Value()
2597
- // return v
2598
- // },
2599
- // }, {
2561
+ }, {
2562
+ input : `
2563
+ #a: {
2564
+ close({}) | close({c: #T}) | close({d: string})
2565
+ #T: {b: 3}
2566
+ }
2567
+ ` ,
2568
+ lookup : func (i * Instance ) Value {
2569
+ f , _ := i .LookupField ("#a" )
2570
+ _ , a := f .Value .Expr () // &
2571
+ _ , a = a [0 ].Expr () // |
2572
+ return a [1 ].Lookup ("c" )
2573
+ },
2574
+ want : "#a.#T" ,
2575
+ }, {
2576
+ input : `
2577
+ package foo
2578
+
2579
+ #Struct: {
2580
+ #T: int
2581
+
2582
+ {b?: #T}
2583
+ }` ,
2584
+ want : "#Struct.#T" ,
2585
+ lookup : func (inst * Instance ) Value {
2586
+ // Locate Struct
2587
+ i , _ := inst .Value ().Fields (Definitions (true ))
2588
+ if ! i .Next () {
2589
+ t .Fatal ("no fields" )
2590
+ }
2591
+ // Locate b
2592
+ i , _ = i .Value ().Fields (Definitions (true ), Optional (true ))
2593
+ if ! (i .Next () && i .Next ()) {
2594
+ t .Fatal ("no fields" )
2595
+ }
2596
+ v := i .Value ()
2597
+ return v
2598
+ },
2599
+ }, {
2600
2600
2601
2601
input : `
2602
2602
package foo
@@ -2617,80 +2617,71 @@ func TestPathCorrection(t *testing.T) {
2617
2617
v = v .Lookup ("a" )
2618
2618
return v
2619
2619
},
2620
- // }, {
2620
+ }, {
2621
2621
2622
2622
// TODO: record additionalItems in list
2623
- // input: `
2624
- // package foo
2625
-
2626
- // #A: #B: #T
2627
-
2628
- // #T: {
2629
- // a: [...#S]
2630
- // #S: {}
2631
- // }
2632
- // `,
2633
- // want: "#T.#S",
2634
- // lookup: func(inst *Instance) Value {
2635
- // f, _ := inst.Value().LookupField("#A")
2636
- // f, _ = f.Value.LookupField("#B")
2637
- // v := f.Value
2638
- // v = Dereference(v)
2639
- // v, _ = v.Lookup("a").Elem()
2640
- // return v
2641
- // },
2642
- // }, {
2623
+ input : `
2624
+ package foo
2643
2625
2644
- // YAY: works.
2645
- // input: `
2646
- // #A: {
2647
- // b: #T
2648
- // }
2649
-
2650
- // #T: {
2651
- // a: #S
2652
- // #S: {}
2653
- // }
2654
- // `,
2655
- // want: "#T.#S",
2656
- // lookup: func(inst *Instance) Value {
2657
- // f, _ := inst.Value().LookupField("#A")
2658
- // v := f.Value.Lookup("b")
2659
- // v = Dereference(v)
2660
- // v = v.Lookup("a")
2661
- // return v
2662
- // },
2663
- // }, {
2626
+ #A: #B: #T
2627
+
2628
+ #T: {
2629
+ a: [...#S]
2630
+ #S: {}
2631
+ }
2632
+ ` ,
2633
+ want : "#T.#S" ,
2634
+ lookup : func (inst * Instance ) Value {
2635
+ f , _ := inst .Value ().LookupField ("#A" )
2636
+ f , _ = f .Value .LookupField ("#B" )
2637
+ v := f .Value
2638
+ v = Dereference (v )
2639
+ v , _ = v .Lookup ("a" ).Elem ()
2640
+ return v
2641
+ },
2642
+ }, {
2643
+ input : `
2644
+ #A: {
2645
+ b: #T
2646
+ }
2664
2647
2665
- // // TODO(eval): embedded structs are currently represented at the same
2666
- // // level as the enclosing struct. This means that the parent of an
2667
- // // embedded struct skips the struct in which it is embedded. Treat
2668
- // // embedded structs as "anonymous" fields.
2669
- // // This could perhaps be made fixed with dereferencing as well.
2670
- // skip: true,
2671
- // input: `
2672
- // #Tracing: {
2673
- // #T: { address?: string }
2674
- // #S: { ip?: string }
2675
-
2676
- // close({}) | close({
2677
- // t: #T
2678
- // }) | close({
2679
- // s: #S
2680
- // })
2681
- // }
2682
- // #X: {}
2683
- // #X // Disconnect top-level struct from the one visible by close.
2684
- // `,
2685
- // want: "",
2686
- // lookup: func(inst *Instance) Value {
2687
- // f, _ := inst.Value().LookupField("#Tracing")
2688
- // v := f.Value.Eval()
2689
- // _, args := v.Expr()
2690
- // v = args[1].Lookup("t")
2691
- // v = Dereference(v)
2692
- // return v
2693
- // },
2648
+ #T: {
2649
+ a: #S
2650
+ #S: {}
2651
+ }
2652
+ ` ,
2653
+ want : "#T.#S" ,
2654
+ lookup : func (inst * Instance ) Value {
2655
+ f , _ := inst .Value ().LookupField ("#A" )
2656
+ v := f .Value .Lookup ("b" )
2657
+ v = Dereference (v )
2658
+ v = v .Lookup ("a" )
2659
+ return v
2660
+ },
2661
+ }, {
2662
+ input : `
2663
+ #Tracing: {
2664
+ #T: { address?: string }
2665
+ #S: { ip?: string }
2666
+
2667
+ close({}) | close({
2668
+ t: #T
2669
+ }) | close({
2670
+ s: #S
2671
+ })
2672
+ }
2673
+ #X: {}
2674
+ #X // Disconnect top-level struct from the one visible by close.
2675
+ ` ,
2676
+ want : "#Tracing.#T" ,
2677
+ lookup : func (inst * Instance ) Value {
2678
+ f , _ := inst .Value ().LookupField ("#Tracing" )
2679
+ v := f .Value .Eval ()
2680
+ _ , args := v .Expr ()
2681
+ v = args [1 ]
2682
+ v = v .Lookup ("t" )
2683
+ return v
2684
+ },
2694
2685
}}
2695
2686
for _ , tc := range testCases {
2696
2687
if tc .skip {
0 commit comments