this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/core/...: fix various attributes printing issues

- properly unwrap comprehensions
- actually call attribute extraction code in "complex struct" path

Fixes #1826

Issue #1458

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I9c650560636112485c81d4226bd90fe450dee60d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/543413
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>

+196 -5
+35 -1
cue/attribute_test.go
··· 32 32 3 33 33 } @field(foo) 34 34 35 + c1: {} @step(1) 36 + if true { 37 + c2: { @step(2a) } @step(2b) 38 + @step(2c) 39 + } 40 + c3: {} @step(3) 41 + if false { 42 + c4: { @step(4a) } @step(4b) 43 + @step(4c) 44 + } 35 45 ` 36 46 37 47 testCases := []struct { ··· 58 68 flags: ValueAttr, 59 69 path: "b", 60 70 out: "[@field(foo) @embed(foo)]", 71 + }, { 72 + flags: ValueAttr, 73 + path: "c1", 74 + out: "[@step(1)]", 75 + }, { 76 + flags: DeclAttr, 77 + path: "c2", 78 + out: "[@step(2a)]", 79 + }, { 80 + flags: FieldAttr, 81 + path: "c2", 82 + out: "[@step(2b)]", 83 + }, { 84 + flags: DeclAttr, 85 + path: "", 86 + out: "[@step(2c)]", 87 + }, { 88 + flags: ValueAttr | FieldAttr, 89 + path: "c3", 90 + out: "[@step(3)]", 91 + }, { 92 + flags: ValueAttr | FieldAttr, 93 + path: "c4", 94 + out: "[]", 61 95 }} 62 96 for _, tc := range testCases { 63 - t.Run("", func(t *testing.T) { 97 + t.Run(tc.path, func(t *testing.T) { 64 98 v := getInstance(t, config).Value().LookupPath(ParsePath(tc.path)) 65 99 a := v.Attributes(tc.flags) 66 100 got := fmt.Sprint(a)
+4
internal/core/export/adt.go
··· 412 412 e.setField(x.Label, f) 413 413 414 414 f.Value = e.expr(env, x.Value) 415 + f.Attrs = extractFieldAttrs(nil, x) 415 416 416 417 // extractDocs(nil) 417 418 return f ··· 426 427 e.setField(x.Label, f) 427 428 428 429 f.Value = e.expr(env, x.Value) 430 + f.Attrs = extractFieldAttrs(nil, x) 429 431 430 432 // extractDocs(nil) 431 433 return f ··· 455 457 } 456 458 457 459 f.Value = e.expr(env, x.Value) 460 + f.Attrs = extractFieldAttrs(nil, x) 458 461 459 462 return f 460 463 ··· 498 501 // extractDocs(nil) 499 502 500 503 f.Value = e.expr(env, x.Value) 504 + f.Attrs = extractFieldAttrs(nil, x) 501 505 502 506 return f 503 507
+1 -1
internal/core/export/expr.go
··· 267 267 } 268 268 if x.cfg.ShowAttributes { 269 269 for _, c := range a { 270 - d.Attrs = extractFieldAttrs(d.Attrs, c) 270 + d.Attrs = extractFieldAttrs(d.Attrs, c.Field()) 271 271 } 272 272 } 273 273 s.Elts = append(s.Elts, d)
+8 -3
internal/core/export/extract.go
··· 144 144 145 145 func ExtractFieldAttrs(v *adt.Vertex) (attrs []*ast.Attribute) { 146 146 for _, x := range v.Conjuncts { 147 - attrs = extractFieldAttrs(attrs, x) 147 + attrs = extractFieldAttrs(attrs, x.Field()) 148 148 } 149 149 return attrs 150 150 } 151 151 152 - func extractFieldAttrs(attrs []*ast.Attribute, c adt.Conjunct) []*ast.Attribute { 153 - if f, ok := c.Source().(*ast.Field); ok { 152 + // extractFieldAttrs extracts the fields from n and appends unique entries to 153 + // attrs. 154 + // 155 + // The value of n should be obtained from the Conjunct.Field method if the 156 + // source for n is a Conjunct so that Comprehensions are properly unwrapped. 157 + func extractFieldAttrs(attrs []*ast.Attribute, n adt.Node) []*ast.Attribute { 158 + if f, ok := n.Source().(*ast.Field); ok { 154 159 for _, a := range f.Attrs { 155 160 if !containsAttr(attrs, a) { 156 161 attrs = append(attrs, a)
+148
internal/core/export/testdata/main/attrs.txtar
··· 43 43 c: a & {@decl2()} 44 44 d: {a, @decl2()} 45 45 } 46 + 47 + comprehensions: { 48 + @step(0) 49 + c1: {} @step(1) 50 + if true { 51 + c2: { @step(2a) } @step(2b) 52 + @step(2c) 53 + } 54 + c3: {} @step(3) 55 + if false { 56 + c4: { @step(4a) } @step(4b) 57 + @step(4c) 58 + } 59 + } 60 + 61 + dynamicComplex: { 62 + a: "foo" 63 + 64 + (a): "foo" @step(1) 65 + 66 + [string]: "foo" @step(2) 67 + 68 + a?: "foo" @step(3) 69 + 70 + b?: "foo" @step(4) 71 + 72 + if true {} // trigger isComplex 73 + } 74 + 75 + dynamicSimple: { 76 + a: "foo" 77 + 78 + a?: "foo" @step(3) 79 + 80 + b?: "foo" @step(4) 81 + } 46 82 -- b.cue -- 47 83 @package("b") 48 84 ··· 104 140 a 105 141 } 106 142 } 143 + comprehensions: { 144 + @step(0) 145 + c1: {} @step(1) 146 + if true { 147 + @step(2c) 148 + c2: { 149 + @step(2a) 150 + } @step(2b) 151 + } 152 + c3: {} @step(3) 153 + if false { 154 + @step(4c) 155 + c4: { 156 + @step(4a) 157 + } @step(4b) 158 + } 159 + } 160 + dynamicComplex: { 161 + a: "foo" 162 + (a): "foo" @step(1) 163 + [string]: "foo" @step(2) 164 + a?: "foo" @step(3) 165 + b?: "foo" @step(4) 166 + if true {} 167 + } 168 + dynamicSimple: { 169 + a: "foo" @step(3) 170 + b?: "foo" @step(4) 171 + } 107 172 -- out/doc -- 108 173 [] 109 174 [a] ··· 126 191 [dontMergeForDef b x] 127 192 [dontMergeForDef c] 128 193 [dontMergeForDef d] 194 + [comprehensions] 195 + [comprehensions c2] 196 + [comprehensions c1] 197 + [comprehensions c3] 198 + [dynamicComplex] 199 + [dynamicComplex a] 200 + [dynamicComplex foo] 201 + [dynamicSimple] 202 + [dynamicSimple a] 129 203 -- out/value -- 130 204 == Simplified 131 205 { ··· 150 224 c: {} 151 225 d: {} 152 226 } 227 + comprehensions: { 228 + c1: {} 229 + c2: {} 230 + c3: {} 231 + } 232 + dynamicComplex: { 233 + foo: "foo" 234 + a: "foo" 235 + } 236 + dynamicSimple: { 237 + a: "foo" 238 + } 153 239 } 154 240 == Raw 155 241 { ··· 178 264 c: {} 179 265 d: {} 180 266 } 267 + comprehensions: { 268 + c1: {} 269 + c2: {} 270 + c3: {} 271 + } 272 + dynamicComplex: { 273 + foo: "foo" 274 + a: "foo" 275 + b?: "foo" 276 + } 277 + dynamicSimple: { 278 + a: "foo" 279 + b?: "foo" 280 + } 181 281 } 182 282 == Final 183 283 { ··· 202 302 c: {} 203 303 d: {} 204 304 } 305 + comprehensions: { 306 + c1: {} 307 + c2: {} 308 + c3: {} 309 + } 310 + dynamicComplex: { 311 + foo: "foo" 312 + a: "foo" 313 + } 314 + dynamicSimple: { 315 + a: "foo" 316 + } 205 317 } 206 318 == All 207 319 { ··· 250 362 d: { 251 363 @decl2(), @decl1() 252 364 } 365 + } 366 + comprehensions: { 367 + @step(0) 368 + @step(2c) 369 + c1: {} @step(1) 370 + c2: { 371 + @step(2a) 372 + } @step(2b) 373 + c3: {} @step(3) 374 + } 375 + dynamicComplex: { 376 + foo: "foo" @step(1) @step(2) 377 + a: "foo" @step(3) @step(2) 378 + b?: "foo" @step(4) @step(2) 379 + } 380 + dynamicSimple: { 381 + a: "foo" @step(3) 382 + b?: "foo" @step(4) 253 383 } 254 384 } 255 385 == Eval ··· 297 427 d: { 298 428 @decl2(), @decl1() 299 429 } 430 + } 431 + comprehensions: { 432 + @step(0) 433 + @step(2c) 434 + c1: {} @step(1) 435 + c2: { 436 + @step(2a) 437 + } @step(2b) 438 + c3: {} @step(3) 439 + } 440 + dynamicComplex: { 441 + foo: "foo" @step(1) @step(2) 442 + a: "foo" @step(3) @step(2) 443 + b?: "foo" @step(4) @step(2) 444 + } 445 + dynamicSimple: { 446 + a: "foo" @step(3) 447 + b?: "foo" @step(4) 300 448 } 301 449 }