Skip to content

Commit

Permalink
playground: rip out the use of cuelang.org/go/internal
Browse files Browse the repository at this point in the history
It was used for encoding a CUE value as either CUE, JSON, or YAML.
Luckily, doing that is not particularly hard, and can be accomplished
in under twenty lines of Go, only having to copy-paste upstream's
internal.ToFile function.

The entire machinery to copy internal packages over can go now.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Ic75a899daa88bf508e994f8ae4072b08ddba238c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1208062
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
  • Loading branch information
mvdan committed Jan 30, 2025
1 parent cc2c604 commit f4f9042
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 4,675 deletions.
57 changes: 0 additions & 57 deletions playground/_scripts/revendorToolsInternal.bash

This file was deleted.

1 change: 0 additions & 1 deletion playground/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
package main

//go:generate ./_scripts/cpWasmExec.bash
//go:generate ./_scripts/revendorToolsInternal.bash
10 changes: 4 additions & 6 deletions playground/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ module github.com/cue-lang/cuelang.org/playground

go 1.23

require (
cuelang.org/go v0.12.0
github.com/cockroachdb/apd/v3 v3.2.1
golang.org/x/text v0.21.0
gopkg.in/yaml.v3 v3.0.1
)
require cuelang.org/go v0.12.0

require (
cuelabs.dev/go/oci/ociregistry v0.0.0-20241125120445-2c00c104c6e1 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/emicklei/proto v1.13.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
Expand All @@ -22,4 +18,6 @@ require (
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/oauth2 v0.25.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
85 changes: 38 additions & 47 deletions playground/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@ package main

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/load"
"cuelang.org/go/cue/token"
"cuelang.org/go/pkg/encoding/json"
"github.com/cue-lang/cuelang.org/playground/internal/cuelang_org_go_internal/encoding"
"github.com/cue-lang/cuelang.org/playground/internal/cuelang_org_go_internal/filetypes"
cueyaml "cuelang.org/go/encoding/yaml"
)

type function string
Expand Down Expand Up @@ -144,57 +142,50 @@ func handleCUECompile(in input, fn function, out output, inputVal string) (strin
if err := v.Validate(validateOpts...); err != nil {
return "", err
}
f, err := filetypes.ParseFile(string(out)+":-", filetypes.Export)
if err != nil {
var buf bytes.Buffer
errors.Print(&buf, err, nil)
panic(fmt.Errorf("failed to parse file from %v: %s", string(out)+":-", buf.Bytes()))
}
var outBuf bytes.Buffer
encConf := &encoding.Config{
Out: &outBuf,
}
e, err := encoding.NewEncoder(ctx, f, encConf)
if err != nil {
return "", fmt.Errorf("failed to build encoder: %v", err)
}

// TODO(mvdan): Note that formatOpts appear to do nothing at all.
// For instance, the tests indent JSON with four spaces instead of two.
//
// TODO(myitcv): make the sharing of format options consistent with
// formatInput.
var formatOpts []format.Option
// TODO(mvdan): once we have a consistent interface for encodings,
// this could possibly be an exposed API upstream, much like cue/load
// already knows how to decode each of these.
// For now, we emulate the essence of what cmd/cue does via internal/encoding
// to encode a CUE value as CUE, JSON, and YAML.
var dst []byte
var err error
switch out {
case outputCUE:
formatOpts = append(formatOpts, format.TabIndent(true))
case outputJSON, outputYaml:
formatOpts = append(formatOpts,
format.TabIndent(false),
format.UseSpaces(2),
)
node := v.Syntax(syntaxOpts...)
dst, err = format.Node(toFile(node))
case outputJSON:
dst, err = json.MarshalIndent(v, "", " ")
dst = append(dst, '\n')
case outputYaml:
dst, err = cueyaml.Encode(v)
}
encConf.Format = formatOpts
synF := getSyntax(v, syntaxOpts)
if err := e.EncodeFile(synF); err != nil {
if err != nil {
return "", fmt.Errorf("failed to encode: %w", err)
}
return outBuf.String(), nil
return string(dst), nil
}

// getSyntax is copied from cmd/cue/cmd/eval.go
func getSyntax(v cue.Value, opts []cue.Option) *ast.File {
n := v.Syntax(opts...)
switch x := n.(type) {
case *ast.File:
return x
// toFile is copied from CUE's internal.toFile;
// mainly needed so that CUE outputs don't always contain braces
// for the top-level struct in a value.
func toFile(n ast.Node) *ast.File {
if n == nil {
return nil
}
switch n := n.(type) {
case *ast.StructLit:
return &ast.File{Decls: x.Elts}
f := &ast.File{Decls: n.Elts}
// Ensure that the comments attached to the struct literal are not lost.
ast.SetComments(f, ast.Comments(n))
return f
case ast.Expr:
ast.SetRelPos(x, token.NoSpace)
return &ast.File{Decls: []ast.Decl{&ast.EmbedDecl{Expr: x}}}
ast.SetRelPos(n, token.NoSpace)
return &ast.File{Decls: []ast.Decl{&ast.EmbedDecl{Expr: n}}}
case *ast.File:
return n
default:
panic("unreachable")
panic(fmt.Sprintf("Unsupported node type %T", n))
}
}

Expand All @@ -212,11 +203,11 @@ func formatInput(in input, s string) string {
}
return string(b)
case inputJSON:
res, err := json.Indent([]byte(s), "", " ")
if err != nil {
var buf bytes.Buffer
if err := json.Indent(&buf, []byte(s), "", " "); err != nil {
return s
}
return res
return buf.String()
default:
panic(fmt.Errorf("don't know how to format %q", in))
}
Expand Down
Loading

0 comments on commit f4f9042

Please sign in to comment.