Skip to content

Commit fcf3306

Browse files
committed
internal/filetypes: simplify
The `stream` attribute is not used or tested for anywhere and is therefore likely incorrect. Remove it: we can always reinstate if it's needed in the future. Also unify `FileInfo` with `File`. This has no direct semantic effect, but means that the two are more closely related, and allows us to add information to `interpretations` that also has an effect in `tagInfo`, something we'll use in a subsequent CL. The latter change does mean that we now get a non-empty `Form` in some cases when it was previously empty but that actually seems more correct: if we're interpreting as JSON Schema, for example, it seems right to treat it as schema not data. Also add some documentation and make `modes.[_].FileInfo` and `modes.[_].Default` into required fields as suggested by the TODO. Also change the tests to use `cue/build` constants, thus giving more confidence that they match the strings used in the CUE. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I3a9246a3060837ea03eae6daf3841ae305faa016 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200923 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 7ced6fd commit fcf3306

File tree

3 files changed

+80
-103
lines changed

3 files changed

+80
-103
lines changed

internal/filetypes/filetypes.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ func FromFile(b *build.File, mode Mode) (*FileInfo, error) {
9292
KeepDefaults: true,
9393
Incomplete: true,
9494
Imports: true,
95-
Stream: true,
9695
Docs: true,
9796
Attributes: true,
9897
}, nil
@@ -312,7 +311,7 @@ func toFile(modeVal, fileVal cue.Value, filename string) (*build.File, error) {
312311

313312
func parseType(scope string, mode Mode) (modeVal, fileVal cue.Value, _ error) {
314313
modeVal = typesValue.LookupPath(cue.MakePath(cue.Str("modes"), cue.Str(mode.String())))
315-
fileVal = modeVal.LookupPath(cue.MakePath(cue.Str("File")))
314+
fileVal = modeVal.LookupPath(cue.MakePath(cue.Str("FileInfo")))
316315

317316
if scope != "" {
318317
for _, tag := range strings.Split(scope, "+") {

internal/filetypes/filetypes_test.go

+53-52
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func TestFromFile(t *testing.T) {
5656
out: &FileInfo{
5757
File: &build.File{
5858
Filename: "",
59-
Encoding: "cue",
60-
Form: "schema",
59+
Encoding: build.CUE,
60+
Form: build.Schema,
6161
},
6262
Definitions: true,
6363
Data: true,
@@ -83,8 +83,8 @@ func TestFromFile(t *testing.T) {
8383
out: &FileInfo{
8484
File: &build.File{
8585
Filename: "",
86-
Encoding: "cue",
87-
Form: "data",
86+
Encoding: build.CUE,
87+
Form: build.Data,
8888
},
8989
Data: true,
9090
Docs: true,
@@ -107,8 +107,8 @@ func TestFromFile(t *testing.T) {
107107
out: &FileInfo{
108108
File: &build.File{
109109
Filename: "foo.yaml",
110-
Encoding: "yaml",
111-
Form: "graph",
110+
Encoding: build.YAML,
111+
Form: build.Graph,
112112
},
113113
Data: true,
114114
References: true,
@@ -121,14 +121,14 @@ func TestFromFile(t *testing.T) {
121121
name: "yaml+openapi",
122122
in: build.File{
123123
Filename: "foo.yaml",
124-
Interpretation: "openapi",
124+
Interpretation: build.OpenAPI,
125125
},
126126
out: &FileInfo{
127127
File: &build.File{
128128
Filename: "foo.yaml",
129-
Encoding: "yaml",
130-
Interpretation: "openapi",
131-
Form: "schema",
129+
Encoding: build.YAML,
130+
Interpretation: build.OpenAPI,
131+
Form: build.Schema,
132132
},
133133
Definitions: true,
134134
Data: true,
@@ -151,9 +151,9 @@ func TestFromFile(t *testing.T) {
151151
out: &FileInfo{
152152
File: &build.File{
153153
Filename: "data.json",
154-
Encoding: "json",
155-
Interpretation: "auto",
156-
Form: "schema",
154+
Encoding: build.JSON,
155+
Interpretation: build.Auto,
156+
Form: build.Schema,
157157
},
158158
Definitions: true,
159159
Data: true,
@@ -176,9 +176,9 @@ func TestFromFile(t *testing.T) {
176176
out: &FileInfo{
177177
File: &build.File{
178178
Filename: "foo.json",
179-
Encoding: "json",
179+
Encoding: build.JSON,
180180
Interpretation: "jsonschema",
181-
Form: "schema",
181+
Form: build.Schema,
182182
},
183183
Definitions: true,
184184
Data: true,
@@ -196,15 +196,15 @@ func TestFromFile(t *testing.T) {
196196
name: "JSONOpenAPI",
197197
in: build.File{
198198
Filename: "foo.json",
199-
Interpretation: "openapi",
199+
Interpretation: build.OpenAPI,
200200
},
201201
mode: Def,
202202
out: &FileInfo{
203203
File: &build.File{
204204
Filename: "foo.json",
205-
Encoding: "json",
206-
Interpretation: "openapi",
207-
Form: "schema",
205+
Encoding: build.JSON,
206+
Interpretation: build.OpenAPI,
207+
Form: build.Schema,
208208
},
209209
Definitions: true,
210210
Data: true,
@@ -222,15 +222,15 @@ func TestFromFile(t *testing.T) {
222222
name: "OpenAPIDefaults",
223223
in: build.File{
224224
Filename: "-",
225-
Interpretation: "openapi",
225+
Interpretation: build.OpenAPI,
226226
},
227227
mode: Def,
228228
out: &FileInfo{
229229
File: &build.File{
230230
Filename: "-",
231-
Encoding: "json",
232-
Interpretation: "openapi",
233-
Form: "schema",
231+
Encoding: build.JSON,
232+
Interpretation: build.OpenAPI,
233+
Form: build.Schema,
234234
},
235235
Definitions: true,
236236
Data: true,
@@ -253,8 +253,8 @@ func TestFromFile(t *testing.T) {
253253
out: &FileInfo{
254254
File: &build.File{
255255
Filename: "foo.go",
256-
Encoding: "code",
257-
Form: "schema",
256+
Encoding: build.Code,
257+
Form: build.Schema,
258258
Tags: map[string]string{"lang": "go"},
259259
},
260260
Definitions: true,
@@ -266,7 +266,6 @@ func TestFromFile(t *testing.T) {
266266
KeepDefaults: true,
267267
Incomplete: true,
268268
Imports: true,
269-
Stream: false,
270269
Docs: true,
271270
Attributes: true,
272271
},
@@ -290,8 +289,8 @@ func TestParseFile(t *testing.T) {
290289
mode: Input,
291290
out: &build.File{
292291
Filename: "file.json",
293-
Encoding: "json",
294-
Interpretation: "auto",
292+
Encoding: build.JSON,
293+
Interpretation: build.Auto,
295294
},
296295
}, {
297296
in: ".json",
@@ -302,49 +301,50 @@ func TestParseFile(t *testing.T) {
302301
mode: Input,
303302
out: &build.File{
304303
Filename: ".json.yaml",
305-
Encoding: "yaml",
306-
Interpretation: "auto",
304+
Encoding: build.YAML,
305+
Interpretation: build.Auto,
307306
},
308307
}, {
309308
in: "file.json",
310309
mode: Def,
311310
out: &build.File{
312311
Filename: "file.json",
313-
Encoding: "json",
312+
Encoding: build.JSON,
314313
},
315314
}, {
316315
in: "schema:file.json",
317316
out: &build.File{
318317
Filename: "file.json",
319-
Encoding: "json",
320-
Interpretation: "auto",
321-
Form: "schema",
318+
Encoding: build.JSON,
319+
Interpretation: build.Auto,
320+
Form: build.Schema,
322321
},
323322
}, {
324323
in: "openapi:-",
325324
out: &build.File{
326325
Filename: "-",
327-
Encoding: "json",
328-
Interpretation: "openapi",
326+
Encoding: build.JSON,
327+
Form: build.Schema,
328+
Interpretation: build.OpenAPI,
329329
},
330330
}, {
331331
in: "cue:file.json",
332332
out: &build.File{
333333
Filename: "file.json",
334-
Encoding: "cue",
334+
Encoding: build.CUE,
335335
},
336336
}, {
337337
in: "cue+schema:-",
338338
out: &build.File{
339339
Filename: "-",
340-
Encoding: "cue",
341-
Form: "schema",
340+
Encoding: build.CUE,
341+
Form: build.Schema,
342342
},
343343
}, {
344344
in: "code+lang=js:foo.x",
345345
out: &build.File{
346346
Filename: "foo.x",
347-
Encoding: "code",
347+
Encoding: build.Code,
348348
Tags: map[string]string{"lang": "js"},
349349
},
350350
}, {
@@ -358,14 +358,14 @@ func TestParseFile(t *testing.T) {
358358
mode: Input,
359359
out: &build.File{
360360
Filename: "-",
361-
Encoding: "cue",
361+
Encoding: build.CUE,
362362
},
363363
}, {
364364
in: "-",
365365
mode: Export,
366366
out: &build.File{
367367
Filename: "-",
368-
Encoding: "json",
368+
Encoding: build.JSON,
369369
},
370370
}}
371371
for _, tc := range testCases {
@@ -385,36 +385,37 @@ func TestParseArgs(t *testing.T) {
385385
out: []*build.File{
386386
{
387387
Filename: "foo.json",
388-
Encoding: "json",
389-
Interpretation: "auto",
388+
Encoding: build.JSON,
389+
Interpretation: build.Auto,
390390
},
391391
{
392392
Filename: "baz.yaml",
393-
Encoding: "yaml",
394-
Interpretation: "auto",
393+
Encoding: build.YAML,
394+
Interpretation: build.Auto,
395395
},
396396
},
397397
}, {
398398
in: "data: foo.cue",
399399
out: []*build.File{
400-
{Filename: "foo.cue", Encoding: "cue", Form: "data"},
400+
{Filename: "foo.cue", Encoding: build.CUE, Form: build.Data},
401401
},
402402
}, {
403403
in: "json: foo.json bar.data jsonschema: bar.schema",
404404
out: []*build.File{
405-
{Filename: "foo.json", Encoding: "json"}, // no auto!
406-
{Filename: "bar.data", Encoding: "json"},
405+
{Filename: "foo.json", Encoding: build.JSON}, // no auto!
406+
{Filename: "bar.data", Encoding: build.JSON},
407407
{
408408
Filename: "bar.schema",
409-
Encoding: "json",
409+
Encoding: build.JSON,
410+
Form: build.Schema,
410411
Interpretation: "jsonschema",
411412
},
412413
},
413414
}, {
414415
in: `json: c:\foo.json c:\path\to\file.dat`,
415416
out: []*build.File{
416-
{Filename: `c:\foo.json`, Encoding: "json"},
417-
{Filename: `c:\path\to\file.dat`, Encoding: "json"},
417+
{Filename: `c:\foo.json`, Encoding: build.JSON},
418+
{Filename: `c:\path\to\file.dat`, Encoding: build.JSON},
418419
},
419420
}, {
420421
in: "json: json+schema: bar.schema",

0 commit comments

Comments
 (0)