Skip to content

Commit

Permalink
playground: exporting is always final
Browse files Browse the repository at this point in the history
The old logic had two different spots where it decided
whether or not the concrete boolean should be set.
In particular, the first one was based on the output format,
presumably since JSON and YAML are only available in functionExport.

Instead, derive this boolean from fn being functionExport.

Note that we use Final but not Concrete here.
We don't want to use Concrete, since that would break "export to CUE"
playground examples out there which use incomplete values.
A TODO is added to add a reminder with a bit more context.

Also apply the concrete boolean consistently for Validate and Syntax;
for Syntax it was only being applied when the output format was CUE.

Fixes #2417.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Id1d5023ac6304cdfd5fe6c27343021a2c63d98f6
Dispatch-Trailer: {"type":"trybot","CL":1170814,"patchset":2,"ref":"refs/changes/14/1170814/2","targetBranch":"alpha"}
  • Loading branch information
mvdan authored and cueckoo committed Oct 16, 2023
1 parent 40fada6 commit ae9f57c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
43 changes: 24 additions & 19 deletions playground/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,29 @@ func handleCUECompile(in input, fn function, out output, inputVal string) (strin
if err := v.Err(); err != nil {
return "", fmt.Errorf("failed to build: %w", err)
}
concrete := true
switch out {
case outputCUE:
concrete = false
case outputJSON, outputYaml:
case outputCUE, outputJSON, outputYaml:
default:
return "", fmt.Errorf("unknown ouput type: %v", out)
}
if err := v.Validate(cue.Concrete(concrete)); err != nil {
cueOpts := []cue.Option{
cue.Docs(true),
cue.Attributes(true),
cue.Optional(true),
cue.Definitions(true),
}
if fn == functionExport {
// TODO(mvdan): note that functionExport doesn't actually mimic "cue export",
// since it doesn't require values to be concrete.
// This can be good for some playground examples with incomplete values that people are already sharing,
// but it's confusing for that mode to be called "export" given the inconsistency with cmd/cue.
//
// In the near future, one possible option would be to show cue@export@cue as an "eval" mode in the UI
// for backwards compatibility, and add a new proper "export" mode as cue@exportv2@cue.
// We could do a transition like this when we switch over to alpha.cuelang.org too.
cueOpts = append(cueOpts, cue.Final())
}
if err := v.Validate(cueOpts...); err != nil {
return "", err
}
f, err := filetypes.ParseFile(string(out)+":-", filetypes.Export)
Expand All @@ -113,27 +127,18 @@ func handleCUECompile(in input, fn function, out output, inputVal string) (strin
return "", fmt.Errorf("failed to build encoder: %v", err)
}

syn := []cue.Option{
cue.Docs(true),
cue.Attributes(true),
cue.Optional(true),
cue.Definitions(true),
}
var opts []format.Option
var formatOpts []format.Option
switch out {
case outputCUE:
if fn != functionDef {
syn = append(syn, cue.Concrete(true))
}
opts = append(opts, format.TabIndent(true))
formatOpts = append(formatOpts, format.TabIndent(true))
case outputJSON, outputYaml:
opts = append(opts,
formatOpts = append(formatOpts,
format.TabIndent(false),
format.UseSpaces(2),
)
}
encConf.Format = opts
synF := getSyntax(v, syn)
encConf.Format = formatOpts
synF := getSyntax(v, cueOpts)
if err := e.EncodeFile(synF); err != nil {
return "", fmt.Errorf("failed to encode: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions playground/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var testTable = []struct {

// Incomplete values.
{inputCUE, functionExport, outputCUE, "foo: int", "foo: int\n", ""},
{inputCUE, functionExport, outputJSON, "foo: int", "", "foo: incomplete value int"},
{inputCUE, functionExport, outputJSON, "foo: int", "", "failed to encode: foo: incomplete value int"},

// Selecting defaults.
{inputCUE, functionExport, outputCUE, "foo: int | *3", "foo: 3\n", ""},
Expand All @@ -55,8 +55,8 @@ var testTable = []struct {
[ID=_]: x: y: ID
"foo": {}
`,
"{\n \"foo\": {\n \"x\": {\n \"y\": \"foo\"\n }\n }\n}\n",
"",
"failed to encode: foo.x.y: incomplete value string",
},

// Cases which one could argue should be errors, beyond just being incomplete.
Expand Down

0 comments on commit ae9f57c

Please sign in to comment.