File tree Expand file tree Collapse file tree 2 files changed +69
-3
lines changed Expand file tree Collapse file tree 2 files changed +69
-3
lines changed Original file line number Diff line number Diff line change @@ -1107,12 +1107,33 @@ func (v Value) Err() error {
1107
1107
}
1108
1108
1109
1109
// Pos returns position information.
1110
+ //
1111
+ // Use v.Expr to get positions for all conjuncts and disjuncts.
1110
1112
func (v Value ) Pos () token.Pos {
1111
- if v .v == nil || v . Source () == nil {
1113
+ if v .v == nil {
1112
1114
return token .NoPos
1113
1115
}
1114
- pos := v .Source ().Pos ()
1115
- return pos
1116
+
1117
+ if src := v .Source (); src != nil {
1118
+ if pos := src .Pos (); pos != token .NoPos {
1119
+ return pos
1120
+ }
1121
+ }
1122
+ // Pick the most-concrete field.
1123
+ var p token.Pos
1124
+ for _ , c := range v .v .Conjuncts {
1125
+ x := c .Elem ()
1126
+ pp := pos (x )
1127
+ if pp == token .NoPos {
1128
+ continue
1129
+ }
1130
+ p = pp
1131
+ // Prefer struct conjuncts with actual fields.
1132
+ if s , ok := x .(* adt.StructLit ); ok && len (s .Fields ) > 0 {
1133
+ break
1134
+ }
1135
+ }
1136
+ return p
1116
1137
}
1117
1138
1118
1139
// TODO: IsFinal: this value can never be changed.
Original file line number Diff line number Diff line change @@ -2918,6 +2918,51 @@ func TestReferencePath(t *testing.T) {
2918
2918
}
2919
2919
}
2920
2920
2921
+ func TestPos (t * testing.T ) {
2922
+ testCases := []struct {
2923
+ value string
2924
+ pos string
2925
+ }{{
2926
+ value : `
2927
+ a: string
2928
+ a: "foo"` ,
2929
+ pos : "3:4" ,
2930
+ }, {
2931
+ value : `
2932
+ a: x: string
2933
+ a: x: "x"` ,
2934
+ pos : "2:4" ,
2935
+ }, {
2936
+ // Prefer struct conjuncts with actual fields.
2937
+ value : `
2938
+ a: [string]: string
2939
+ a: x: "x"` ,
2940
+ pos : "3:4" ,
2941
+ }, {
2942
+ value : `
2943
+ a: [string]: [string]: string
2944
+ a: x: y: "x"` ,
2945
+ pos : "3:4" ,
2946
+ }, {
2947
+ value : `
2948
+ a: [string]: [string]: [string]: string
2949
+ a: x: y: z: "x"` ,
2950
+ pos : "3:4" ,
2951
+ }}
2952
+ for _ , tc := range testCases {
2953
+ t .Run ("" , func (t * testing.T ) {
2954
+ var c Context
2955
+ c .runtime ().Init ()
2956
+ v := c .CompileString (tc .value )
2957
+ v = v .LookupPath (ParsePath ("a" ))
2958
+ pos := v .Pos ().String ()
2959
+ if pos != tc .pos {
2960
+ t .Errorf ("got %v; want %v" , pos , tc .pos )
2961
+ }
2962
+ })
2963
+ }
2964
+ }
2965
+
2921
2966
func TestPathCorrection (t * testing.T ) {
2922
2967
testCases := []struct {
2923
2968
input string
You can’t perform that action at this time.
0 commit comments