Skip to content

Commit 53d18cc

Browse files
committed
cue: allow omitting leading "#" for LookupDef
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 <[email protected]>
1 parent 5515ee9 commit 53d18cc

File tree

4 files changed

+32
-36
lines changed

4 files changed

+32
-36
lines changed

cue/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,13 @@ func (v Value) LookupDef(name string) Value {
15241524
return newChildValue(&o, i)
15251525
}
15261526
}
1527+
if !strings.HasPrefix(name, "#") {
1528+
alt := v.LookupDef("#" + name)
1529+
// Use the original error message if this resulted in an error as well.
1530+
if alt.Err() == nil {
1531+
return alt
1532+
}
1533+
}
15271534
return newErrValue(v, ctx.mkErr(v.path.v,
15281535
"defintion %q not found", name))
15291536
}

internal/filetypes/filetypes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestFromFile(t *testing.T) {
4242
}{{
4343
name: "must specify encoding",
4444
in: build.File{},
45-
out: `FileInfo.encoding: non-concrete value string`,
45+
out: `#FileInfo.encoding: non-concrete value string`,
4646
}, {
4747
// Default without any
4848
name: "cue",

internal/filetypes/types.cue

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ package build
2323
// There
2424

2525
// A File corresponds to a Go build.File.
26-
File :: {
26+
#File: {
2727
filename: string
28-
encoding: Encoding
29-
interpretation?: Interpretation
30-
form?: Form
28+
encoding: #Encoding
29+
interpretation?: #Interpretation
30+
form?: #Form
3131
tags?: {[string]: string}
3232
}
3333

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

4040
// A FileInfo defines how a file is encoded and interpreted.
41-
FileInfo :: {
42-
File
41+
#FileInfo: {
42+
#File
4343

4444
// For each of these fields it is explained what a true value means
4545
// for encoding/decoding.
@@ -68,12 +68,11 @@ modes: _
6868
// In input mode, settings flags are interpreted as what is allowed to occur
6969
// in the input. The default settings, therefore, tend to be permissive.
7070
modes: input: {
71-
Default :: {
71+
#Default: {
7272
encoding: *"cue" | _
7373
...
7474
}
75-
76-
FileInfo :: x, x = {
75+
#FileInfo: x, let x = {
7776
docs: *true | false
7877
attributes: *true | false
7978
}
@@ -86,12 +85,11 @@ modes: input: {
8685
}
8786

8887
modes: export: {
89-
Default :: {
88+
#Default: {
9089
encoding: *"json" | _
9190
...
9291
}
93-
94-
FileInfo :: x, x = {
92+
#FileInfo: x, let x = {
9593
docs: true | *false
9694
attributes: true | *false
9795
}
@@ -101,7 +99,7 @@ modes: export: {
10199
}
102100

103101
modes: ouptut: {
104-
FileInfo :: x, x = {
102+
#FileInfo: x, let x = {
105103
docs: true | *false
106104
attributes: true | *false
107105
}
@@ -112,11 +110,11 @@ modes: ouptut: {
112110

113111
// eval is a legacy mode
114112
modes: eval: {
115-
Default :: {
113+
#Default: {
116114
encoding: *"cue" | _
117115
...
118116
}
119-
FileInfo :: x, x = {
117+
#FileInfo: x, let x = {
120118
docs: true | *false
121119
attributes: true | *false
122120
}
@@ -126,12 +124,11 @@ modes: eval: {
126124
}
127125

128126
modes: def: {
129-
Default :: {
127+
#Default: {
130128
encoding: *"cue" | _
131129
...
132130
}
133-
134-
FileInfo :: x, x = {
131+
#FileInfo: x, let x = {
135132
docs: *true | false
136133
attributes: *true | false
137134
}
@@ -159,16 +156,15 @@ extensions: {
159156
}
160157

161158
// A Encoding indicates a file format for representing a program.
162-
Encoding :: !="" // | error("no encoding specified")
159+
#Encoding: !="" // | error("no encoding specified")
163160

164161
// An Interpretation determines how a certain program should be interpreted.
165162
// For instance, data may be interpreted as describing a schema, which itself
166163
// can be converted to a CUE schema.
167-
Interpretation :: string
168-
169-
Form :: string
164+
#Interpretation: string
165+
#Form: string
170166

171-
file: FileInfo & {
167+
file: #FileInfo & {
172168

173169
filename: "foo.json"
174170
form: "schema"
@@ -219,7 +215,7 @@ tags: {
219215
}
220216

221217
// forms defines schema for all forms. It does not include the form ID.
222-
forms: [Name=string]: FileInfo
218+
forms: [Name=string]: #FileInfo
223219

224220
forms: "": _
225221

@@ -327,7 +323,7 @@ encodings: code: {
327323
stream: false
328324
}
329325

330-
interpretations: [Name=string]: FileInfo
326+
interpretations: [Name=string]: #FileInfo
331327

332328
interpretations: "": _
333329

internal/filetypes/types.go

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)