Skip to content

Commit

Permalink
internal/filetypes: simplify
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rogpeppe committed Sep 10, 2024
1 parent 7ced6fd commit fcf3306
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 103 deletions.
3 changes: 1 addition & 2 deletions internal/filetypes/filetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func FromFile(b *build.File, mode Mode) (*FileInfo, error) {
KeepDefaults: true,
Incomplete: true,
Imports: true,
Stream: true,
Docs: true,
Attributes: true,
}, nil
Expand Down Expand Up @@ -312,7 +311,7 @@ func toFile(modeVal, fileVal cue.Value, filename string) (*build.File, error) {

func parseType(scope string, mode Mode) (modeVal, fileVal cue.Value, _ error) {
modeVal = typesValue.LookupPath(cue.MakePath(cue.Str("modes"), cue.Str(mode.String())))
fileVal = modeVal.LookupPath(cue.MakePath(cue.Str("File")))
fileVal = modeVal.LookupPath(cue.MakePath(cue.Str("FileInfo")))

if scope != "" {
for _, tag := range strings.Split(scope, "+") {
Expand Down
105 changes: 53 additions & 52 deletions internal/filetypes/filetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestFromFile(t *testing.T) {
out: &FileInfo{
File: &build.File{
Filename: "",
Encoding: "cue",
Form: "schema",
Encoding: build.CUE,
Form: build.Schema,
},
Definitions: true,
Data: true,
Expand All @@ -83,8 +83,8 @@ func TestFromFile(t *testing.T) {
out: &FileInfo{
File: &build.File{
Filename: "",
Encoding: "cue",
Form: "data",
Encoding: build.CUE,
Form: build.Data,
},
Data: true,
Docs: true,
Expand All @@ -107,8 +107,8 @@ func TestFromFile(t *testing.T) {
out: &FileInfo{
File: &build.File{
Filename: "foo.yaml",
Encoding: "yaml",
Form: "graph",
Encoding: build.YAML,
Form: build.Graph,
},
Data: true,
References: true,
Expand All @@ -121,14 +121,14 @@ func TestFromFile(t *testing.T) {
name: "yaml+openapi",
in: build.File{
Filename: "foo.yaml",
Interpretation: "openapi",
Interpretation: build.OpenAPI,
},
out: &FileInfo{
File: &build.File{
Filename: "foo.yaml",
Encoding: "yaml",
Interpretation: "openapi",
Form: "schema",
Encoding: build.YAML,
Interpretation: build.OpenAPI,
Form: build.Schema,
},
Definitions: true,
Data: true,
Expand All @@ -151,9 +151,9 @@ func TestFromFile(t *testing.T) {
out: &FileInfo{
File: &build.File{
Filename: "data.json",
Encoding: "json",
Interpretation: "auto",
Form: "schema",
Encoding: build.JSON,
Interpretation: build.Auto,
Form: build.Schema,
},
Definitions: true,
Data: true,
Expand All @@ -176,9 +176,9 @@ func TestFromFile(t *testing.T) {
out: &FileInfo{
File: &build.File{
Filename: "foo.json",
Encoding: "json",
Encoding: build.JSON,
Interpretation: "jsonschema",
Form: "schema",
Form: build.Schema,
},
Definitions: true,
Data: true,
Expand All @@ -196,15 +196,15 @@ func TestFromFile(t *testing.T) {
name: "JSONOpenAPI",
in: build.File{
Filename: "foo.json",
Interpretation: "openapi",
Interpretation: build.OpenAPI,
},
mode: Def,
out: &FileInfo{
File: &build.File{
Filename: "foo.json",
Encoding: "json",
Interpretation: "openapi",
Form: "schema",
Encoding: build.JSON,
Interpretation: build.OpenAPI,
Form: build.Schema,
},
Definitions: true,
Data: true,
Expand All @@ -222,15 +222,15 @@ func TestFromFile(t *testing.T) {
name: "OpenAPIDefaults",
in: build.File{
Filename: "-",
Interpretation: "openapi",
Interpretation: build.OpenAPI,
},
mode: Def,
out: &FileInfo{
File: &build.File{
Filename: "-",
Encoding: "json",
Interpretation: "openapi",
Form: "schema",
Encoding: build.JSON,
Interpretation: build.OpenAPI,
Form: build.Schema,
},
Definitions: true,
Data: true,
Expand All @@ -253,8 +253,8 @@ func TestFromFile(t *testing.T) {
out: &FileInfo{
File: &build.File{
Filename: "foo.go",
Encoding: "code",
Form: "schema",
Encoding: build.Code,
Form: build.Schema,
Tags: map[string]string{"lang": "go"},
},
Definitions: true,
Expand All @@ -266,7 +266,6 @@ func TestFromFile(t *testing.T) {
KeepDefaults: true,
Incomplete: true,
Imports: true,
Stream: false,
Docs: true,
Attributes: true,
},
Expand All @@ -290,8 +289,8 @@ func TestParseFile(t *testing.T) {
mode: Input,
out: &build.File{
Filename: "file.json",
Encoding: "json",
Interpretation: "auto",
Encoding: build.JSON,
Interpretation: build.Auto,
},
}, {
in: ".json",
Expand All @@ -302,49 +301,50 @@ func TestParseFile(t *testing.T) {
mode: Input,
out: &build.File{
Filename: ".json.yaml",
Encoding: "yaml",
Interpretation: "auto",
Encoding: build.YAML,
Interpretation: build.Auto,
},
}, {
in: "file.json",
mode: Def,
out: &build.File{
Filename: "file.json",
Encoding: "json",
Encoding: build.JSON,
},
}, {
in: "schema:file.json",
out: &build.File{
Filename: "file.json",
Encoding: "json",
Interpretation: "auto",
Form: "schema",
Encoding: build.JSON,
Interpretation: build.Auto,
Form: build.Schema,
},
}, {
in: "openapi:-",
out: &build.File{
Filename: "-",
Encoding: "json",
Interpretation: "openapi",
Encoding: build.JSON,
Form: build.Schema,
Interpretation: build.OpenAPI,
},
}, {
in: "cue:file.json",
out: &build.File{
Filename: "file.json",
Encoding: "cue",
Encoding: build.CUE,
},
}, {
in: "cue+schema:-",
out: &build.File{
Filename: "-",
Encoding: "cue",
Form: "schema",
Encoding: build.CUE,
Form: build.Schema,
},
}, {
in: "code+lang=js:foo.x",
out: &build.File{
Filename: "foo.x",
Encoding: "code",
Encoding: build.Code,
Tags: map[string]string{"lang": "js"},
},
}, {
Expand All @@ -358,14 +358,14 @@ func TestParseFile(t *testing.T) {
mode: Input,
out: &build.File{
Filename: "-",
Encoding: "cue",
Encoding: build.CUE,
},
}, {
in: "-",
mode: Export,
out: &build.File{
Filename: "-",
Encoding: "json",
Encoding: build.JSON,
},
}}
for _, tc := range testCases {
Expand All @@ -385,36 +385,37 @@ func TestParseArgs(t *testing.T) {
out: []*build.File{
{
Filename: "foo.json",
Encoding: "json",
Interpretation: "auto",
Encoding: build.JSON,
Interpretation: build.Auto,
},
{
Filename: "baz.yaml",
Encoding: "yaml",
Interpretation: "auto",
Encoding: build.YAML,
Interpretation: build.Auto,
},
},
}, {
in: "data: foo.cue",
out: []*build.File{
{Filename: "foo.cue", Encoding: "cue", Form: "data"},
{Filename: "foo.cue", Encoding: build.CUE, Form: build.Data},
},
}, {
in: "json: foo.json bar.data jsonschema: bar.schema",
out: []*build.File{
{Filename: "foo.json", Encoding: "json"}, // no auto!
{Filename: "bar.data", Encoding: "json"},
{Filename: "foo.json", Encoding: build.JSON}, // no auto!
{Filename: "bar.data", Encoding: build.JSON},
{
Filename: "bar.schema",
Encoding: "json",
Encoding: build.JSON,
Form: build.Schema,
Interpretation: "jsonschema",
},
},
}, {
in: `json: c:\foo.json c:\path\to\file.dat`,
out: []*build.File{
{Filename: `c:\foo.json`, Encoding: "json"},
{Filename: `c:\path\to\file.dat`, Encoding: "json"},
{Filename: `c:\foo.json`, Encoding: build.JSON},
{Filename: `c:\path\to\file.dat`, Encoding: build.JSON},
},
}, {
in: "json: json+schema: bar.schema",
Expand Down
Loading

0 comments on commit fcf3306

Please sign in to comment.