this repo has no description
0
fork

Configure Feed

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

internal/encoding/yaml: use adt.Unify instead of Value.Unify

This change should by itself be a no-op, as we
still need to pass cycle information through to
the respective OpContexts. This will be done in
a followup CL. We include these changes in a
separate CL, though, to be able to isolate any
issues that may arise from this change.

Issue #3649

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I2915f5d0e54fbd07bf20093e3da4a0ce4ae30cbc
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208700
Reviewed-by: Matthew Sackman <matthew@cue.works>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+47 -8
+3 -1
encoding/yaml/yaml.go
··· 24 24 "cuelang.org/go/cue/ast" 25 25 cueyaml "cuelang.org/go/internal/encoding/yaml" 26 26 "cuelang.org/go/internal/source" 27 + "cuelang.org/go/internal/value" 27 28 ) 28 29 29 30 // Extract parses the YAML specified by src to a CUE expression. If ··· 96 97 // Validate validates the YAML and confirms it matches the constraints 97 98 // specified by v. For YAML streams, all values must match v. 98 99 func Validate(b []byte, v cue.Value) error { 99 - _, err := cueyaml.Validate(b, v) 100 + ctx := value.OpContext(v) 101 + _, err := cueyaml.Validate(ctx, b, v) 100 102 return err 101 103 }
+15 -5
internal/encoding/yaml/validate.go
··· 19 19 "io" 20 20 21 21 "cuelang.org/go/cue" 22 + "cuelang.org/go/internal/core/adt" 22 23 "cuelang.org/go/internal/pkg" 24 + "cuelang.org/go/internal/value" 23 25 ) 24 26 25 27 // Validate validates YAML and confirms it is an instance of schema. 26 28 // If the YAML source is a stream, every object must match v. 27 - func Validate(b []byte, v cue.Value) (bool, error) { 29 + // 30 + // If Validate is called in a broader context, like a validation or function 31 + // call, the cycle context of n should be accumulated in c before this call. 32 + // This can be done by using the Expr method on the CallContext. 33 + func Validate(c *adt.OpContext, b []byte, v cue.Value) (bool, error) { 28 34 d := NewDecoder("yaml.Validate", b) 29 35 r := v.Context() 30 36 for { ··· 50 56 // if err := v.Subsume(inst.Value(), cue.Final()); err != nil { 51 57 // return false, err 52 58 // } 53 - x = v.Unify(x) 59 + vx := adt.Unify(c, value.Vertex(x), value.Vertex(v)) 54 60 if err := x.Err(); err != nil { 55 61 return false, err 56 62 } 63 + x = value.Make(c, vx) 64 + 57 65 if err := x.Validate(cue.Concrete(true)); err != nil { 58 66 // Strip error codes: incomplete errors are terminal in this case. 59 67 var b pkg.Bottomer ··· 69 77 // specified by v using unification. This means that b must be consistent with, 70 78 // but does not have to be an instance of v. If the YAML source is a stream, 71 79 // every object must match v. 72 - func ValidatePartial(b []byte, v cue.Value) (bool, error) { 80 + func ValidatePartial(c *adt.OpContext, b []byte, v cue.Value) (bool, error) { 73 81 d := NewDecoder("yaml.ValidatePartial", b) 74 82 r := v.Context() 75 83 for { ··· 86 94 return false, err 87 95 } 88 96 89 - if x := v.Unify(x); x.Err() != nil { 90 - return false, x.Err() 97 + vx := adt.Unify(c, value.Vertex(x), value.Vertex(v)) 98 + x = value.Make(c, vx) 99 + if err := x.Err(); err != nil { 100 + return false, err 91 101 } 92 102 } 93 103 }
+24
internal/value/value.go
··· 45 45 panic("unreachable") 46 46 } 47 47 48 + // OpContext returns an OpContext with proper node formatting initialized. 49 + func OpContext[Ctx *cue.Runtime | *cue.Context | cue.Value](c Ctx) *adt.OpContext { 50 + var r *runtime.Runtime 51 + var v *adt.Vertex 52 + switch x := any(c).(type) { 53 + case *cue.Runtime: 54 + r = (*runtime.Runtime)(x) 55 + r.Init() 56 + case *cue.Context: 57 + r = (*runtime.Runtime)(x) 58 + case cue.Value: 59 + r, v = ToInternal(x) 60 + default: 61 + panic("unreachable") 62 + } 63 + return eval.NewContext(r, v) 64 + } 65 + 48 66 func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) { 49 67 var t types.Value 50 68 v.Core(&t) 51 69 return t.R, t.V 70 + } 71 + 72 + func Vertex(v cue.Value) *adt.Vertex { 73 + var t types.Value 74 + v.Core(&t) 75 + return t.V 52 76 } 53 77 54 78 // Make wraps cue.MakeValue.
+5 -2
pkg/encoding/yaml/manual.go
··· 22 22 "cuelang.org/go/cue/ast" 23 23 cueyaml "cuelang.org/go/internal/encoding/yaml" 24 24 "cuelang.org/go/internal/pkg" 25 + "cuelang.org/go/internal/value" 25 26 ) 26 27 27 28 // Marshal returns the YAML encoding of v. ··· 86 87 // Validate validates YAML and confirms it is an instance of schema. 87 88 // If the YAML source is a stream, every object must match v. 88 89 func Validate(b []byte, v pkg.Schema) (bool, error) { 89 - return cueyaml.Validate(b, v) 90 + ctx := value.OpContext(v) 91 + return cueyaml.Validate(ctx, b, v) 90 92 } 91 93 92 94 // ValidatePartial validates YAML and confirms it matches the constraints ··· 94 96 // but does not have to be an instance of v. If the YAML source is a stream, 95 97 // every object must match v. 96 98 func ValidatePartial(b []byte, v pkg.Schema) (bool, error) { 97 - return cueyaml.ValidatePartial(b, v) 99 + ctx := value.OpContext(v) 100 + return cueyaml.ValidatePartial(ctx, b, v) 98 101 }