Skip to content

Commit

Permalink
encoding/gocode/gocodec: remove cue.Runtime usages
Browse files Browse the repository at this point in the history
This removes most usages of cue.Runtime in gocodec tests, except for a
smoke test that ensures the deprecated cue.Runtime still works with
the API.

Also, the package doc is fixed to reference the correct package name.

Updates #2480.

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I464e0d730fbdfaf5afc8bebd547b7f1590f2dd93
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194862
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
NoamTD authored and mvdan committed May 17, 2024
1 parent 27a0190 commit 078020b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion encoding/gocode/gocodec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package codec converts Go to and from CUE and validates Go values based on
// Package gocodec converts Go to and from CUE and validates Go values based on
// CUE constraints.
//
// CUE constraints can be used to validate Go types as well as fill out
Expand Down
33 changes: 16 additions & 17 deletions encoding/gocode/gocodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,20 @@ func TestComplete(t *testing.T) {
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := &cue.Runtime{}
codec := New(r, nil)
ctx := cuecontext.New()
codec := New(ctx, nil)

v, err := codec.ExtractType(tc.value)
if err != nil {
t.Fatal(err)
}

if tc.constraints != "" {
inst, err := r.Compile(tc.name, tc.constraints)
if err != nil {
c := ctx.CompileString(tc.constraints, cue.Filename(tc.name))
if err := c.Err(); err != nil {
t.Fatal(err)
}
v = v.Unify(inst.Value())
v = v.Unify(c)
}

err = codec.Complete(v, tc.value)
Expand All @@ -243,17 +243,17 @@ func TestEncode(t *testing.T) {
dst: new(int),
want: 4,
}}
r := &cue.Runtime{}
c := New(r, nil)
ctx := cuecontext.New()
c := New(ctx, nil)

for _, tc := range testCases {
t.Run("", func(t *testing.T) {
inst, err := r.Compile("test", tc.in)
if err != nil {
in := ctx.CompileString(tc.in, cue.Filename("test"))
if err := in.Err(); err != nil {
t.Fatal(err)
}

err = c.Encode(inst.Value(), tc.dst)
err := c.Encode(in, tc.dst)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestDecode(t *testing.T) {
}
}`,
}}
c := New(&cue.Runtime{}, nil)
c := New(cuecontext.New(), nil)

for _, tc := range testCases {
t.Run("", func(t *testing.T) {
Expand Down Expand Up @@ -336,21 +336,20 @@ func TestX(t *testing.T) {
wantErr = fail
)

r := &cue.Runtime{}
codec := New(r, nil)
ctx := cuecontext.New()
codec := New(ctx, nil)

v, err := codec.ExtractType(value)
if err != nil {
t.Fatal(err)
}

if constraints != "" {
inst, err := r.Compile(name, constraints)
if err != nil {
c := ctx.CompileString(constraints, cue.Filename(name))
if err := c.Err(); err != nil {
t.Fatal(err)
}
w := inst.Value()
v = v.Unify(w)
v = v.Unify(c)
}

err = codec.Validate(v, value)
Expand Down

0 comments on commit 078020b

Please sign in to comment.