Skip to content

Commit

Permalink
cue/interpreter/embed: add support for embedding
Browse files Browse the repository at this point in the history
This is a first-stab and partial implementation of the
embedding proposal. See the TODO list included in
embed.go to see what is outstanding.

Issue #2031

This issue is not closed, as it also referes to the
complementary export attribute.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: Ic296a28aa009509f9a17913c7e5a0794de5a7a35
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196718
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Aram Hăvărneanu <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mpvl committed Jun 27, 2024
1 parent c7f9334 commit 6594f45
Show file tree
Hide file tree
Showing 12 changed files with 791 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/interpreter/embed"
"cuelang.org/go/cue/stats"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
Expand Down Expand Up @@ -220,6 +221,9 @@ For more information on writing CUE configuration files see cuelang.org.`,
DisableSuggestions: true,
}

embedding := cuecontext.Interpreter(embed.New())
rootContextOptions = append(rootContextOptions, embedding)

c := &Command{
Command: cmd,
root: cmd,
Expand Down
293 changes: 293 additions & 0 deletions cmd/cue/cmd/testdata/script/embed.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
exec cue eval
cmp stdout out/noembed

env CUE_EXPERIMENT=embed

exec cue eval
cmp stdout out/eval

exec cue export --out cue
cmp stdout out/export

exec cue vet
cmp stdout out/vet

-- test.cue --
@extern(embed)

package foo

a: _ @embed(file="test.json")

b: _ @embed(file="input.yaml")

c: _ @embed(file="test.json", type=text)

d: _ @embed(glob="y/*.*", type=yaml)

d: _ @embed(glob="x/*.yaml") // merge into the same map

f: _ @embed(file="openapi.json", type=openapi)

g: _ @embed(file="openapi.json") // test no auto mode!


special: {
// These are all valid.
underscoreFile: _ @embed(file="y/_test.json")
dotFile: _ @embed(file="y/.test.json")
underscoreDir: _ @embed(file="_y/test.json")
dotDir: _ @embed(file=".y/test.json")

// TODO: fix nested modules are currently not supported, but we may opt to
// support them in the future. This is currently not handled. It should
// probably be up to the loader to provide a fs.FS that handles this
// according to spec.
nestedModJSON: _ @embed(file="a/b/foo.json")
nestedModFile: _ @embed(file="a/b/cue.mod/modules.cue")
}

-- test.json --
{ "x": 34 }
-- input.yaml --
a1: 2

-- y/test.json --
{ "x": 34 }
-- y/_test.json --
{ "z": 45 }
-- y/.test.json --
{ "z": 46 }
-- _y/test.json --
{ "z": 47 }
-- .y/test.json --
{ "z": 48 }
-- _z/test.json --
-- x/input.yaml --
a1: 2
-- a/b/cue.mod/modules.cue --
module: "acme.com"
language: version: "v0.9.0"
-- a/b/foo.json --
{"a": 1, "b": 2}
-- openapi.json --
{
"openapi": "3.0.0",
"info": {
"title": "My OpenAPI",
"version": "v1alpha1"
},
"paths": {},
"components": {
"schemas": {
"Bar": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"$ref": "#/components/schemas/Foo"
}
}
},
"Foo": {
"type": "object",
"required": [
"a",
"b"
],
"properties": {
"a": {
"type": "integer"
},
"b": {
"type": "integer",
"minimum": 0,
"exclusiveMaximum": 10
}
}
}
}
}
}
-- out/noembed --
a: _
b: _
c: _
d: _
f: _
g: _
special: {
underscoreFile: _
dotFile: _
underscoreDir: _
dotDir: _
nestedModJSON: _
nestedModFile: _
}
-- out/eval --
a: {
x: 34
}
b: {
a1: 2
}
c: """
{ "x": 34 }

"""
d: {
"y/.test.json": {
z: 46
}
"y/_test.json": {
z: 45
}
"x/input.yaml": {
a1: 2
}
"y/test.json": {
x: 34
}
}
f: {
info: {
title: "My OpenAPI"
version: "v1alpha1"
}
#Bar: {
foo: {
a: int
b: uint & <10
}
}
#Foo: {
a: int
b: uint & <10
}
}
g: {
openapi: "3.0.0"
info: {
title: "My OpenAPI"
version: "v1alpha1"
}
paths: {}
components: {
schemas: {
Bar: {
type: "object"
required: ["foo"]
properties: {
foo: {
$ref: "#/components/schemas/Foo"
}
}
}
Foo: {
type: "object"
required: ["a", "b"]
properties: {
a: {
type: "integer"
}
b: {
type: "integer"
minimum: 0
exclusiveMaximum: 10
}
}
}
}
}
}
special: {
underscoreFile: {
z: 45
}
dotFile: {
z: 46
}
underscoreDir: {
z: 47
}
dotDir: {
z: 48
}
nestedModJSON: {
a: 1
b: 2
}
nestedModFile: {
module: "acme.com"
language: {
version: "v0.9.0"
}
}
}
-- out/export --
a: x: 34
b: a1: 2
c: """
{ "x": 34 }

"""
d: {
"y/.test.json": z: 46
"y/_test.json": z: 45
"x/input.yaml": a1: 2
"y/test.json": x: 34
}
f: info: {
title: "My OpenAPI"
version: "v1alpha1"
}
g: {
openapi: "3.0.0"
info: {
title: "My OpenAPI"
version: "v1alpha1"
}
paths: {}
components: schemas: {
Bar: {
type: "object"
required: ["foo"]
properties: foo: $ref: "#/components/schemas/Foo"
}
Foo: {
type: "object"
required: ["a", "b"]
properties: {
a: type: "integer"
b: {
type: "integer"
minimum: 0
exclusiveMaximum: 10
}
}
}
}
}
special: {
// These are all valid.
underscoreFile: z: 45
dotFile: z: 46
underscoreDir: z: 47
dotDir: z: 48

// TODO: fix nested modules are currently not supported, but we may opt to
// support them in the future. This is currently not handled. It should
// probably be up to the loader to provide a fs.FS that handles this
// according to spec.
nestedModJSON: {
a: 1
b: 2
}
nestedModFile: {
module: "acme.com"
language: version: "v0.9.0"
}
}
-- out/vet --
Loading

0 comments on commit 6594f45

Please sign in to comment.