Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: allow omitting leading "#" for LookupDef
Browse files Browse the repository at this point in the history
This is mostly to smooth the transition to #-style definitions.

Case in point: the internal filetypes package was upgraded and
to new-style definitions (included in this CL). The conversion was
automatic, but the code now requires a leading `#` prefix for
definitions. This change allows using `cue fix` without breaking
code.

Note that now a field name can never be a valid definition name
if it does not start with `#`.

Change-Id: Id69a9601e8d244c27a0855f454b730ce9baf6b9f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6240
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 6, 2020
1 parent 5515ee9 commit 53d18cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
7 changes: 7 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,13 @@ func (v Value) LookupDef(name string) Value {
return newChildValue(&o, i)
}
}
if !strings.HasPrefix(name, "#") {
alt := v.LookupDef("#" + name)
// Use the original error message if this resulted in an error as well.
if alt.Err() == nil {
return alt
}
}
return newErrValue(v, ctx.mkErr(v.path.v,
"defintion %q not found", name))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/filetypes/filetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestFromFile(t *testing.T) {
}{{
name: "must specify encoding",
in: build.File{},
out: `FileInfo.encoding: non-concrete value string`,
out: `#FileInfo.encoding: non-concrete value string`,
}, {
// Default without any
name: "cue",
Expand Down
48 changes: 22 additions & 26 deletions internal/filetypes/types.cue
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ package build
// There

// A File corresponds to a Go build.File.
File :: {
#File: {
filename: string
encoding: Encoding
interpretation?: Interpretation
form?: Form
encoding: #Encoding
interpretation?: #Interpretation
form?: #Form
tags?: {[string]: string}
}

// Default is the file used for stdin and stdout. The settings depend
// on the file mode.
Default :: File & {
#Default: #File & {
filename: *"-" | string
}

// A FileInfo defines how a file is encoded and interpreted.
FileInfo :: {
File
#FileInfo: {
#File

// For each of these fields it is explained what a true value means
// for encoding/decoding.
Expand Down Expand Up @@ -68,12 +68,11 @@ modes: _
// In input mode, settings flags are interpreted as what is allowed to occur
// in the input. The default settings, therefore, tend to be permissive.
modes: input: {
Default :: {
#Default: {
encoding: *"cue" | _
...
}

FileInfo :: x, x = {
#FileInfo: x, let x = {
docs: *true | false
attributes: *true | false
}
Expand All @@ -86,12 +85,11 @@ modes: input: {
}

modes: export: {
Default :: {
#Default: {
encoding: *"json" | _
...
}

FileInfo :: x, x = {
#FileInfo: x, let x = {
docs: true | *false
attributes: true | *false
}
Expand All @@ -101,7 +99,7 @@ modes: export: {
}

modes: ouptut: {
FileInfo :: x, x = {
#FileInfo: x, let x = {
docs: true | *false
attributes: true | *false
}
Expand All @@ -112,11 +110,11 @@ modes: ouptut: {

// eval is a legacy mode
modes: eval: {
Default :: {
#Default: {
encoding: *"cue" | _
...
}
FileInfo :: x, x = {
#FileInfo: x, let x = {
docs: true | *false
attributes: true | *false
}
Expand All @@ -126,12 +124,11 @@ modes: eval: {
}

modes: def: {
Default :: {
#Default: {
encoding: *"cue" | _
...
}

FileInfo :: x, x = {
#FileInfo: x, let x = {
docs: *true | false
attributes: *true | false
}
Expand Down Expand Up @@ -159,16 +156,15 @@ extensions: {
}

// A Encoding indicates a file format for representing a program.
Encoding :: !="" // | error("no encoding specified")
#Encoding: !="" // | error("no encoding specified")

// An Interpretation determines how a certain program should be interpreted.
// For instance, data may be interpreted as describing a schema, which itself
// can be converted to a CUE schema.
Interpretation :: string

Form :: string
#Interpretation: string
#Form: string

file: FileInfo & {
file: #FileInfo & {

filename: "foo.json"
form: "schema"
Expand Down Expand Up @@ -219,7 +215,7 @@ tags: {
}

// forms defines schema for all forms. It does not include the form ID.
forms: [Name=string]: FileInfo
forms: [Name=string]: #FileInfo

forms: "": _

Expand Down Expand Up @@ -327,7 +323,7 @@ encodings: code: {
stream: false
}

interpretations: [Name=string]: FileInfo
interpretations: [Name=string]: #FileInfo

interpretations: "": _

Expand Down
11 changes: 2 additions & 9 deletions internal/filetypes/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53d18cc

Please sign in to comment.