Skip to content

Commit 6594f45

Browse files
committed
cue/interpreter/embed: add support for embedding
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]>
1 parent c7f9334 commit 6594f45

File tree

12 files changed

+791
-7
lines changed

12 files changed

+791
-7
lines changed

cmd/cue/cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"cuelang.org/go/cue"
3131
"cuelang.org/go/cue/cuecontext"
3232
"cuelang.org/go/cue/errors"
33+
"cuelang.org/go/cue/interpreter/embed"
3334
"cuelang.org/go/cue/stats"
3435
"cuelang.org/go/internal"
3536
"cuelang.org/go/internal/core/adt"
@@ -220,6 +221,9 @@ For more information on writing CUE configuration files see cuelang.org.`,
220221
DisableSuggestions: true,
221222
}
222223

224+
embedding := cuecontext.Interpreter(embed.New())
225+
rootContextOptions = append(rootContextOptions, embedding)
226+
223227
c := &Command{
224228
Command: cmd,
225229
root: cmd,
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
exec cue eval
2+
cmp stdout out/noembed
3+
4+
env CUE_EXPERIMENT=embed
5+
6+
exec cue eval
7+
cmp stdout out/eval
8+
9+
exec cue export --out cue
10+
cmp stdout out/export
11+
12+
exec cue vet
13+
cmp stdout out/vet
14+
15+
-- test.cue --
16+
@extern(embed)
17+
18+
package foo
19+
20+
a: _ @embed(file="test.json")
21+
22+
b: _ @embed(file="input.yaml")
23+
24+
c: _ @embed(file="test.json", type=text)
25+
26+
d: _ @embed(glob="y/*.*", type=yaml)
27+
28+
d: _ @embed(glob="x/*.yaml") // merge into the same map
29+
30+
f: _ @embed(file="openapi.json", type=openapi)
31+
32+
g: _ @embed(file="openapi.json") // test no auto mode!
33+
34+
35+
special: {
36+
// These are all valid.
37+
underscoreFile: _ @embed(file="y/_test.json")
38+
dotFile: _ @embed(file="y/.test.json")
39+
underscoreDir: _ @embed(file="_y/test.json")
40+
dotDir: _ @embed(file=".y/test.json")
41+
42+
// TODO: fix nested modules are currently not supported, but we may opt to
43+
// support them in the future. This is currently not handled. It should
44+
// probably be up to the loader to provide a fs.FS that handles this
45+
// according to spec.
46+
nestedModJSON: _ @embed(file="a/b/foo.json")
47+
nestedModFile: _ @embed(file="a/b/cue.mod/modules.cue")
48+
}
49+
50+
-- test.json --
51+
{ "x": 34 }
52+
-- input.yaml --
53+
a1: 2
54+
55+
-- y/test.json --
56+
{ "x": 34 }
57+
-- y/_test.json --
58+
{ "z": 45 }
59+
-- y/.test.json --
60+
{ "z": 46 }
61+
-- _y/test.json --
62+
{ "z": 47 }
63+
-- .y/test.json --
64+
{ "z": 48 }
65+
-- _z/test.json --
66+
-- x/input.yaml --
67+
a1: 2
68+
-- a/b/cue.mod/modules.cue --
69+
module: "acme.com"
70+
language: version: "v0.9.0"
71+
-- a/b/foo.json --
72+
{"a": 1, "b": 2}
73+
-- openapi.json --
74+
{
75+
"openapi": "3.0.0",
76+
"info": {
77+
"title": "My OpenAPI",
78+
"version": "v1alpha1"
79+
},
80+
"paths": {},
81+
"components": {
82+
"schemas": {
83+
"Bar": {
84+
"type": "object",
85+
"required": [
86+
"foo"
87+
],
88+
"properties": {
89+
"foo": {
90+
"$ref": "#/components/schemas/Foo"
91+
}
92+
}
93+
},
94+
"Foo": {
95+
"type": "object",
96+
"required": [
97+
"a",
98+
"b"
99+
],
100+
"properties": {
101+
"a": {
102+
"type": "integer"
103+
},
104+
"b": {
105+
"type": "integer",
106+
"minimum": 0,
107+
"exclusiveMaximum": 10
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}
114+
-- out/noembed --
115+
a: _
116+
b: _
117+
c: _
118+
d: _
119+
f: _
120+
g: _
121+
special: {
122+
underscoreFile: _
123+
dotFile: _
124+
underscoreDir: _
125+
dotDir: _
126+
nestedModJSON: _
127+
nestedModFile: _
128+
}
129+
-- out/eval --
130+
a: {
131+
x: 34
132+
}
133+
b: {
134+
a1: 2
135+
}
136+
c: """
137+
{ "x": 34 }
138+
139+
"""
140+
d: {
141+
"y/.test.json": {
142+
z: 46
143+
}
144+
"y/_test.json": {
145+
z: 45
146+
}
147+
"x/input.yaml": {
148+
a1: 2
149+
}
150+
"y/test.json": {
151+
x: 34
152+
}
153+
}
154+
f: {
155+
info: {
156+
title: "My OpenAPI"
157+
version: "v1alpha1"
158+
}
159+
#Bar: {
160+
foo: {
161+
a: int
162+
b: uint & <10
163+
}
164+
}
165+
#Foo: {
166+
a: int
167+
b: uint & <10
168+
}
169+
}
170+
g: {
171+
openapi: "3.0.0"
172+
info: {
173+
title: "My OpenAPI"
174+
version: "v1alpha1"
175+
}
176+
paths: {}
177+
components: {
178+
schemas: {
179+
Bar: {
180+
type: "object"
181+
required: ["foo"]
182+
properties: {
183+
foo: {
184+
$ref: "#/components/schemas/Foo"
185+
}
186+
}
187+
}
188+
Foo: {
189+
type: "object"
190+
required: ["a", "b"]
191+
properties: {
192+
a: {
193+
type: "integer"
194+
}
195+
b: {
196+
type: "integer"
197+
minimum: 0
198+
exclusiveMaximum: 10
199+
}
200+
}
201+
}
202+
}
203+
}
204+
}
205+
special: {
206+
underscoreFile: {
207+
z: 45
208+
}
209+
dotFile: {
210+
z: 46
211+
}
212+
underscoreDir: {
213+
z: 47
214+
}
215+
dotDir: {
216+
z: 48
217+
}
218+
nestedModJSON: {
219+
a: 1
220+
b: 2
221+
}
222+
nestedModFile: {
223+
module: "acme.com"
224+
language: {
225+
version: "v0.9.0"
226+
}
227+
}
228+
}
229+
-- out/export --
230+
a: x: 34
231+
b: a1: 2
232+
c: """
233+
{ "x": 34 }
234+
235+
"""
236+
d: {
237+
"y/.test.json": z: 46
238+
"y/_test.json": z: 45
239+
"x/input.yaml": a1: 2
240+
"y/test.json": x: 34
241+
}
242+
f: info: {
243+
title: "My OpenAPI"
244+
version: "v1alpha1"
245+
}
246+
g: {
247+
openapi: "3.0.0"
248+
info: {
249+
title: "My OpenAPI"
250+
version: "v1alpha1"
251+
}
252+
paths: {}
253+
components: schemas: {
254+
Bar: {
255+
type: "object"
256+
required: ["foo"]
257+
properties: foo: $ref: "#/components/schemas/Foo"
258+
}
259+
Foo: {
260+
type: "object"
261+
required: ["a", "b"]
262+
properties: {
263+
a: type: "integer"
264+
b: {
265+
type: "integer"
266+
minimum: 0
267+
exclusiveMaximum: 10
268+
}
269+
}
270+
}
271+
}
272+
}
273+
special: {
274+
// These are all valid.
275+
underscoreFile: z: 45
276+
dotFile: z: 46
277+
underscoreDir: z: 47
278+
dotDir: z: 48
279+
280+
// TODO: fix nested modules are currently not supported, but we may opt to
281+
// support them in the future. This is currently not handled. It should
282+
// probably be up to the loader to provide a fs.FS that handles this
283+
// according to spec.
284+
nestedModJSON: {
285+
a: 1
286+
b: 2
287+
}
288+
nestedModFile: {
289+
module: "acme.com"
290+
language: version: "v0.9.0"
291+
}
292+
}
293+
-- out/vet --

0 commit comments

Comments
 (0)