Skip to content

Commit

Permalink
internal/filetypes: simplify some of the CUE logic
Browse files Browse the repository at this point in the history
hasEncoding returned both whether the encoding field was concrete
and whether the field had a concrete default value,
when in fact only the latter was necessary - no tests change behavior
with this change, even though a couple of unit tests did run into
the edge case of hasEncoding returning (false, true).

The change to have hasEncoding return two values is relatively recent,
from https://cue-review.googlesource.com/c/cue/+/5410 in mid 2020,
and it seems like none of the added test cases there needed the added
logic to hasEncoding.

While here, move "tags" back to the top-level scope.
Just like the "interpretations" struct, it does not vary between modes.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I7d238617f249bced74d0e5ee640e92e86e5d4dda
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193575
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mvdan committed Apr 23, 2024
1 parent 25e8713 commit a21e2df
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 80 deletions.
41 changes: 18 additions & 23 deletions internal/filetypes/filetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,31 +259,26 @@ func ParseFile(s string, mode Mode) (*build.File, error) {
return toFile(modeVal, fileVal, file)
}

func hasEncoding(v cue.Value) (concrete, hasDefault bool) {
func hasEncoding(v cue.Value) bool {
enc := v.LookupPath(cue.MakePath(cue.Str("encoding")))
d, _ := enc.Default()
return enc.IsConcrete(), d.IsConcrete()
return d.IsConcrete()
}

func toFile(modeVal, fileVal cue.Value, filename string) (*build.File, error) {
fileVal = fileVal.Fill(filename, "filename")

if concrete, hasDefault := hasEncoding(fileVal); !concrete {
if !hasEncoding(fileVal) {
if filename == "-" {
if !hasDefault {
fileVal = fileVal.Unify(modeVal.LookupPath(cue.MakePath(cue.Str("Default"))))
}
fileVal = fileVal.Unify(modeVal.LookupPath(cue.MakePath(cue.Str("Default"))))
} else if ext := fileExt(filename); ext != "" {
if x := modeVal.LookupPath(cue.MakePath(cue.Str("extensions"), cue.Str(ext))); x.Exists() || !hasDefault {
fileVal = fileVal.Unify(x)
if err := fileVal.Err(); err != nil {
return nil, errors.Newf(token.NoPos,
"unknown file extension %s", ext)
}
extFile := modeVal.LookupPath(cue.MakePath(cue.Str("extensions"), cue.Str(ext)))
fileVal = fileVal.Unify(extFile)
if err := fileVal.Err(); err != nil {
return nil, errors.Newf(token.NoPos, "unknown file extension %s", ext)
}
} else if !hasDefault {
return nil, errors.Newf(token.NoPos,
"no encoding specified for file %q", filename)
} else {
return nil, errors.Newf(token.NoPos, "no encoding specified for file %q", filename)
}
}

Expand All @@ -295,19 +290,19 @@ func toFile(modeVal, fileVal cue.Value, filename string) (*build.File, error) {
return f, nil
}

func parseType(s string, mode Mode) (modeVal, fileVal cue.Value, _ 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")))

if s != "" {
for _, t := range strings.Split(s, "+") {
if p := strings.IndexByte(t, '='); p >= 0 {
fileVal = fileVal.Fill(t[p+1:], "tags", t[:p])
if scope != "" {
for _, tag := range strings.Split(scope, "+") {
tagName, tagVal, ok := strings.Cut(tag, "=")
if ok {
fileVal = fileVal.Fill(tagVal, "tags", tagName)
} else {
info := modeVal.LookupPath(cue.MakePath(cue.Str("tags"), cue.Str(t)))
info := typesValue.LookupPath(cue.MakePath(cue.Str("tags"), cue.Str(tag)))
if !info.Exists() {
return cue.Value{}, cue.Value{}, errors.Newf(token.NoPos,
"unknown filetype %s", t)
return cue.Value{}, cue.Value{}, errors.Newf(token.NoPos, "unknown filetype %s", tag)
}
fileVal = fileVal.Unify(info)
}
Expand Down
115 changes: 58 additions & 57 deletions internal/filetypes/types.cue
Original file line number Diff line number Diff line change
Expand Up @@ -168,63 +168,6 @@ modes: [string]: {
// ".pb": tags.binpb // binarypb
}

// tags maps command line tags to file properties.
tags: {
schema: form: "schema"
graph: form: "graph"
dag: form: "dag"
data: form: "data"

cue: encoding: "cue"

json: encoding: "json"
jsonl: encoding: "jsonl"
yaml: encoding: "yaml"
proto: encoding: "proto"
textproto: encoding: "textproto"
// "binpb": encodings.binproto

// pb is used either to indicate binary encoding, or to indicate
pb: *{
encoding: "binarypb"
interpretation: ""
} | {
encoding: !="binarypb"
interpretation: "pb"
}

text: {
encoding: "text"
form: "data"
}
binary: {
encoding: "binary"
form: "data"
}
go: {
encoding: "code"
interpretation: ""
tags: lang: "go"
}
code: {
encoding: "code"
interpretation: ""
tags: lang: *"" | string
}

auto: {
interpretation: "auto"
encoding: *"json" | _
}
jsonschema: {
interpretation: "jsonschema"
encoding: *"json" | _
}
openapi: {
interpretation: "openapi"
encoding: *"json" | _
}
}
// encodings: "": error("no encoding specified")

encodings: cue: {
Expand Down Expand Up @@ -365,3 +308,61 @@ interpretations: pb: {
forms.data
stream: true
}

// tags maps command line tags to file properties.
tags: {
schema: form: "schema"
graph: form: "graph"
dag: form: "dag"
data: form: "data"

cue: encoding: "cue"

json: encoding: "json"
jsonl: encoding: "jsonl"
yaml: encoding: "yaml"
proto: encoding: "proto"
textproto: encoding: "textproto"
// "binpb": encodings.binproto

// pb is used either to indicate binary encoding, or to indicate
pb: *{
encoding: "binarypb"
interpretation: ""
} | {
encoding: !="binarypb"
interpretation: "pb"
}

text: {
encoding: "text"
form: "data"
}
binary: {
encoding: "binary"
form: "data"
}
go: {
encoding: "code"
interpretation: ""
tags: lang: "go"
}
code: {
encoding: "code"
interpretation: ""
tags: lang: *"" | string
}

auto: {
interpretation: "auto"
encoding: *"json" | _
}
jsonschema: {
interpretation: "jsonschema"
encoding: *"json" | _
}
openapi: {
interpretation: "openapi"
encoding: *"json" | _
}
}

0 comments on commit a21e2df

Please sign in to comment.