cuelang.org/go@v0.10.1/cue/types.go (about) 1 // Copyright 2018 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cue 16 17 import ( 18 "bytes" 19 "encoding/json" 20 "fmt" 21 "io" 22 "math" 23 "math/big" 24 "strings" 25 26 "github.com/cockroachdb/apd/v3" 27 28 "cuelang.org/go/cue/ast" 29 "cuelang.org/go/cue/build" 30 "cuelang.org/go/cue/errors" 31 "cuelang.org/go/cue/token" 32 "cuelang.org/go/internal" 33 "cuelang.org/go/internal/core/adt" 34 "cuelang.org/go/internal/core/compile" 35 "cuelang.org/go/internal/core/convert" 36 "cuelang.org/go/internal/core/eval" 37 "cuelang.org/go/internal/core/export" 38 "cuelang.org/go/internal/core/runtime" 39 "cuelang.org/go/internal/core/subsume" 40 "cuelang.org/go/internal/core/validate" 41 internaljson "cuelang.org/go/internal/encoding/json" 42 "cuelang.org/go/internal/types" 43 ) 44 45 // Kind determines the underlying type of a Value. 46 type Kind = adt.Kind 47 48 const ( 49 // BottomKind represents the bottom value. 50 BottomKind Kind = adt.BottomKind 51 52 // NullKind indicates a null value. 53 NullKind Kind = adt.NullKind 54 55 // BoolKind indicates a boolean value. 56 BoolKind Kind = adt.BoolKind 57 58 // IntKind represents an integral number. 59 IntKind Kind = adt.IntKind 60 61 // FloatKind represents a decimal float point number that cannot be 62 // converted to an integer. The underlying number may still be integral, 63 // but resulting from an operation that enforces the float type. 64 FloatKind Kind = adt.FloatKind 65 66 // StringKind indicates any kind of string. 67 StringKind Kind = adt.StringKind 68 69 // BytesKind is a blob of data. 70 BytesKind Kind = adt.BytesKind 71 72 // StructKind is a kev-value map. 73 StructKind Kind = adt.StructKind 74 75 // ListKind indicates a list of values. 76 ListKind Kind = adt.ListKind 77 78 // _numberKind is used as a implementation detail inside 79 // Kind.String to indicate NumberKind. 80 81 // NumberKind represents any kind of number. 82 NumberKind Kind = adt.NumberKind 83 84 // TopKind represents the top value. 85 TopKind Kind = adt.TopKind 86 ) 87 88 // An structValue represents a JSON object. 89 // 90 // TODO: remove 91 type structValue struct { 92 ctx *adt.OpContext 93 v Value 94 obj *adt.Vertex 95 arcs []*adt.Vertex 96 } 97 98 type hiddenStructValue = structValue 99 100 // Len reports the number of fields in this struct. 101 func (o *hiddenStructValue) Len() int { 102 if o.obj == nil { 103 return 0 104 } 105 return len(o.arcs) 106 } 107 108 // At reports the key and value of the ith field, i < o.Len(). 109 func (o *hiddenStructValue) At(i int) (key string, v Value) { 110 arc := o.arcs[i] 111 return o.v.idx.LabelStr(arc.Label), newChildValue(o, i) 112 } 113 114 func (o *hiddenStructValue) at(i int) *adt.Vertex { 115 return o.arcs[i] 116 } 117 118 // Lookup reports the field for the given key. The returned Value is invalid 119 // if it does not exist. 120 func (o *hiddenStructValue) Lookup(key string) Value { 121 f := o.v.idx.StrLabel(key) 122 i := 0 123 len := o.Len() 124 for ; i < len; i++ { 125 if o.arcs[i].Label == f { 126 break 127 } 128 } 129 if i == len { 130 x := mkErr(o.v.idx, o.obj, 0, "field not found: %v", key) 131 x.NotExists = true 132 // TODO: more specifically we should test whether the values that 133 // are addressable from the root of the configuration can support the 134 // looked up value. This will avoid false positives such as when 135 // an open literal struct is passed to a builtin. 136 if o.obj.Accept(o.ctx, f) { 137 x.Code = adt.IncompleteError 138 } 139 return newErrValue(o.v, x) 140 } 141 return newChildValue(o, i) 142 } 143 144 // MarshalJSON returns a valid JSON encoding or reports an error if any of the 145 // fields is invalid. 146 func (o *structValue) marshalJSON() (b []byte, err errors.Error) { 147 b = append(b, '{') 148 n := o.Len() 149 for i := range n { 150 k, v := o.At(i) 151 s, err := internaljson.Marshal(k) 152 if err != nil { 153 return nil, unwrapJSONError(err) 154 } 155 b = append(b, s...) 156 b = append(b, ':') 157 bb, err := internaljson.Marshal(v) 158 if err != nil { 159 return nil, unwrapJSONError(err) 160 } 161 b = append(b, bb...) 162 if i < n-1 { 163 b = append(b, ',') 164 } 165 } 166 b = append(b, '}') 167 return b, nil 168 } 169 170 var _ errors.Error = &marshalError{} 171 172 type marshalError struct { 173 err errors.Error 174 b *adt.Bottom 175 } 176 177 func toMarshalErr(v Value, b *adt.Bottom) error { 178 return &marshalError{v.toErr(b), b} 179 } 180 181 func marshalErrf(v Value, src adt.Node, code adt.ErrorCode, msg string, args ...interface{}) error { 182 arguments := append([]interface{}{code, msg}, args...) 183 b := mkErr(v.idx, src, arguments...) 184 return toMarshalErr(v, b) 185 } 186 187 func (e *marshalError) Error() string { 188 return fmt.Sprintf("cue: marshal error: %v", e.err) 189 } 190 191 func (e *marshalError) Bottom() *adt.Bottom { return e.b } 192 func (e *marshalError) Path() []string { return e.err.Path() } 193 func (e *marshalError) Msg() (string, []interface{}) { return e.err.Msg() } 194 func (e *marshalError) Position() token.Pos { return e.err.Position() } 195 func (e *marshalError) InputPositions() []token.Pos { 196 return e.err.InputPositions() 197 } 198 199 func unwrapJSONError(err error) errors.Error { 200 switch x := err.(type) { 201 case *json.MarshalerError: 202 return unwrapJSONError(x.Err) 203 case *marshalError: 204 return x 205 case errors.Error: 206 return &marshalError{x, nil} 207 default: 208 return &marshalError{errors.Wrapf(err, token.NoPos, "json error"), nil} 209 } 210 } 211 212 // An Iterator iterates over values. 213 type Iterator struct { 214 val Value 215 idx *runtime.Runtime 216 ctx *adt.OpContext 217 arcs []*adt.Vertex 218 p int 219 cur Value 220 f adt.Feature 221 arcType adt.ArcType 222 } 223 224 type hiddenIterator = Iterator 225 226 // Next advances the iterator to the next value and reports whether there was any. 227 // It must be called before the first call to [Iterator.Value] or [Iterator.Selector]. 228 func (i *Iterator) Next() bool { 229 if i.p >= len(i.arcs) { 230 i.cur = Value{} 231 return false 232 } 233 arc := i.arcs[i.p] 234 arc.Finalize(i.ctx) 235 p := linkParent(i.val.parent_, i.val.v, arc) 236 i.f = arc.Label 237 i.arcType = arc.ArcType 238 i.cur = makeValue(i.val.idx, arc, p) 239 i.p++ 240 return true 241 } 242 243 // Value returns the current value in the list. 244 // It will panic if [Iterator.Next] advanced past the last entry. 245 func (i *Iterator) Value() Value { 246 return i.cur 247 } 248 249 // Selector reports the field label of this iteration. 250 func (i *Iterator) Selector() Selector { 251 sel := featureToSel(i.f, i.idx) 252 // Only call wrapConstraint if there is any constraint type to wrap with. 253 if ctype := fromArcType(i.arcType); ctype != 0 { 254 sel = wrapConstraint(sel, ctype) 255 } 256 return sel 257 } 258 259 // Label reports the label of the value if i iterates over struct fields and "" 260 // otherwise. 261 // 262 // Slated to be deprecated: use [Iterator.Selector] and [Selector.String]. 263 // Note that this will give more accurate string representations. 264 func (i *hiddenIterator) Label() string { 265 if i.f == 0 { 266 return "" 267 } 268 return i.idx.LabelStr(i.f) 269 } 270 271 // IsHidden reports if a field is hidden from the data model. 272 // 273 // Deprecated: use i.Selector().PkgPath() != "" 274 func (i *hiddenIterator) IsHidden() bool { 275 return i.f.IsHidden() 276 } 277 278 // IsOptional reports if a field is optional. 279 func (i *Iterator) IsOptional() bool { 280 return i.arcType == adt.ArcOptional 281 } 282 283 // FieldType reports the type of the field. 284 func (i *Iterator) FieldType() SelectorType { 285 return featureToSelType(i.f, i.arcType) 286 } 287 288 // IsDefinition reports if a field is a definition. 289 // 290 // Deprecated: use i.Selector().IsDefinition() 291 func (i *hiddenIterator) IsDefinition() bool { 292 return i.f.IsDef() 293 } 294 295 // marshalJSON iterates over the list and generates JSON output. HasNext 296 // will return false after this operation. 297 func marshalList(l *Iterator) (b []byte, err errors.Error) { 298 b = append(b, '[') 299 if l.Next() { 300 for i := 0; ; i++ { 301 x, err := internaljson.Marshal(l.Value()) 302 if err != nil { 303 return nil, unwrapJSONError(err) 304 } 305 b = append(b, x...) 306 if !l.Next() { 307 break 308 } 309 b = append(b, ',') 310 } 311 } 312 b = append(b, ']') 313 return b, nil 314 } 315 316 func (v Value) getNum(k adt.Kind) (*adt.Num, errors.Error) { 317 v, _ = v.Default() 318 ctx := v.ctx() 319 if err := v.checkKind(ctx, k); err != nil { 320 return nil, v.toErr(err) 321 } 322 n, _ := v.eval(ctx).(*adt.Num) 323 return n, nil 324 } 325 326 // MantExp breaks x into its mantissa and exponent components and returns the 327 // exponent. If a non-nil mant argument is provided its value is set to the 328 // mantissa of x. The components satisfy x == mant × 10**exp. It returns an 329 // error if v is not a number. 330 // 331 // The components are not normalized. For instance, 2.00 is represented mant == 332 // 200 and exp == -2. Calling MantExp with a nil argument is an efficient way to 333 // get the exponent of the receiver. 334 func (v Value) MantExp(mant *big.Int) (exp int, err error) { 335 n, err := v.getNum(adt.NumberKind) 336 if err != nil { 337 return 0, err 338 } 339 if n.X.Form != 0 { 340 return 0, ErrInfinite 341 } 342 if mant != nil { 343 mant.Set(n.X.Coeff.MathBigInt()) 344 if n.X.Negative { 345 mant.Neg(mant) 346 } 347 } 348 return int(n.X.Exponent), nil 349 } 350 351 // Decimal is for internal use only. The Decimal type that is returned is 352 // subject to change. 353 func (v hiddenValue) Decimal() (d *internal.Decimal, err error) { 354 n, err := v.getNum(adt.NumberKind) 355 if err != nil { 356 return nil, err 357 } 358 return &n.X, nil 359 } 360 361 // AppendInt appends the string representation of x in the given base to buf and 362 // returns the extended buffer, or an error if the underlying number was not 363 // an integer. 364 func (v Value) AppendInt(buf []byte, base int) ([]byte, error) { 365 i, err := v.Int(nil) 366 if err != nil { 367 return nil, err 368 } 369 return i.Append(buf, base), nil 370 } 371 372 // AppendFloat appends to buf the string form of the floating-point number x. 373 // It returns an error if v is not a number. 374 func (v Value) AppendFloat(buf []byte, fmt byte, prec int) ([]byte, error) { 375 n, err := v.getNum(adt.NumberKind) 376 if err != nil { 377 return nil, err 378 } 379 ctx := internal.BaseContext 380 nd := int(apd.NumDigits(&n.X.Coeff)) + int(n.X.Exponent) 381 if n.X.Form == apd.Infinite { 382 if n.X.Negative { 383 buf = append(buf, '-') 384 } 385 return append(buf, string('∞')...), nil 386 } 387 if fmt == 'f' && nd > 0 { 388 ctx = ctx.WithPrecision(uint32(nd + prec)) 389 } else { 390 ctx = ctx.WithPrecision(uint32(prec)) 391 } 392 var d apd.Decimal 393 ctx.Round(&d, &n.X) 394 return d.Append(buf, fmt), nil 395 } 396 397 var ( 398 // ErrBelow indicates that a value was rounded down in a conversion. 399 ErrBelow = errors.New("value was rounded down") 400 401 // ErrAbove indicates that a value was rounded up in a conversion. 402 ErrAbove = errors.New("value was rounded up") 403 404 // ErrInfinite indicates that a value is infinite. 405 ErrInfinite = errors.New("infinite") 406 ) 407 408 // Int converts the underlying integral number to an big.Int. It reports an 409 // error if the underlying value is not an integer type. If a non-nil *Int 410 // argument z is provided, Int stores the result in z instead of allocating a 411 // new Int. 412 func (v Value) Int(z *big.Int) (*big.Int, error) { 413 n, err := v.getNum(adt.IntKind) 414 if err != nil { 415 return nil, err 416 } 417 if z == nil { 418 z = &big.Int{} 419 } 420 if n.X.Exponent != 0 { 421 panic("cue: exponent should always be nil for integer types") 422 } 423 z.Set(n.X.Coeff.MathBigInt()) 424 if n.X.Negative { 425 z.Neg(z) 426 } 427 return z, nil 428 } 429 430 // Int64 converts the underlying integral number to int64. It reports an 431 // error if the underlying value is not an integer type or cannot be represented 432 // as an int64. The result is (math.MinInt64, ErrAbove) for x < math.MinInt64, 433 // and (math.MaxInt64, ErrBelow) for x > math.MaxInt64. 434 func (v Value) Int64() (int64, error) { 435 n, err := v.getNum(adt.IntKind) 436 if err != nil { 437 return 0, err 438 } 439 if !n.X.Coeff.IsInt64() { 440 if n.X.Negative { 441 return math.MinInt64, ErrAbove 442 } 443 return math.MaxInt64, ErrBelow 444 } 445 i := n.X.Coeff.Int64() 446 if n.X.Negative { 447 i = -i 448 } 449 return i, nil 450 } 451 452 // Uint64 converts the underlying integral number to uint64. It reports an 453 // error if the underlying value is not an integer type or cannot be represented 454 // as a uint64. The result is (0, ErrAbove) for x < 0, and 455 // (math.MaxUint64, ErrBelow) for x > math.MaxUint64. 456 func (v Value) Uint64() (uint64, error) { 457 n, err := v.getNum(adt.IntKind) 458 if err != nil { 459 return 0, err 460 } 461 if n.X.Negative { 462 return 0, ErrAbove 463 } 464 if !n.X.Coeff.IsUint64() { 465 return math.MaxUint64, ErrBelow 466 } 467 i := n.X.Coeff.Uint64() 468 return i, nil 469 } 470 471 var ( 472 smallestPosFloat64 *apd.Decimal 473 smallestNegFloat64 *apd.Decimal 474 maxPosFloat64 *apd.Decimal 475 maxNegFloat64 *apd.Decimal 476 ) 477 478 func init() { 479 const ( 480 // math.SmallestNonzeroFloat64: 1 / 2**(1023 - 1 + 52) 481 smallest = "4.940656458412465441765687928682213723651e-324" 482 // math.MaxFloat64: 2**1023 * (2**53 - 1) / 2**52 483 max = "1.797693134862315708145274237317043567981e+308" 484 ) 485 ctx := internal.BaseContext.WithPrecision(40) 486 487 var err error 488 smallestPosFloat64, _, err = ctx.NewFromString(smallest) 489 if err != nil { 490 panic(err) 491 } 492 smallestNegFloat64, _, err = ctx.NewFromString("-" + smallest) 493 if err != nil { 494 panic(err) 495 } 496 maxPosFloat64, _, err = ctx.NewFromString(max) 497 if err != nil { 498 panic(err) 499 } 500 maxNegFloat64, _, err = ctx.NewFromString("-" + max) 501 if err != nil { 502 panic(err) 503 } 504 } 505 506 // Float64 returns the float64 value nearest to x. It reports an error if v is 507 // not a number. If x is too small to be represented by a float64 (|x| < 508 // math.SmallestNonzeroFloat64), the result is (0, ErrBelow) or (-0, ErrAbove), 509 // respectively, depending on the sign of x. If x is too large to be represented 510 // by a float64 (|x| > math.MaxFloat64), the result is (+Inf, ErrAbove) or 511 // (-Inf, ErrBelow), depending on the sign of x. 512 func (v Value) Float64() (float64, error) { 513 n, err := v.getNum(adt.NumberKind) 514 if err != nil { 515 return 0, err 516 } 517 if n.X.IsZero() { 518 return 0.0, nil 519 } 520 if n.X.Negative { 521 if n.X.Cmp(smallestNegFloat64) == 1 { 522 return -0, ErrAbove 523 } 524 if n.X.Cmp(maxNegFloat64) == -1 { 525 return math.Inf(-1), ErrBelow 526 } 527 } else { 528 if n.X.Cmp(smallestPosFloat64) == -1 { 529 return 0, ErrBelow 530 } 531 if n.X.Cmp(maxPosFloat64) == 1 { 532 return math.Inf(1), ErrAbove 533 } 534 } 535 f, _ := n.X.Float64() 536 return f, nil 537 } 538 539 // Value holds any value, which may be a Boolean, Error, List, Null, Number, 540 // Struct, or String. 541 type Value struct { 542 idx *runtime.Runtime 543 v *adt.Vertex 544 // Parent keeps track of the parent if the value corresponding to v.Parent 545 // differs, recursively. 546 parent_ *parent 547 } 548 549 // parent is a distinct type from Value to ensure more type safety: Value 550 // is typically used by value, so taking a pointer to it has a high risk 551 // or globbering the contents. 552 type parent struct { 553 v *adt.Vertex 554 p *parent 555 } 556 557 func (v Value) parent() Value { 558 switch { 559 case v.v == nil: 560 return Value{} 561 case v.parent_ != nil: 562 return Value{v.idx, v.parent_.v, v.parent_.p} 563 default: 564 return Value{v.idx, v.v.Parent, nil} 565 } 566 } 567 568 type valueScope Value 569 570 func (v valueScope) Vertex() *adt.Vertex { return v.v } 571 func (v valueScope) Parent() compile.Scope { 572 p := Value(v).parent() 573 if p.v == nil { 574 return nil 575 } 576 return valueScope(p) 577 } 578 579 type hiddenValue = Value 580 581 // Core is for internal use only. 582 func (v hiddenValue) Core(x *types.Value) { 583 x.V = v.v 584 x.R = v.idx 585 } 586 587 func newErrValue(v Value, b *adt.Bottom) Value { 588 node := &adt.Vertex{BaseValue: b} 589 if v.v != nil { 590 node.Label = v.v.Label 591 node.Parent = v.v.Parent 592 } 593 node.ForceDone() 594 node.AddConjunct(adt.MakeRootConjunct(nil, b)) 595 return makeChildValue(v.parent(), node) 596 } 597 598 func newVertexRoot(idx *runtime.Runtime, ctx *adt.OpContext, x *adt.Vertex) Value { 599 if ctx != nil { 600 // This is indicative of an zero Value. In some cases this is called 601 // with an error value. 602 x.Finalize(ctx) 603 } else { 604 x.ForceDone() 605 } 606 return makeValue(idx, x, nil) 607 } 608 609 func newValueRoot(idx *runtime.Runtime, ctx *adt.OpContext, x adt.Expr) Value { 610 if n, ok := x.(*adt.Vertex); ok { 611 return newVertexRoot(idx, ctx, n) 612 } 613 node := &adt.Vertex{} 614 node.AddConjunct(adt.MakeRootConjunct(nil, x)) 615 return newVertexRoot(idx, ctx, node) 616 } 617 618 func newChildValue(o *structValue, i int) Value { 619 arc := o.at(i) 620 // TODO: fix linkage to parent. 621 return makeValue(o.v.idx, arc, linkParent(o.v.parent_, o.v.v, arc)) 622 } 623 624 // Dereference reports the value v refers to if v is a reference or v itself 625 // otherwise. 626 func Dereference(v Value) Value { 627 n := v.v 628 if n == nil { 629 return v 630 } 631 632 c, count := n.SingleConjunct() 633 if count != 1 { 634 return v 635 } 636 637 env, expr := c.EnvExpr() 638 639 // TODO: consider supporting unwrapping of structs or comprehensions around 640 // a single embedded reference. 641 r, _ := expr.(adt.Resolver) 642 if r == nil { 643 return v 644 } 645 646 c = adt.MakeRootConjunct(env, expr) 647 648 ctx := v.ctx() 649 n, b := ctx.Resolve(c, r) 650 if b != nil { 651 return newErrValue(v, b) 652 } 653 n.Finalize(ctx) 654 // NOTE: due to structure sharing, the path of the referred node may end 655 // up different from the one explicitly pointed to. The value will be the 656 // same, but the scope may differ. 657 // TODO(structureshare): see if we can construct the original path. This 658 // only has to be done if structures are being shared. 659 return makeValue(v.idx, n, nil) 660 } 661 662 func makeValue(idx *runtime.Runtime, v *adt.Vertex, p *parent) Value { 663 if v.Status() == 0 || v.BaseValue == nil { 664 panic(fmt.Sprintf("not properly initialized (state: %v, value: %T)", 665 v.Status(), v.BaseValue)) 666 } 667 return Value{idx, v, p} 668 } 669 670 // makeChildValue makes a new value, of which p is the parent, and links the 671 // parent pointer to p if necessary. 672 func makeChildValue(p Value, arc *adt.Vertex) Value { 673 return makeValue(p.idx, arc, linkParent(p.parent_, p.v, arc)) 674 } 675 676 // linkParent creates the parent struct for an arc, if necessary. 677 // 678 // The parent struct is necessary if the parent struct also has a parent struct, 679 // or if arc is (structurally) shared and does not have node as a parent. 680 func linkParent(p *parent, node, arc *adt.Vertex) *parent { 681 if p == nil && node == arc.Parent { 682 return nil 683 } 684 return &parent{node, p} 685 } 686 687 func remakeValue(base Value, env *adt.Environment, v adt.Expr) Value { 688 // TODO: right now this is necessary because disjunctions do not have 689 // populated conjuncts. 690 if v, ok := v.(*adt.Vertex); ok && !v.IsUnprocessed() { 691 return Value{base.idx, v, nil} 692 } 693 n := &adt.Vertex{Label: base.v.Label} 694 n.AddConjunct(adt.MakeRootConjunct(env, v)) 695 n = manifest(base.ctx(), n) 696 n.Parent = base.v.Parent 697 return makeChildValue(base.parent(), n) 698 } 699 700 func remakeFinal(base Value, env *adt.Environment, v adt.Value) Value { 701 n := &adt.Vertex{Parent: base.v.Parent, Label: base.v.Label, BaseValue: v} 702 n.ForceDone() 703 return makeChildValue(base.parent(), n) 704 } 705 706 func (v Value) ctx() *adt.OpContext { 707 return newContext(v.idx) 708 } 709 710 // Eval resolves the references of a value and returns the result. 711 // This method is not necessary to obtain concrete values. 712 func (v Value) Eval() Value { 713 if v.v == nil { 714 return v 715 } 716 x := v.v 717 // x = eval.FinalizeValue(v.idx.Runtime, v.v) 718 // x.Finalize(v.ctx()) 719 x = x.ToDataSingle() 720 return makeValue(v.idx, x, v.parent_) 721 // return remakeValue(v, nil, ctx.value(x)) 722 } 723 724 // Default reports the default value and whether it existed. It returns the 725 // normal value if there is no default. 726 func (v Value) Default() (Value, bool) { 727 if v.v == nil { 728 return v, false 729 } 730 731 d := v.v.Default() 732 if d == v.v { 733 return v, false 734 } 735 return makeValue(v.idx, d, v.parent_), true 736 737 // d, ok := v.v.Value.(*adt.Disjunction) 738 // if !ok { 739 // return v, false 740 // } 741 742 // var w *adt.Vertex 743 744 // switch d.NumDefaults { 745 // case 0: 746 // return v, false 747 748 // case 1: 749 // w = d.Values[0] 750 751 // default: 752 // x := *v.v 753 // x.Value = &adt.Disjunction{ 754 // Src: d.Src, 755 // Values: d.Values[:d.NumDefaults], 756 // NumDefaults: 0, 757 // } 758 // w = &x 759 // } 760 761 // w.Conjuncts = nil 762 // for _, c := range v.v.Conjuncts { 763 // // TODO: preserve field information. 764 // expr, _ := stripNonDefaults(c.Expr()) 765 // w.AddConjunct(adt.MakeConjunct(c.Env, expr)) 766 // } 767 768 // return makeValue(v.idx, w), true 769 770 // if !stripped { 771 // return v, false 772 // } 773 774 // n := *v.v 775 // n.Conjuncts = conjuncts 776 // return Value{v.idx, &n}, true 777 778 // isDefault := false 779 // for _, c := range v.v.Conjuncts { 780 // if hasDisjunction(c.Expr()) { 781 // isDefault = true 782 // break 783 // } 784 // } 785 786 // if !isDefault { 787 // return v, false 788 // } 789 790 // TODO: record expanded disjunctions in output. 791 // - Rename Disjunction to DisjunctionExpr 792 // - Introduce Disjuncts with Values. 793 // - In Expr introduce Star 794 // - Don't pick default by default? 795 796 // Evaluate the value. 797 // x := eval.FinalizeValue(v.idx.Runtime, v.v) 798 // if b, _ := x.Value.(*adt.Bottom); b != nil { // && b.IsIncomplete() { 799 // return v, false 800 // } 801 // // Finalize and return here. 802 // return Value{v.idx, x}, isDefault 803 } 804 805 // TODO: this should go: record preexpanded disjunctions in Vertex. 806 func hasDisjunction(expr adt.Expr) bool { 807 switch x := expr.(type) { 808 case *adt.DisjunctionExpr: 809 return true 810 case *adt.Conjunction: 811 for _, v := range x.Values { 812 if hasDisjunction(v) { 813 return true 814 } 815 } 816 case *adt.BinaryExpr: 817 switch x.Op { 818 case adt.OrOp: 819 return true 820 case adt.AndOp: 821 return hasDisjunction(x.X) || hasDisjunction(x.Y) 822 } 823 } 824 return false 825 } 826 827 // TODO: this should go: record preexpanded disjunctions in Vertex. 828 func stripNonDefaults(expr adt.Expr) (r adt.Expr, stripped bool) { 829 switch x := expr.(type) { 830 case *adt.DisjunctionExpr: 831 if !x.HasDefaults { 832 return x, false 833 } 834 d := *x 835 d.Values = []adt.Disjunct{} 836 for _, v := range x.Values { 837 if v.Default { 838 d.Values = append(d.Values, v) 839 } 840 } 841 if len(d.Values) == 1 { 842 return d.Values[0].Val, true 843 } 844 return &d, true 845 846 case *adt.BinaryExpr: 847 if x.Op != adt.AndOp { 848 return x, false 849 } 850 a, sa := stripNonDefaults(x.X) 851 b, sb := stripNonDefaults(x.Y) 852 if sa || sb { 853 bin := *x 854 bin.X = a 855 bin.Y = b 856 return &bin, true 857 } 858 return x, false 859 860 default: 861 return x, false 862 } 863 } 864 865 // Label reports he label used to obtain this value from the enclosing struct. 866 // 867 // TODO: get rid of this somehow. Probably by including a FieldInfo struct 868 // or the like. 869 func (v hiddenValue) Label() (string, bool) { 870 if v.v == nil || v.v.Label == 0 { 871 return "", false 872 } 873 return v.idx.LabelStr(v.v.Label), true 874 } 875 876 // Kind returns the kind of value. It returns BottomKind for atomic values that 877 // are not concrete. For instance, it will return BottomKind for the bounds 878 // >=0. 879 func (v Value) Kind() Kind { 880 if v.v == nil { 881 return BottomKind 882 } 883 c := v.v.BaseValue 884 if !v.v.IsConcrete() { 885 return BottomKind 886 } 887 // TODO: perhaps we should not consider open lists as "incomplete". 888 if v.IncompleteKind() == adt.ListKind && !v.v.IsClosedList() { 889 return BottomKind 890 } 891 return c.Kind() 892 } 893 894 // IncompleteKind returns a mask of all kinds that this value may be. 895 func (v Value) IncompleteKind() Kind { 896 if v.v == nil { 897 return BottomKind 898 } 899 return v.v.Kind() 900 } 901 902 // MarshalJSON marshalls this value into valid JSON. 903 func (v Value) MarshalJSON() (b []byte, err error) { 904 b, err = v.marshalJSON() 905 if err != nil { 906 return nil, unwrapJSONError(err) 907 } 908 return b, nil 909 } 910 911 func (v Value) marshalJSON() (b []byte, err error) { 912 v, _ = v.Default() 913 if v.v == nil { 914 return internaljson.Marshal(nil) 915 } 916 ctx := newContext(v.idx) 917 x := v.eval(ctx) 918 919 if _, ok := x.(adt.Resolver); ok { 920 return nil, marshalErrf(v, x, adt.IncompleteError, "value %q contains unresolved references", str(ctx, x)) 921 } 922 if !adt.IsConcrete(x) { 923 return nil, marshalErrf(v, x, adt.IncompleteError, "cannot convert incomplete value %q to JSON", str(ctx, x)) 924 } 925 926 // TODO: implement marshalles in value. 927 switch k := x.Kind(); k { 928 case adt.NullKind: 929 return internaljson.Marshal(nil) 930 case adt.BoolKind: 931 return internaljson.Marshal(x.(*adt.Bool).B) 932 case adt.IntKind, adt.FloatKind, adt.NumberKind: 933 b, err := x.(*adt.Num).X.MarshalText() 934 b = bytes.TrimLeft(b, "+") 935 return b, err 936 case adt.StringKind: 937 return internaljson.Marshal(x.(*adt.String).Str) 938 case adt.BytesKind: 939 return internaljson.Marshal(x.(*adt.Bytes).B) 940 case adt.ListKind: 941 i, _ := v.List() 942 return marshalList(&i) 943 case adt.StructKind: 944 obj, err := v.structValData(ctx) 945 if err != nil { 946 return nil, toMarshalErr(v, err) 947 } 948 return obj.marshalJSON() 949 case adt.BottomKind: 950 return nil, toMarshalErr(v, x.(*adt.Bottom)) 951 default: 952 return nil, marshalErrf(v, x, 0, "cannot convert value %q of type %T to JSON", str(ctx, x), x) 953 } 954 } 955 956 // Syntax converts the possibly partially evaluated value into syntax. This 957 // can use used to print the value with package format. 958 func (v Value) Syntax(opts ...Option) ast.Node { 959 // TODO: the default should ideally be simplified representation that 960 // exactly represents the value. The latter can currently only be 961 // ensured with Raw(). 962 if v.v == nil { 963 return nil 964 } 965 var o options = getOptions(opts) 966 // var inst *Instance 967 968 p := export.Profile{ 969 Simplify: !o.raw, 970 TakeDefaults: o.final, 971 ShowOptional: !o.omitOptional && !o.concrete, 972 ShowDefinitions: !o.omitDefinitions && !o.concrete, 973 ShowHidden: !o.omitHidden && !o.concrete, 974 ShowAttributes: !o.omitAttrs, 975 ShowDocs: o.docs, 976 ShowErrors: o.showErrors, 977 InlineImports: o.inlineImports, 978 Fragment: o.raw, 979 } 980 981 pkgID := v.instance().ID() 982 983 bad := func(name string, err error) ast.Node { 984 const format = `"%s: internal error 985 Error: %s 986 987 Profile: 988 %#v 989 990 Value: 991 %v 992 993 You could file a bug with the above information at: 994 https://cuelang.org/issues/new?assignees=&labels=NeedsInvestigation&template=bug_report.md&title=. 995 ` 996 cg := &ast.CommentGroup{Doc: true} 997 msg := fmt.Sprintf(format, name, err, p, v) 998 for _, line := range strings.Split(msg, "\n") { 999 cg.List = append(cg.List, &ast.Comment{Text: "// " + line}) 1000 } 1001 x := &ast.BadExpr{} 1002 ast.AddComment(x, cg) 1003 return x 1004 } 1005 1006 // var expr ast.Expr 1007 var err error 1008 var f *ast.File 1009 if o.concrete || o.final || o.resolveReferences { 1010 f, err = p.Vertex(v.idx, pkgID, v.v) 1011 if err != nil { 1012 return bad(`"cuelang.org/go/internal/core/export".Vertex`, err) 1013 } 1014 } else { 1015 p.AddPackage = true 1016 f, err = p.Def(v.idx, pkgID, v.v) 1017 if err != nil { 1018 return bad(`"cuelang.org/go/internal/core/export".Def`, err) 1019 } 1020 } 1021 1022 outer: 1023 for _, d := range f.Decls { 1024 switch d.(type) { 1025 case *ast.Package, *ast.ImportDecl: 1026 return f 1027 case *ast.CommentGroup, *ast.Attribute: 1028 default: 1029 break outer 1030 } 1031 } 1032 1033 if len(f.Decls) == 1 { 1034 if e, ok := f.Decls[0].(*ast.EmbedDecl); ok { 1035 return e.Expr 1036 } 1037 } 1038 return &ast.StructLit{ 1039 Elts: f.Decls, 1040 } 1041 } 1042 1043 // Doc returns all documentation comments associated with the field from which 1044 // the current value originates. 1045 func (v Value) Doc() []*ast.CommentGroup { 1046 if v.v == nil { 1047 return nil 1048 } 1049 return export.ExtractDoc(v.v) 1050 } 1051 1052 // Split returns a list of values from which v originated such that 1053 // the unification of all these values equals v and for all returned values. 1054 // It will also split unchecked unifications (embeddings), so unifying the 1055 // split values may fail if actually unified. 1056 // Source returns a non-nil value. 1057 // 1058 // Deprecated: use [Value.Expr]. 1059 func (v hiddenValue) Split() []Value { 1060 if v.v == nil { 1061 return nil 1062 } 1063 a := []Value{} 1064 v.v.VisitLeafConjuncts(func(x adt.Conjunct) bool { 1065 env, expr := x.EnvExpr() 1066 a = append(a, remakeValue(v, env, expr)) 1067 return true 1068 }) 1069 return a 1070 } 1071 1072 // Source returns the original node for this value. The return value may not 1073 // be an [ast.Expr]. For instance, a struct kind may be represented by a 1074 // struct literal, a field comprehension, or a file. It returns nil for 1075 // computed nodes. Use [Value.Expr] to get all source values that apply to a field. 1076 func (v Value) Source() ast.Node { 1077 if v.v == nil { 1078 return nil 1079 } 1080 count := 0 1081 var src ast.Node 1082 v.v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 1083 src = c.Source() 1084 count++ 1085 return true 1086 }) 1087 if count > 1 || src == nil { 1088 src = v.v.Value().Source() 1089 } 1090 return src 1091 } 1092 1093 // If v exactly represents a package, BuildInstance returns 1094 // the build instance corresponding to the value; otherwise it returns nil. 1095 // 1096 // The value returned by Value.ReferencePath will commonly represent 1097 // a package. 1098 func (v Value) BuildInstance() *build.Instance { 1099 if v.idx == nil { 1100 return nil 1101 } 1102 return v.idx.GetInstanceFromNode(v.v) 1103 } 1104 1105 // Err returns the error represented by v or nil v is not an error. 1106 func (v Value) Err() error { 1107 if err := v.checkKind(v.ctx(), adt.BottomKind); err != nil { 1108 return v.toErr(err) 1109 } 1110 return nil 1111 } 1112 1113 // Pos returns position information. 1114 // 1115 // Use v.Expr to get positions for all conjuncts and disjuncts. 1116 func (v Value) Pos() token.Pos { 1117 if v.v == nil { 1118 return token.NoPos 1119 } 1120 1121 if src := v.Source(); src != nil { 1122 if pos := src.Pos(); pos != token.NoPos { 1123 return pos 1124 } 1125 } 1126 // Pick the most-concrete field. 1127 var p token.Pos 1128 v.v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 1129 x := c.Elem() 1130 pp := pos(x) 1131 if pp == token.NoPos { 1132 return true 1133 } 1134 p = pp 1135 // Prefer struct conjuncts with actual fields. 1136 if s, ok := x.(*adt.StructLit); ok && len(s.Fields) > 0 { 1137 return false 1138 } 1139 return true 1140 }) 1141 return p 1142 } 1143 1144 // TODO: IsFinal: this value can never be changed. 1145 1146 // IsClosed reports whether a list or struct is closed. It reports false when 1147 // the value is not a list or struct. 1148 // 1149 // Deprecated: use Allows(AnyString) and Allows(AnyIndex) or Kind/IncompleteKind. 1150 func (v hiddenValue) IsClosed() bool { 1151 if v.v == nil { 1152 return false 1153 } 1154 switch v.Kind() { 1155 case ListKind: 1156 return v.v.IsClosedList() 1157 case StructKind: 1158 // TODO: remove this more expensive computation once the old evaluator 1159 // is removed. 1160 return !v.Allows(AnyString) 1161 } 1162 return false 1163 } 1164 1165 // Allows reports whether a field with the given selector could be added to v. 1166 // 1167 // Allows does not take into account validators like list.MaxItems(4). This may 1168 // change in the future. 1169 func (v Value) Allows(sel Selector) bool { 1170 if v.v.HasEllipsis { 1171 return true 1172 } 1173 c := v.ctx() 1174 f := sel.sel.feature(c) 1175 return v.v.Accept(c, f) 1176 } 1177 1178 // IsConcrete reports whether the current value is a concrete scalar value 1179 // (not relying on default values), a terminal error, a list, or a struct. 1180 // It does not verify that values of lists or structs are concrete themselves. 1181 // To check whether there is a concrete default, use this method on [Value.Default]. 1182 func (v Value) IsConcrete() bool { 1183 if v.v == nil { 1184 return false // any is neither concrete, not a list or struct. 1185 } 1186 if b := v.v.Bottom(); b != nil { 1187 return !b.IsIncomplete() 1188 } 1189 if !adt.IsConcrete(v.v) { 1190 return false 1191 } 1192 if v.IncompleteKind() == adt.ListKind && !v.v.IsClosedList() { 1193 return false 1194 } 1195 return true 1196 } 1197 1198 // // Deprecated: IsIncomplete 1199 // // 1200 // // It indicates that the value cannot be fully evaluated due to 1201 // // insufficient information. 1202 // func (v Value) IsIncomplete() bool { 1203 // panic("deprecated") 1204 // } 1205 1206 // Exists reports whether this value existed in the configuration. 1207 func (v Value) Exists() bool { 1208 if v.v == nil { 1209 return false 1210 } 1211 if err := v.v.Bottom(); err != nil { 1212 return !err.NotExists 1213 } 1214 return true 1215 } 1216 1217 // isKind reports whether a value matches a particular kind. 1218 // It is like checkKind, except that it doesn't construct an error value. 1219 // Note that when v is bottom, the method always returns false. 1220 func (v Value) isKind(ctx *adt.OpContext, want adt.Kind) bool { 1221 if v.v == nil { 1222 return false 1223 } 1224 x := v.eval(ctx) 1225 if _, ok := x.(*adt.Bottom); ok { 1226 return false 1227 } 1228 k := x.Kind() 1229 if want != adt.BottomKind { 1230 if k&want == adt.BottomKind { 1231 return false 1232 } 1233 if !adt.IsConcrete(x) { 1234 return false 1235 } 1236 } 1237 return true 1238 } 1239 1240 // checkKind returns a bottom error if a value does not match a particular kind, 1241 // describing the reason why. Note that when v is bottom, it is always returned as-is. 1242 func (v Value) checkKind(ctx *adt.OpContext, want adt.Kind) *adt.Bottom { 1243 if v.v == nil { 1244 return errNotExists 1245 } 1246 // TODO: use checkKind 1247 x := v.eval(ctx) 1248 if b, ok := x.(*adt.Bottom); ok { 1249 return b 1250 } 1251 k := x.Kind() 1252 if want != adt.BottomKind { 1253 if k&want == adt.BottomKind { 1254 return mkErr(v.idx, x, "cannot use value %v (type %s) as %s", 1255 ctx.Str(x), k, want) 1256 } 1257 if !adt.IsConcrete(x) { 1258 return mkErr(v.idx, x, adt.IncompleteError, "non-concrete value %v", k) 1259 } 1260 } 1261 return nil 1262 } 1263 1264 func makeInt(v Value, x int64) Value { 1265 n := &adt.Num{K: adt.IntKind} 1266 n.X.SetInt64(int64(x)) 1267 return remakeFinal(v, nil, n) 1268 } 1269 1270 // Len returns the number of items of the underlying value. 1271 // For lists it reports the capacity of the list. For structs it indicates the 1272 // number of fields, for bytes the number of bytes. 1273 func (v Value) Len() Value { 1274 if v.v != nil { 1275 switch x := v.eval(v.ctx()).(type) { 1276 case *adt.Vertex: 1277 if x.IsList() { 1278 n := &adt.Num{K: adt.IntKind} 1279 n.X.SetInt64(int64(len(x.Elems()))) 1280 if x.IsClosedList() { 1281 return remakeFinal(v, nil, n) 1282 } 1283 // Note: this HAS to be a Conjunction value and cannot be 1284 // an adt.BinaryExpr, as the expressions would be considered 1285 // to be self-contained and unresolvable when evaluated 1286 // (can never become concrete). 1287 c := &adt.Conjunction{Values: []adt.Value{ 1288 &adt.BasicType{K: adt.IntKind}, 1289 &adt.BoundValue{Op: adt.GreaterEqualOp, Value: n}, 1290 }} 1291 return remakeFinal(v, nil, c) 1292 1293 } 1294 case *adt.Bytes: 1295 return makeInt(v, int64(len(x.B))) 1296 case *adt.String: 1297 return makeInt(v, int64(len([]rune(x.Str)))) 1298 } 1299 } 1300 const msg = "len not supported for type %v" 1301 return remakeValue(v, nil, mkErr(v.idx, v.v, msg, v.Kind())) 1302 1303 } 1304 1305 // Elem returns the value of undefined element types of lists and structs. 1306 // 1307 // Deprecated: use [Value.LookupPath] in combination with "AnyString" or "AnyIndex". 1308 func (v hiddenValue) Elem() (Value, bool) { 1309 sel := AnyString 1310 if v.v.IsList() { 1311 sel = AnyIndex 1312 } 1313 x := v.LookupPath(MakePath(sel)) 1314 return x, x.Exists() 1315 } 1316 1317 // List creates an iterator over the values of a list or reports an error if 1318 // v is not a list. 1319 func (v Value) List() (Iterator, error) { 1320 v, _ = v.Default() 1321 ctx := v.ctx() 1322 if err := v.checkKind(ctx, adt.ListKind); err != nil { 1323 return Iterator{idx: v.idx, ctx: ctx}, v.toErr(err) 1324 } 1325 arcs := []*adt.Vertex{} 1326 for _, a := range v.v.Elems() { 1327 if a.Label.IsInt() { 1328 arcs = append(arcs, a) 1329 } 1330 } 1331 return Iterator{idx: v.idx, ctx: ctx, val: v, arcs: arcs}, nil 1332 } 1333 1334 // Null reports an error if v is not null. 1335 func (v Value) Null() error { 1336 v, _ = v.Default() 1337 if err := v.checkKind(v.ctx(), adt.NullKind); err != nil { 1338 return v.toErr(err) 1339 } 1340 return nil 1341 } 1342 1343 // IsNull reports whether v is null. 1344 func (v Value) IsNull() bool { 1345 v, _ = v.Default() 1346 return v.isKind(v.ctx(), adt.NullKind) 1347 } 1348 1349 // Bool returns the bool value of v or false and an error if v is not a boolean. 1350 func (v Value) Bool() (bool, error) { 1351 v, _ = v.Default() 1352 ctx := v.ctx() 1353 if err := v.checkKind(ctx, adt.BoolKind); err != nil { 1354 return false, v.toErr(err) 1355 } 1356 return v.eval(ctx).(*adt.Bool).B, nil 1357 } 1358 1359 // String returns the string value if v is a string or an error otherwise. 1360 func (v Value) String() (string, error) { 1361 v, _ = v.Default() 1362 ctx := v.ctx() 1363 if err := v.checkKind(ctx, adt.StringKind); err != nil { 1364 return "", v.toErr(err) 1365 } 1366 return v.eval(ctx).(*adt.String).Str, nil 1367 } 1368 1369 // Bytes returns a byte slice if v represents a list of bytes or an error 1370 // otherwise. 1371 func (v Value) Bytes() ([]byte, error) { 1372 v, _ = v.Default() 1373 ctx := v.ctx() 1374 switch x := v.eval(ctx).(type) { 1375 case *adt.Bytes: 1376 return bytes.Clone(x.B), nil 1377 case *adt.String: 1378 return []byte(x.Str), nil 1379 } 1380 return nil, v.toErr(v.checkKind(ctx, adt.BytesKind|adt.StringKind)) 1381 } 1382 1383 // Reader returns a new Reader if v is a string or bytes type and an error 1384 // otherwise. 1385 func (v hiddenValue) Reader() (io.Reader, error) { 1386 v, _ = v.Default() 1387 ctx := v.ctx() 1388 switch x := v.eval(ctx).(type) { 1389 case *adt.Bytes: 1390 return bytes.NewReader(x.B), nil 1391 case *adt.String: 1392 return strings.NewReader(x.Str), nil 1393 } 1394 return nil, v.toErr(v.checkKind(ctx, adt.StringKind|adt.BytesKind)) 1395 } 1396 1397 // TODO: distinguish between optional, hidden, etc. Probably the best approach 1398 // is to mark options in context and have a single function for creating 1399 // a structVal. 1400 1401 // structVal returns an structVal or an error if v is not a struct. 1402 func (v Value) structValData(ctx *adt.OpContext) (structValue, *adt.Bottom) { 1403 return v.structValOpts(ctx, options{ 1404 omitHidden: true, 1405 omitDefinitions: true, 1406 omitOptional: true, 1407 }) 1408 } 1409 1410 // structVal returns an structVal or an error if v is not a struct. 1411 func (v Value) structValOpts(ctx *adt.OpContext, o options) (s structValue, err *adt.Bottom) { 1412 v, _ = v.Default() 1413 1414 obj := v.v 1415 1416 switch b := v.v.Bottom(); { 1417 case b != nil && b.IsIncomplete() && !o.concrete && !o.final: 1418 1419 // Allow scalar values if hidden or definition fields are requested. 1420 case !o.omitHidden, !o.omitDefinitions: 1421 default: 1422 obj, err = v.getStruct() 1423 if err != nil { 1424 return structValue{}, err 1425 } 1426 } 1427 1428 // features are topologically sorted. 1429 // TODO(sort): make sort order part of the evaluator and eliminate this. 1430 features := export.VertexFeatures(ctx, obj) 1431 1432 arcs := make([]*adt.Vertex, 0, len(obj.Arcs)) 1433 1434 for _, f := range features { 1435 if f.IsLet() { 1436 continue 1437 } 1438 if f.IsDef() && (o.omitDefinitions || o.concrete) { 1439 continue 1440 } 1441 if f.IsHidden() && o.omitHidden { 1442 continue 1443 } 1444 arc := obj.LookupRaw(f) 1445 if arc == nil { 1446 continue 1447 } 1448 switch arc.ArcType { 1449 case adt.ArcOptional: 1450 if o.omitOptional { 1451 continue 1452 } 1453 case adt.ArcRequired: 1454 // We report an error for required fields if the configuration is 1455 // final or concrete. We also do so if omitOptional is true, as 1456 // it avoids hiding errors in required fields. 1457 if o.omitOptional || o.concrete || o.final { 1458 arc = &adt.Vertex{ 1459 Label: f, 1460 Parent: arc.Parent, 1461 Conjuncts: arc.Conjuncts, 1462 BaseValue: adt.NewRequiredNotPresentError(ctx, arc), 1463 } 1464 arc.ForceDone() 1465 } 1466 } 1467 arcs = append(arcs, arc) 1468 } 1469 return structValue{ctx, v, obj, arcs}, nil 1470 } 1471 1472 // Struct returns the underlying struct of a value or an error if the value 1473 // is not a struct. 1474 // 1475 // Deprecated: use [Value.Fields]. 1476 func (v hiddenValue) Struct() (*Struct, error) { 1477 ctx := v.ctx() 1478 obj, err := v.structValOpts(ctx, options{}) 1479 if err != nil { 1480 return nil, v.toErr(err) 1481 } 1482 return &Struct{obj}, nil 1483 } 1484 1485 func (v Value) getStruct() (*adt.Vertex, *adt.Bottom) { 1486 ctx := v.ctx() 1487 if err := v.checkKind(ctx, adt.StructKind); err != nil { 1488 if !err.ChildError { 1489 return nil, err 1490 } 1491 } 1492 return v.v, nil 1493 } 1494 1495 // Struct represents a CUE struct value. 1496 type Struct struct { 1497 structValue 1498 } 1499 1500 type hiddenStruct = Struct 1501 1502 // FieldInfo contains information about a struct field. 1503 // 1504 // Deprecated: only used by deprecated functions. 1505 type FieldInfo struct { 1506 Selector string 1507 Name string // Deprecated: use Selector 1508 Pos int 1509 Value Value 1510 1511 SelectorType SelectorType 1512 1513 IsDefinition bool 1514 IsOptional bool 1515 IsHidden bool 1516 } 1517 1518 func (s *hiddenStruct) Len() int { 1519 return s.structValue.Len() 1520 } 1521 1522 // field reports information about the ith field, i < o.Len(). 1523 func (s *hiddenStruct) Field(i int) FieldInfo { 1524 a := s.at(i) 1525 opt := a.ArcType == adt.ArcOptional 1526 selType := featureToSelType(a.Label, a.ArcType) 1527 ctx := s.v.ctx() 1528 1529 v := makeChildValue(s.v, a) 1530 name := s.v.idx.LabelStr(a.Label) 1531 str := a.Label.SelectorString(ctx) 1532 return FieldInfo{str, name, i, v, selType, a.Label.IsDef(), opt, a.Label.IsHidden()} 1533 } 1534 1535 // FieldByName looks up a field for the given name. If isIdent is true, it will 1536 // look up a definition or hidden field (starting with `_` or `_#`). Otherwise 1537 // it interprets name as an arbitrary string for a regular field. 1538 func (s *hiddenStruct) FieldByName(name string, isIdent bool) (FieldInfo, error) { 1539 f := s.v.idx.Label(name, isIdent) 1540 for i, a := range s.arcs { 1541 if a.Label == f { 1542 return s.Field(i), nil 1543 } 1544 } 1545 return FieldInfo{}, errNotFound 1546 } 1547 1548 // Fields creates an iterator over the Struct's fields. 1549 func (s *hiddenStruct) Fields(opts ...Option) *Iterator { 1550 iter, _ := s.v.Fields(opts...) 1551 return iter 1552 } 1553 1554 // Fields creates an iterator over v's fields if v is a struct or an error 1555 // otherwise. 1556 func (v Value) Fields(opts ...Option) (*Iterator, error) { 1557 o := options{omitDefinitions: true, omitHidden: true, omitOptional: true} 1558 o.updateOptions(opts) 1559 ctx := v.ctx() 1560 obj, err := v.structValOpts(ctx, o) 1561 if err != nil { 1562 return &Iterator{idx: v.idx, ctx: ctx}, v.toErr(err) 1563 } 1564 1565 return &Iterator{idx: v.idx, ctx: ctx, val: v, arcs: obj.arcs}, nil 1566 } 1567 1568 // Lookup reports the value at a path starting from v. The empty path returns v 1569 // itself. 1570 // 1571 // [Value.Exists] can be used to verify if the returned value existed. 1572 // Lookup cannot be used to look up hidden or optional fields or definitions. 1573 // 1574 // Deprecated: use [Value.LookupPath]. At some point before v1.0.0, this method will 1575 // be removed to be reused eventually for looking up a selector. 1576 func (v hiddenValue) Lookup(path ...string) Value { 1577 ctx := v.ctx() 1578 for _, k := range path { 1579 // TODO(eval) TODO(error): always search in full data and change error 1580 // message if a field is found but is of the incorrect type. 1581 obj, err := v.structValData(ctx) 1582 if err != nil { 1583 // TODO: return a Value at the same location and a new error? 1584 return newErrValue(v, err) 1585 } 1586 v = obj.Lookup(k) 1587 } 1588 return v 1589 } 1590 1591 // Path returns the path to this value from the root of an Instance. 1592 // 1593 // This is currently only defined for values that have a fixed path within 1594 // a configuration, and thus not those that are derived from Elem, Template, 1595 // or programmatically generated values such as those returned by Unify. 1596 func (v Value) Path() Path { 1597 if v.v == nil { 1598 return Path{} 1599 } 1600 return Path{path: appendPath(nil, v)} 1601 } 1602 1603 // Path computes the sequence of Features leading from the root to of the 1604 // instance to this Vertex. 1605 func appendPath(a []Selector, v Value) []Selector { 1606 if p := v.parent(); p.v != nil { 1607 a = appendPath(a, p) 1608 } 1609 1610 if v.v.Label == 0 { 1611 // A Label may be 0 for programmatically inserted nodes. 1612 return a 1613 } 1614 1615 f := v.v.Label 1616 if index := f.Index(); index == adt.MaxIndex { 1617 return append(a, Selector{anySelector(f)}) 1618 } 1619 1620 var sel selector 1621 switch t := f.Typ(); t { 1622 case adt.IntLabel: 1623 sel = indexSelector(f) 1624 case adt.DefinitionLabel: 1625 sel = definitionSelector(f.SelectorString(v.idx)) 1626 1627 case adt.HiddenDefinitionLabel, adt.HiddenLabel: 1628 sel = scopedSelector{ 1629 name: f.IdentString(v.idx), 1630 pkg: f.PkgID(v.idx), 1631 } 1632 1633 case adt.StringLabel: 1634 sel = stringSelector(f.StringValue(v.idx)) 1635 1636 default: 1637 panic(fmt.Sprintf("unsupported label type %v", t)) 1638 } 1639 return append(a, Selector{sel}) 1640 } 1641 1642 // LookupDef is equal to LookupPath(MakePath(Def(name))). 1643 // 1644 // Deprecated: use [Value.LookupPath]. 1645 func (v hiddenValue) LookupDef(name string) Value { 1646 return v.LookupPath(MakePath(Def(name))) 1647 } 1648 1649 var errNotFound = errors.Newf(token.NoPos, "field not found") 1650 1651 // FieldByName looks up a field for the given name. If isIdent is true, it will 1652 // look up a definition or hidden field (starting with `_` or `_#`). Otherwise 1653 // it interprets name as an arbitrary string for a regular field. 1654 // 1655 // Deprecated: use [Value.LookupPath]. 1656 func (v hiddenValue) FieldByName(name string, isIdent bool) (f FieldInfo, err error) { 1657 s, err := v.Struct() 1658 if err != nil { 1659 return f, err 1660 } 1661 return s.FieldByName(name, isIdent) 1662 } 1663 1664 // LookupField reports information about a field of v. 1665 // 1666 // Deprecated: use [Value.LookupPath]. 1667 func (v hiddenValue) LookupField(name string) (FieldInfo, error) { 1668 s, err := v.Struct() 1669 if err != nil { 1670 // TODO: return a Value at the same location and a new error? 1671 return FieldInfo{}, err 1672 } 1673 f, err := s.FieldByName(name, true) 1674 if err != nil { 1675 return f, err 1676 } 1677 if f.IsHidden { 1678 return f, errNotFound 1679 } 1680 return f, err 1681 } 1682 1683 // TODO: expose this API? 1684 // 1685 // // EvalExpr evaluates an expression within the scope of v, which must be 1686 // // a struct. 1687 // // 1688 // // Expressions may refer to builtin packages if they can be uniquely identified. 1689 // func (v Value) EvalExpr(expr ast.Expr) Value { 1690 // ctx := v.ctx() 1691 // result := evalExpr(ctx, v.eval(ctx), expr) 1692 // return newValueRoot(ctx, result) 1693 // } 1694 1695 // Fill creates a new value by unifying v with the value of x at the given path. 1696 // 1697 // Values may be any Go value that can be converted to CUE, an ast.Expr or 1698 // a Value. In the latter case, it will panic if the Value is not from the same 1699 // Runtime. 1700 // 1701 // Any reference in v referring to the value at the given path will resolve 1702 // to x in the newly created value. The resulting value is not validated. 1703 // 1704 // Deprecated: use [Value.FillPath]. 1705 func (v hiddenValue) Fill(x interface{}, path ...string) Value { 1706 if v.v == nil { 1707 return v 1708 } 1709 selectors := make([]Selector, len(path)) 1710 for i, p := range path { 1711 selectors[i] = Str(p) 1712 } 1713 return v.FillPath(MakePath(selectors...), x) 1714 } 1715 1716 // FillPath creates a new value by unifying v with the value of x at the given 1717 // path. 1718 // 1719 // If x is an cue/ast.Expr, it will be evaluated within the context of the 1720 // given path: identifiers that are not resolved within the expression are 1721 // resolved as if they were defined at the path position. 1722 // 1723 // If x is a Value, it will be used as is. It panics if x is not created 1724 // from the same Runtime as v. 1725 // 1726 // Otherwise, the given Go value will be converted to CUE using the same rules 1727 // as Context.Encode. 1728 // 1729 // Any reference in v referring to the value at the given path will resolve to x 1730 // in the newly created value. The resulting value is not validated. 1731 func (v Value) FillPath(p Path, x interface{}) Value { 1732 if v.v == nil { 1733 // TODO: panic here? 1734 return v 1735 } 1736 ctx := v.ctx() 1737 if err := p.Err(); err != nil { 1738 return newErrValue(v, mkErr(v.idx, nil, 0, "invalid path: %v", err)) 1739 } 1740 var expr adt.Expr 1741 switch x := x.(type) { 1742 case Value: 1743 if v.idx != x.idx { 1744 panic("values are not from the same runtime") 1745 } 1746 expr = x.v 1747 case ast.Expr: 1748 n := getScopePrefix(v, p) 1749 // TODO: inject import path of current package? 1750 expr = resolveExpr(ctx, n, x) 1751 default: 1752 expr = convert.GoValueToValue(ctx, x, true) 1753 } 1754 for i := len(p.path) - 1; i >= 0; i-- { 1755 switch sel := p.path[i]; sel.Type() { 1756 case StringLabel | PatternConstraint: 1757 expr = &adt.StructLit{Decls: []adt.Decl{ 1758 &adt.BulkOptionalField{ 1759 Filter: &adt.BasicType{K: adt.StringKind}, 1760 Value: expr, 1761 }, 1762 }} 1763 1764 case IndexLabel | PatternConstraint: 1765 expr = &adt.ListLit{Elems: []adt.Elem{ 1766 &adt.Ellipsis{Value: expr}, 1767 }} 1768 1769 case IndexLabel: 1770 i := sel.Index() 1771 list := &adt.ListLit{} 1772 any := &adt.Top{} 1773 // TODO(perf): make this a constant thing. This will be possible with the query extension. 1774 for range i { 1775 list.Elems = append(list.Elems, any) 1776 } 1777 list.Elems = append(list.Elems, expr, &adt.Ellipsis{}) 1778 expr = list 1779 1780 default: 1781 f := &adt.Field{ 1782 Label: sel.sel.feature(v.idx), 1783 Value: expr, 1784 ArcType: adt.ArcMember, 1785 } 1786 switch sel.ConstraintType() { 1787 case OptionalConstraint: 1788 f.ArcType = adt.ArcOptional 1789 case RequiredConstraint: 1790 f.ArcType = adt.ArcRequired 1791 } 1792 1793 expr = &adt.StructLit{Decls: []adt.Decl{f}} 1794 } 1795 } 1796 n := &adt.Vertex{} 1797 n.AddConjunct(adt.MakeRootConjunct(nil, expr)) 1798 n.Finalize(ctx) 1799 w := makeValue(v.idx, n, v.parent_) 1800 return v.Unify(w) 1801 } 1802 1803 // Template returns a function that represents the template definition for a 1804 // struct in a configuration file. It returns nil if v is not a struct kind or 1805 // if there is no template associated with the struct. 1806 // 1807 // The returned function returns the value that would be unified with field 1808 // given its name. 1809 // 1810 // Deprecated: use [Value.LookupPath] in combination with using optional selectors. 1811 func (v hiddenValue) Template() func(label string) Value { 1812 if v.v == nil { 1813 return nil 1814 } 1815 1816 // Implementation for the old evaluator. 1817 types := v.v.OptionalTypes() 1818 switch { 1819 case types&(adt.HasAdditional|adt.HasPattern) != 0: 1820 case v.v.PatternConstraints != nil: 1821 default: 1822 return nil 1823 } 1824 1825 return func(label string) Value { 1826 return v.LookupPath(MakePath(Str(label).Optional())) 1827 } 1828 } 1829 1830 // Subsume reports nil when w is an instance of v or an error otherwise. 1831 // 1832 // Without options, the entire value is considered for assumption, which means 1833 // Subsume tests whether v is a backwards compatible (newer) API version of w. 1834 // 1835 // Use the [Final] option to check subsumption if a w is known to be final, and 1836 // should assumed to be closed. 1837 // 1838 // Use the [Raw] option to do a low-level subsumption, taking defaults into 1839 // account. 1840 // 1841 // Value v and w must be obtained from the same build. TODO: remove this 1842 // requirement. 1843 func (v Value) Subsume(w Value, opts ...Option) error { 1844 o := getOptions(opts) 1845 p := subsume.CUE 1846 switch { 1847 case o.final && o.ignoreClosedness: 1848 p = subsume.FinalOpen 1849 case o.final: 1850 p = subsume.Final 1851 case o.ignoreClosedness: 1852 p = subsume.API 1853 } 1854 if !o.raw { 1855 p.Defaults = true 1856 } 1857 ctx := v.ctx() 1858 return p.Value(ctx, v.v, w.v) 1859 } 1860 1861 // Deprecated: use [Value.Subsume]. 1862 // 1863 // Subsumes reports whether w is an instance of v. 1864 // 1865 // Without options, Subsumes checks whether v is a backwards compatible schema 1866 // of w. 1867 // 1868 // By default, Subsumes tests whether two values are compatible 1869 // Value v and w must be obtained from the same build. 1870 // TODO: remove this requirement. 1871 func (v hiddenValue) Subsumes(w Value) bool { 1872 ctx := v.ctx() 1873 p := subsume.Profile{Defaults: true} 1874 return p.Check(ctx, v.v, w.v) 1875 } 1876 1877 func allowed(ctx *adt.OpContext, parent, n *adt.Vertex) *adt.Bottom { 1878 if !parent.IsClosedList() && !parent.IsClosedStruct() { 1879 return nil 1880 } 1881 1882 for _, a := range n.Arcs { 1883 if !parent.Accept(ctx, a.Label) { 1884 defer ctx.PopArc(ctx.PushArc(parent)) 1885 label := a.Label.SelectorString(ctx) 1886 parent.Accept(ctx, a.Label) 1887 return ctx.NewErrf("field not allowed: %s", label) 1888 } 1889 } 1890 return nil 1891 } 1892 1893 func addConjuncts(dst, src *adt.Vertex) { 1894 c := adt.MakeRootConjunct(nil, src) 1895 if src.Closed { 1896 var root adt.CloseInfo 1897 c.CloseInfo = root.SpawnRef(src, src.Closed, nil) 1898 } 1899 dst.AddConjunct(c) 1900 } 1901 1902 // Unify reports the greatest lower bound of v and w. 1903 // 1904 // Value v and w must be obtained from the same build. 1905 // TODO: remove this requirement. 1906 func (v Value) Unify(w Value) Value { 1907 if v.v == nil { 1908 return w 1909 } 1910 if w.v == nil || w.v == v.v { 1911 return v 1912 } 1913 1914 n := &adt.Vertex{} 1915 addConjuncts(n, v.v) 1916 addConjuncts(n, w.v) 1917 1918 ctx := newContext(v.idx) 1919 n.Finalize(ctx) 1920 1921 n.Parent = v.v.Parent 1922 n.Label = v.v.Label 1923 n.Closed = v.v.Closed || w.v.Closed 1924 1925 if err := n.Err(ctx); err != nil { 1926 return makeValue(v.idx, n, v.parent_) 1927 } 1928 if err := allowed(ctx, v.v, n); err != nil { 1929 return newErrValue(w, err) 1930 } 1931 if err := allowed(ctx, w.v, n); err != nil { 1932 return newErrValue(v, err) 1933 } 1934 1935 return makeValue(v.idx, n, v.parent_) 1936 } 1937 1938 // UnifyAccept is as v.Unify(w), but will disregard the closedness rules for 1939 // v and w, and will, instead, only allow fields that are present in accept. 1940 // 1941 // UnifyAccept is used to piecemeal unify individual conjuncts obtained from 1942 // accept without violating closedness rules. 1943 func (v Value) UnifyAccept(w Value, accept Value) Value { 1944 if v.v == nil { 1945 return w 1946 } 1947 if w.v == nil { 1948 return v 1949 } 1950 if accept.v == nil { 1951 panic("accept must exist") 1952 } 1953 1954 n := &adt.Vertex{} 1955 n.AddConjunct(adt.MakeRootConjunct(nil, v.v)) 1956 n.AddConjunct(adt.MakeRootConjunct(nil, w.v)) 1957 1958 ctx := newContext(v.idx) 1959 n.Finalize(ctx) 1960 1961 n.Parent = v.v.Parent 1962 n.Label = v.v.Label 1963 1964 if err := n.Err(ctx); err != nil { 1965 return makeValue(v.idx, n, v.parent_) 1966 } 1967 if err := allowed(ctx, accept.v, n); err != nil { 1968 return newErrValue(accept, err) 1969 } 1970 1971 return makeValue(v.idx, n, v.parent_) 1972 } 1973 1974 // Equals reports whether two values are equal, ignoring optional fields. 1975 // The result is undefined for incomplete values. 1976 func (v Value) Equals(other Value) bool { 1977 if v.v == nil || other.v == nil { 1978 return false 1979 } 1980 return adt.Equal(v.ctx(), v.v, other.v, 0) 1981 } 1982 1983 func (v Value) instance() *Instance { 1984 if v.v == nil { 1985 return nil 1986 } 1987 return getImportFromNode(v.idx, v.v) 1988 } 1989 1990 // Reference returns the instance and path referred to by this value such that 1991 // inst.Lookup(path) resolves to the same value, or no path if this value is not 1992 // a reference. If a reference contains index selection (foo[bar]), it will 1993 // only return a reference if the index resolves to a concrete value. 1994 // 1995 // Deprecated: use [Value.ReferencePath] 1996 func (v hiddenValue) Reference() (inst *Instance, path []string) { 1997 root, p := v.ReferencePath() 1998 if !root.Exists() { 1999 return nil, nil 2000 } 2001 2002 inst = getImportFromNode(v.idx, root.v) 2003 for _, sel := range p.Selectors() { 2004 switch x := sel.sel.(type) { 2005 case stringSelector: 2006 path = append(path, string(x)) 2007 default: 2008 path = append(path, sel.String()) 2009 } 2010 } 2011 2012 return inst, path 2013 } 2014 2015 // ReferencePath returns the value and path referred to by this value such that 2016 // value.LookupPath(path) resolves to the same value, or no path if this value 2017 // is not a reference. 2018 func (v Value) ReferencePath() (root Value, p Path) { 2019 // TODO: don't include references to hidden fields. 2020 c, count := v.v.SingleConjunct() 2021 if count != 1 { 2022 return Value{}, Path{} 2023 } 2024 ctx := v.ctx() 2025 2026 env, expr := c.EnvExpr() 2027 2028 x, path := reference(v.idx, ctx, env, expr) 2029 if x == nil { 2030 return Value{}, Path{} 2031 } 2032 // NOTE: due to structure sharing, the path of the referred node may end 2033 // up different from the one explicitly pointed to. The value will be the 2034 // same, but the scope may differ. 2035 // TODO(structureshare): see if we can construct the original path. This 2036 // only has to be done if structures are being shared. 2037 return makeValue(v.idx, x, nil), Path{path: path} 2038 } 2039 2040 func reference(rt *runtime.Runtime, c *adt.OpContext, env *adt.Environment, r adt.Expr) (inst *adt.Vertex, path []Selector) { 2041 ctx := c 2042 defer ctx.PopState(ctx.PushState(env, r.Source())) 2043 2044 switch x := r.(type) { 2045 // TODO: do we need to handle Vertex as well, in case this is hard-wired? 2046 // Probably not, as this results from dynamic content. 2047 2048 case *adt.NodeLink: 2049 // TODO: consider getting rid of NodeLink. 2050 inst, path = mkPath(rt, nil, x.Node) 2051 2052 case *adt.FieldReference: 2053 env := ctx.Env(x.UpCount) 2054 inst, path = mkPath(rt, nil, env.Vertex) 2055 path = appendSelector(path, featureToSel(x.Label, rt)) 2056 2057 case *adt.LabelReference: 2058 env := ctx.Env(x.UpCount) 2059 return mkPath(rt, nil, env.Vertex) 2060 2061 case *adt.DynamicReference: 2062 env := ctx.Env(x.UpCount) 2063 inst, path = mkPath(rt, nil, env.Vertex) 2064 v, _ := ctx.Evaluate(env, x.Label) 2065 path = appendSelector(path, valueToSel(v)) 2066 2067 case *adt.ImportReference: 2068 inst = rt.LoadImport(rt.LabelStr(x.ImportPath)) 2069 2070 case *adt.SelectorExpr: 2071 inst, path = reference(rt, c, env, x.X) 2072 path = appendSelector(path, featureToSel(x.Sel, rt)) 2073 2074 case *adt.IndexExpr: 2075 inst, path = reference(rt, c, env, x.X) 2076 v, _ := ctx.Evaluate(env, x.Index) 2077 path = appendSelector(path, valueToSel(v)) 2078 } 2079 if inst == nil { 2080 return nil, nil 2081 } 2082 return inst, path 2083 } 2084 2085 func mkPath(r *runtime.Runtime, a []Selector, v *adt.Vertex) (root *adt.Vertex, path []Selector) { 2086 if v.Parent == nil { 2087 return v, a 2088 } 2089 root, path = mkPath(r, a, v.Parent) 2090 path = appendSelector(path, featureToSel(v.Label, r)) 2091 return root, path 2092 } 2093 2094 type options struct { 2095 concrete bool // enforce that values are concrete 2096 raw bool // show original values 2097 hasHidden bool 2098 omitHidden bool 2099 omitDefinitions bool 2100 omitOptional bool 2101 omitAttrs bool 2102 inlineImports bool 2103 resolveReferences bool 2104 showErrors bool 2105 final bool 2106 ignoreClosedness bool // used for comparing APIs 2107 docs bool 2108 disallowCycles bool // implied by concrete 2109 } 2110 2111 // An Option defines modes of evaluation. 2112 type Option option 2113 2114 type option func(p *options) 2115 2116 // Final indicates a value is final. It implicitly closes all structs and lists 2117 // in a value and selects defaults. 2118 func Final() Option { 2119 return func(o *options) { 2120 o.final = true 2121 o.omitDefinitions = true 2122 o.omitOptional = true 2123 o.omitHidden = true 2124 } 2125 } 2126 2127 // Schema specifies the input is a Schema. Used by Subsume. 2128 func Schema() Option { 2129 return func(o *options) { 2130 o.ignoreClosedness = true 2131 } 2132 } 2133 2134 // Concrete ensures that all values are concrete. 2135 // 2136 // For Validate this means it returns an error if this is not the case. 2137 // In other cases a non-concrete value will be replaced with an error. 2138 func Concrete(concrete bool) Option { 2139 return func(p *options) { 2140 if concrete { 2141 p.concrete = true 2142 p.final = true 2143 if !p.hasHidden { 2144 p.omitHidden = true 2145 p.omitDefinitions = true 2146 } 2147 } 2148 } 2149 } 2150 2151 // InlineImports causes references to values within imported packages to be 2152 // inlined. References to builtin packages are not inlined. 2153 func InlineImports(expand bool) Option { 2154 return func(p *options) { p.inlineImports = expand } 2155 } 2156 2157 // DisallowCycles forces validation in the presence of cycles, even if 2158 // non-concrete values are allowed. This is implied by [Concrete]. 2159 func DisallowCycles(disallow bool) Option { 2160 return func(p *options) { p.disallowCycles = disallow } 2161 } 2162 2163 // ResolveReferences forces the evaluation of references when outputting. 2164 // 2165 // Deprecated: [Value.Syntax] will now always attempt to resolve dangling references and 2166 // make the output self-contained. When [Final] or [Concrete] are used, 2167 // it will already attempt to resolve all references. 2168 // See also [InlineImports]. 2169 func ResolveReferences(resolve bool) Option { 2170 return func(p *options) { 2171 p.resolveReferences = resolve 2172 2173 // ResolveReferences is implemented as a Value printer, rather than 2174 // a definition printer, even though it should be more like the latter. 2175 // To reflect this we convert incomplete errors to their original 2176 // expression. 2177 // 2178 // TODO: ShowErrors mostly shows incomplete errors, even though this is 2179 // just an approximation. There seems to be some inconsistencies as to 2180 // when child errors are marked as such, making the conversion somewhat 2181 // inconsistent. This option is conservative, though. 2182 p.showErrors = true 2183 } 2184 } 2185 2186 // ErrorsAsValues treats errors as a regular value, including them at the 2187 // location in the tree where they occur, instead of interpreting them as a 2188 // configuration-wide failure that is returned instead of root value. 2189 // Used by Syntax. 2190 func ErrorsAsValues(show bool) Option { 2191 return func(p *options) { p.showErrors = show } 2192 } 2193 2194 // Raw tells Syntax to generate the value as is without any simplifications and 2195 // without ensuring a value is self contained. Any references are left dangling. 2196 // The generated syntax tree can be compiled by passing the Value from which it 2197 // was generated to scope. 2198 // 2199 // The option InlineImports overrides this option with respect to ensuring the 2200 // output is self contained. 2201 func Raw() Option { 2202 return func(p *options) { p.raw = true } 2203 } 2204 2205 // All indicates that all fields and values should be included in processing 2206 // even if they can be elided or omitted. 2207 func All() Option { 2208 return func(p *options) { 2209 p.omitAttrs = false 2210 p.omitHidden = false 2211 p.omitDefinitions = false 2212 p.omitOptional = false 2213 } 2214 } 2215 2216 // Docs indicates whether docs should be included. 2217 func Docs(include bool) Option { 2218 return func(p *options) { p.docs = true } 2219 } 2220 2221 // Definitions indicates whether definitions should be included. 2222 // 2223 // Definitions may still be included for certain functions if they are referred 2224 // to by other values. 2225 func Definitions(include bool) Option { 2226 return func(p *options) { 2227 p.hasHidden = true 2228 p.omitDefinitions = !include 2229 } 2230 } 2231 2232 // Hidden indicates that definitions and hidden fields should be included. 2233 func Hidden(include bool) Option { 2234 return func(p *options) { 2235 p.hasHidden = true 2236 p.omitHidden = !include 2237 p.omitDefinitions = !include 2238 } 2239 } 2240 2241 // Optional indicates that optional fields should be included. 2242 func Optional(include bool) Option { 2243 return func(p *options) { p.omitOptional = !include } 2244 } 2245 2246 // Attributes indicates that attributes should be included. 2247 func Attributes(include bool) Option { 2248 return func(p *options) { p.omitAttrs = !include } 2249 } 2250 2251 func getOptions(opts []Option) (o options) { 2252 o.updateOptions(opts) 2253 return 2254 } 2255 2256 func (o *options) updateOptions(opts []Option) { 2257 for _, fn := range opts { 2258 fn(o) 2259 } 2260 } 2261 2262 // Validate reports any errors, recursively. The returned error may represent 2263 // more than one error, retrievable with errors.Errors, if more than one 2264 // exists. 2265 // 2266 // Note that by default not all errors are reported, unless options like 2267 // [Concrete] are used. The [Final] option can be used to check for missing 2268 // required fields. 2269 func (v Value) Validate(opts ...Option) error { 2270 o := options{} 2271 o.updateOptions(opts) 2272 2273 cfg := &validate.Config{ 2274 Concrete: o.concrete, 2275 Final: o.final, 2276 DisallowCycles: o.disallowCycles, 2277 AllErrors: true, 2278 } 2279 2280 b := validate.Validate(v.ctx(), v.v, cfg) 2281 if b != nil { 2282 return v.toErr(b) 2283 } 2284 return nil 2285 } 2286 2287 // Walk descends into all values of v, calling f. If f returns false, Walk 2288 // will not descent further. It only visits values that are part of the data 2289 // model, so this excludes definitions and optional, required, and hidden 2290 // fields. 2291 func (v Value) Walk(before func(Value) bool, after func(Value)) { 2292 ctx := v.ctx() 2293 switch v.Kind() { 2294 case StructKind: 2295 if before != nil && !before(v) { 2296 return 2297 } 2298 obj, _ := v.structValOpts(ctx, options{ 2299 omitHidden: true, 2300 omitDefinitions: true, 2301 }) 2302 for i := range obj.Len() { 2303 _, v := obj.At(i) 2304 // TODO: should we error on required fields, or visit them anyway? 2305 // Walk is not designed to error at this moment, though. 2306 if v.v.ArcType != adt.ArcMember { 2307 continue 2308 } 2309 v.Walk(before, after) 2310 } 2311 case ListKind: 2312 if before != nil && !before(v) { 2313 return 2314 } 2315 list, _ := v.List() 2316 for list.Next() { 2317 list.Value().Walk(before, after) 2318 } 2319 default: 2320 if before != nil { 2321 before(v) 2322 } 2323 } 2324 if after != nil { 2325 after(v) 2326 } 2327 } 2328 2329 // Expr reports the operation of the underlying expression and the values it 2330 // operates on. 2331 // 2332 // For unary expressions, it returns the single value of the expression. 2333 // 2334 // For binary expressions it returns first the left and right value, in that 2335 // order. For associative operations however, (for instance '&' and '|'), it may 2336 // return more than two values, where the operation is to be applied in 2337 // sequence. 2338 // 2339 // For selector and index expressions it returns the subject and then the index. 2340 // For selectors, the index is the string value of the identifier. 2341 // 2342 // For interpolations it returns a sequence of values to be concatenated, some 2343 // of which will be literal strings and some unevaluated expressions. 2344 // 2345 // A builtin call expression returns the value of the builtin followed by the 2346 // args of the call. 2347 func (v Value) Expr() (Op, []Value) { 2348 // TODO: return v if this is complete? Yes for now 2349 if v.v == nil { 2350 return NoOp, nil 2351 } 2352 2353 var expr adt.Expr 2354 var env *adt.Environment 2355 2356 if v.v.IsData() { 2357 expr = v.v.Value() 2358 goto process 2359 2360 } 2361 2362 switch c, count := v.v.SingleConjunct(); count { 2363 case 0: 2364 if v.v.BaseValue == nil { 2365 return NoOp, []Value{makeValue(v.idx, v.v, v.parent_)} // TODO: v? 2366 } 2367 expr = v.v.Value() 2368 2369 case 1: 2370 // the default case, processed below. 2371 env, expr = c.EnvExpr() 2372 if w, ok := expr.(*adt.Vertex); ok { 2373 return Value{v.idx, w, v.parent_}.Expr() 2374 } 2375 2376 default: 2377 a := []Value{} 2378 ctx := v.ctx() 2379 v.v.VisitLeafConjuncts(func(c adt.Conjunct) bool { 2380 // Keep parent here. TODO: do we need remove the requirement 2381 // from other conjuncts? 2382 n := &adt.Vertex{ 2383 Parent: v.v.Parent, 2384 Label: v.v.Label, 2385 } 2386 n.AddConjunct(c) 2387 n.Finalize(ctx) 2388 a = append(a, makeValue(v.idx, n, v.parent_)) 2389 return true 2390 }) 2391 2392 return adt.AndOp, a 2393 } 2394 2395 process: 2396 2397 // TODO: replace appends with []Value{}. For not leave. 2398 a := []Value{} 2399 op := NoOp 2400 switch x := expr.(type) { 2401 case *adt.BinaryExpr: 2402 a = append(a, remakeValue(v, env, x.X)) 2403 a = append(a, remakeValue(v, env, x.Y)) 2404 op = x.Op 2405 case *adt.UnaryExpr: 2406 a = append(a, remakeValue(v, env, x.X)) 2407 op = x.Op 2408 case *adt.BoundExpr: 2409 a = append(a, remakeValue(v, env, x.Expr)) 2410 op = x.Op 2411 case *adt.BoundValue: 2412 a = append(a, remakeValue(v, env, x.Value)) 2413 op = x.Op 2414 case *adt.Conjunction: 2415 // pre-expanded unification 2416 for _, conjunct := range x.Values { 2417 a = append(a, remakeValue(v, env, conjunct)) 2418 } 2419 op = AndOp 2420 case *adt.Disjunction: 2421 count := 0 2422 outer: 2423 for i, disjunct := range x.Values { 2424 if i < x.NumDefaults { 2425 for _, n := range x.Values[x.NumDefaults:] { 2426 if subsume.Simplify.Value(v.ctx(), n, disjunct) == nil { 2427 continue outer 2428 } 2429 } 2430 } 2431 count++ 2432 a = append(a, remakeValue(v, env, disjunct)) 2433 } 2434 if count > 1 { 2435 op = OrOp 2436 } 2437 2438 case *adt.DisjunctionExpr: 2439 // Filter defaults that are subsumed by another value. 2440 count := 0 2441 outerExpr: 2442 for _, disjunct := range x.Values { 2443 if disjunct.Default { 2444 for _, n := range x.Values { 2445 a := adt.Vertex{ 2446 Label: v.v.Label, 2447 } 2448 b := a 2449 a.AddConjunct(adt.MakeRootConjunct(env, n.Val)) 2450 b.AddConjunct(adt.MakeRootConjunct(env, disjunct.Val)) 2451 2452 ctx := eval.NewContext(v.idx, nil) 2453 a.Finalize(ctx) 2454 b.Finalize(ctx) 2455 if allowed(ctx, v.v, &b) != nil { 2456 // Everything subsumed bottom 2457 continue outerExpr 2458 } 2459 if allowed(ctx, v.v, &a) != nil { 2460 // An error doesn't subsume anything except another error. 2461 continue 2462 } 2463 a.Parent = v.v.Parent 2464 if !n.Default && subsume.Simplify.Value(ctx, &a, &b) == nil { 2465 continue outerExpr 2466 } 2467 } 2468 } 2469 count++ 2470 a = append(a, remakeValue(v, env, disjunct.Val)) 2471 } 2472 if count > 1 { 2473 op = adt.OrOp 2474 } 2475 2476 case *adt.Interpolation: 2477 for _, p := range x.Parts { 2478 a = append(a, remakeValue(v, env, p)) 2479 } 2480 op = InterpolationOp 2481 2482 case *adt.FieldReference: 2483 // TODO: allow hard link 2484 ctx := v.ctx() 2485 f := ctx.PushState(env, x.Src) 2486 env := ctx.Env(x.UpCount) 2487 a = append(a, remakeValue(v, nil, &adt.NodeLink{Node: env.Vertex})) 2488 a = append(a, remakeValue(v, nil, ctx.NewString(x.Label.SelectorString(ctx)))) 2489 _ = ctx.PopState(f) 2490 op = SelectorOp 2491 2492 case *adt.SelectorExpr: 2493 a = append(a, remakeValue(v, env, x.X)) 2494 // A string selector is quoted. 2495 a = append(a, remakeValue(v, env, &adt.String{ 2496 Str: x.Sel.SelectorString(v.idx), 2497 })) 2498 op = SelectorOp 2499 2500 case *adt.IndexExpr: 2501 a = append(a, remakeValue(v, env, x.X)) 2502 a = append(a, remakeValue(v, env, x.Index)) 2503 op = IndexOp 2504 case *adt.SliceExpr: 2505 a = append(a, remakeValue(v, env, x.X)) 2506 a = append(a, remakeValue(v, env, x.Lo)) 2507 a = append(a, remakeValue(v, env, x.Hi)) 2508 op = SliceOp 2509 case *adt.CallExpr: 2510 // Interpret "and" and "or" builtin semantically. 2511 if fn, ok := x.Fun.(*adt.Builtin); ok && len(x.Args) == 1 && 2512 (fn.Name == "or" || fn.Name == "and") { 2513 2514 iter, _ := remakeValue(v, env, x.Args[0]).List() 2515 for iter.Next() { 2516 a = append(a, iter.Value()) 2517 } 2518 2519 op = OrOp 2520 if fn.Name == "and" { 2521 op = AndOp 2522 } 2523 2524 if len(a) == 0 { 2525 // Mimic semantics of builtin. 2526 switch op { 2527 case AndOp: 2528 a = append(a, remakeValue(v, env, &adt.Top{})) 2529 case OrOp: 2530 a = append(a, remakeValue(v, env, &adt.Bottom{ 2531 Code: adt.IncompleteError, 2532 Err: errors.Newf(x.Src.Fun.Pos(), "empty list in call to or"), 2533 })) 2534 } 2535 op = NoOp 2536 } 2537 break 2538 } 2539 a = append(a, remakeValue(v, env, x.Fun)) 2540 for _, arg := range x.Args { 2541 a = append(a, remakeValue(v, env, arg)) 2542 } 2543 op = CallOp 2544 case *adt.BuiltinValidator: 2545 a = append(a, remakeValue(v, env, x.Builtin)) 2546 for _, arg := range x.Args { 2547 a = append(a, remakeValue(v, env, arg)) 2548 } 2549 op = CallOp 2550 2551 case *adt.StructLit: 2552 hasEmbed := false 2553 fields := []adt.Decl{} 2554 for _, d := range x.Decls { 2555 switch d.(type) { 2556 default: 2557 fields = append(fields, d) 2558 case adt.Value: 2559 fields = append(fields, d) 2560 case adt.Expr: 2561 hasEmbed = true 2562 } 2563 } 2564 2565 if !hasEmbed { 2566 a = append(a, v) 2567 break 2568 } 2569 2570 ctx := v.ctx() 2571 2572 n := v.v 2573 2574 if len(fields) > 0 { 2575 n = &adt.Vertex{ 2576 Parent: v.v.Parent, 2577 Label: v.v.Label, 2578 } 2579 2580 s := &adt.StructLit{} 2581 if k := v.v.Kind(); k != adt.StructKind && k != BottomKind { 2582 // TODO: we should also add such a declaration for embeddings 2583 // of structs with definitions. However, this is currently 2584 // also not supported at the CUE level. If we do, it may be 2585 // best handled with a special mode of unification. 2586 s.Decls = append(s.Decls, &adt.BasicType{K: k}) 2587 } 2588 s.Decls = append(s.Decls, fields...) 2589 c := adt.MakeRootConjunct(env, s) 2590 n.AddConjunct(c) 2591 n.Finalize(ctx) 2592 n.Parent = v.v.Parent 2593 } 2594 2595 // Simulate old embeddings. 2596 envEmbed := &adt.Environment{ 2597 Up: env, 2598 Vertex: n, 2599 } 2600 2601 for _, d := range x.Decls { 2602 switch x := d.(type) { 2603 case adt.Value: 2604 case adt.Expr: 2605 // embedding 2606 n := &adt.Vertex{Label: v.v.Label} 2607 c := adt.MakeRootConjunct(envEmbed, x) 2608 n.AddConjunct(c) 2609 n.Finalize(ctx) 2610 n.Parent = v.v.Parent 2611 a = append(a, makeValue(v.idx, n, v.parent_)) 2612 } 2613 } 2614 2615 // Could be done earlier, but keep struct with fields at end. 2616 if len(fields) > 0 { 2617 a = append(a, makeValue(v.idx, n, v.parent_)) 2618 } 2619 2620 if len(a) == 1 { 2621 return a[0].Expr() 2622 } 2623 op = adt.AndOp 2624 2625 default: 2626 a = append(a, v) 2627 } 2628 return op, a 2629 }