Skip to content

Commit c9c8cb4

Browse files
committed
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 <[email protected]> Change-Id: I2915f5d0e54fbd07bf20093e3da4a0ce4ae30cbc Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208700 Reviewed-by: Matthew Sackman <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 11be031 commit c9c8cb4

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

encoding/yaml/yaml.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"cuelang.org/go/cue/ast"
2525
cueyaml "cuelang.org/go/internal/encoding/yaml"
2626
"cuelang.org/go/internal/source"
27+
"cuelang.org/go/internal/value"
2728
)
2829

2930
// Extract parses the YAML specified by src to a CUE expression. If
@@ -96,6 +97,7 @@ func EncodeStream(iter cue.Iterator) ([]byte, error) {
9697
// Validate validates the YAML and confirms it matches the constraints
9798
// specified by v. For YAML streams, all values must match v.
9899
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)
100102
return err
101103
}

internal/encoding/yaml/validate.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ import (
1919
"io"
2020

2121
"cuelang.org/go/cue"
22+
"cuelang.org/go/internal/core/adt"
2223
"cuelang.org/go/internal/pkg"
24+
"cuelang.org/go/internal/value"
2325
)
2426

2527
// Validate validates YAML and confirms it is an instance of schema.
2628
// 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) {
2834
d := NewDecoder("yaml.Validate", b)
2935
r := v.Context()
3036
for {
@@ -50,10 +56,12 @@ func Validate(b []byte, v cue.Value) (bool, error) {
5056
// if err := v.Subsume(inst.Value(), cue.Final()); err != nil {
5157
// return false, err
5258
// }
53-
x = v.Unify(x)
59+
vx := adt.Unify(c, value.Vertex(x), value.Vertex(v))
5460
if err := x.Err(); err != nil {
5561
return false, err
5662
}
63+
x = value.Make(c, vx)
64+
5765
if err := x.Validate(cue.Concrete(true)); err != nil {
5866
// Strip error codes: incomplete errors are terminal in this case.
5967
var b pkg.Bottomer
@@ -69,7 +77,7 @@ func Validate(b []byte, v cue.Value) (bool, error) {
6977
// specified by v using unification. This means that b must be consistent with,
7078
// but does not have to be an instance of v. If the YAML source is a stream,
7179
// 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) {
7381
d := NewDecoder("yaml.ValidatePartial", b)
7482
r := v.Context()
7583
for {
@@ -86,8 +94,10 @@ func ValidatePartial(b []byte, v cue.Value) (bool, error) {
8694
return false, err
8795
}
8896

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
91101
}
92102
}
93103
}

internal/value/value.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,36 @@ func Context[Ctx *cue.Runtime | *cue.Context | cue.Value | *adt.OpContext](ctx C
4545
panic("unreachable")
4646
}
4747

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+
4866
func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) {
4967
var t types.Value
5068
v.Core(&t)
5169
return t.R, t.V
5270
}
5371

72+
func Vertex(v cue.Value) *adt.Vertex {
73+
var t types.Value
74+
v.Core(&t)
75+
return t.V
76+
}
77+
5478
// Make wraps cue.MakeValue.
5579
func Make(ctx *adt.OpContext, v adt.Value) cue.Value {
5680
return Context(ctx).Encode(v)

pkg/encoding/yaml/manual.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"cuelang.org/go/cue/ast"
2323
cueyaml "cuelang.org/go/internal/encoding/yaml"
2424
"cuelang.org/go/internal/pkg"
25+
"cuelang.org/go/internal/value"
2526
)
2627

2728
// Marshal returns the YAML encoding of v.
@@ -86,13 +87,15 @@ func UnmarshalStream(data []byte) (ast.Expr, error) {
8687
// Validate validates YAML and confirms it is an instance of schema.
8788
// If the YAML source is a stream, every object must match v.
8889
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)
9092
}
9193

9294
// ValidatePartial validates YAML and confirms it matches the constraints
9395
// specified by v using unification. This means that b must be consistent with,
9496
// but does not have to be an instance of v. If the YAML source is a stream,
9597
// every object must match v.
9698
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)
98101
}

0 commit comments

Comments
 (0)