From 53d18cc0cd158fcd8292be4884f01985b00bf479 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Tue, 2 Jun 2020 16:24:48 +0200 Subject: [PATCH] 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 --- cue/types.go | 7 ++++ internal/filetypes/filetypes_test.go | 2 +- internal/filetypes/types.cue | 48 +++++++++++++--------------- internal/filetypes/types.go | 11 ++----- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/cue/types.go b/cue/types.go index 179b1330b..a4b717abb 100644 --- a/cue/types.go +++ b/cue/types.go @@ -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)) } diff --git a/internal/filetypes/filetypes_test.go b/internal/filetypes/filetypes_test.go index 15eec971d..090266d10 100644 --- a/internal/filetypes/filetypes_test.go +++ b/internal/filetypes/filetypes_test.go @@ -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", diff --git a/internal/filetypes/types.cue b/internal/filetypes/types.cue index 93d471461..1ddb67b97 100644 --- a/internal/filetypes/types.cue +++ b/internal/filetypes/types.cue @@ -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. @@ -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 } @@ -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 } @@ -101,7 +99,7 @@ modes: export: { } modes: ouptut: { - FileInfo :: x, x = { + #FileInfo: x, let x = { docs: true | *false attributes: true | *false } @@ -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 } @@ -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 } @@ -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" @@ -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: "": _ @@ -327,7 +323,7 @@ encodings: code: { stream: false } -interpretations: [Name=string]: FileInfo +interpretations: [Name=string]: #FileInfo interpretations: "": _ diff --git a/internal/filetypes/types.go b/internal/filetypes/types.go index a8c79ee75..fdbcc216a 100644 --- a/internal/filetypes/types.go +++ b/internal/filetypes/types.go @@ -9,13 +9,6 @@ import ( "cuelang.org/go/encoding/gocode/gocodec" ) -var cuegenvalFileInfo = cuegenMake("FileInfo", &FileInfo{}) - -// Validate validates x. -func (x *FileInfo) Validate() error { - return cuegenCodec.Validate(cuegenvalFileInfo, x) -} - var cuegenCodec, cuegenInstance = func() (*gocodec.Codec, *cue.Instance) { var r *cue.Runtime r = &cue.Runtime{} @@ -47,5 +40,5 @@ func cuegenMake(name string, x interface{}) cue.Value { return v } -// Data size: 1132 bytes. -var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\xdcWO\x8b\xdc6\x14\xb7&[\xa8DR\xda\x0fPP}\b\xe9@\xe7Z\x18\b\xb9\xa4\x81\\J\xe9u\t\x8b\u05a3\xf1\xba\xb1%c\xcbe\x97\xee\x1c\u06a6i?p\u03d9\xf2\xf4$\u02d2=Iw!P\xaa\u02cc\u007fO\xef\xff{\xd2\u04e3\xe3\x9f+\xb2:\xfe\x95\x91\xe3oY\xf6\xed\xf1\xd7\a\x84<\xacTo\x84*\xe4sa\x04\xe0\xe4\x019\xfbQkCV\x199\xfbA\x98+\xf20#\x9f\xbc\xa8j\u0653\xe3\xdb,\u02fe<\xfe\xb1\"\xe4\xb3\xf3W\xc5 7\xfb\xaav\x9co3r|\x93eO\x8e\xbf? \xe4\u04c0\xbf\xc9\u020a\x9c}/\x1a\t\x82\xce,\u0232,{\xf7\xc5#\xb0\x84\x90\x15!\xd4\u0734\xb2\xdf\x14\x83$\xef>\xff\xbb\x15\xc5kQJ~9T\xf5\x8e1P\u0377[\xfe\v\xa3 U\x89Fn\xb9[\xbd\xe9*U2*U\xa1w\x95*G\xc2w\x0e`\xb4RFvm'\x8d0\x95V\u03f6\xfce\x040\xba\xd7]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ea\xfa\n\xf3B\xf3|\xcb/\xfc\xc7|~\x1c/l?H\xf2[\x9e\xdb\xea\xb7\xff\xfc\xac\x95\xdc\xc3i1\xc67\xf2\x93\x88\xfc5\u007f\x9c\"\x8c&\xf7u*/\xbe\xb9Sj|\x87\u03e8\xd1m\x9eR\xe3{=9#\xc6L\xd8\x00,\x85\u0245f\xe6\xf2\xb2\xe1'\u04c7Zf\xa3mH\x06\xc6\x1d2\x00#-\xfe\xda\x17B2g\xf9\x8e\x8b\xb2\xb3\x9c\x15\x8f\u0191\u007f\xbf\xe1q\xa4\x97#\x9c\xc6.\x99\xc9\xed\xf3j,#?\xeb\u0151I{9}\x1e\x85w\x1a\x0e\xf8\xf1<9:6\x9d#\x17c\xb0\x18\x82E\xaf\xd2\xeevo\xc7d\xfc\x81\xder\ub085Q\x88sxF\xd8/\x16\xa6\x1c\x87\xc2\x17\x1b\a\x98\x14\x85[#\xa0\x05$\x17\xc5\xda\u0363X\xbb\xb7\xde9}1\xac\x96asm&\x92\xe1\xf8\x03\xb4\xd4\xce\x05\x8b\x96\x1a0<\xa6\xbc:\xfb\x05\aNU\xcbp\xba\xa4\x8f\x83|\xaf\xf5\u01bd4'/^\xffP9\xb0xF\xbb\xfb\xc9\x15\x1e^'\x9a\xe9\xf4\xb3*\x9e\xe4N\xb0\x9fxF}\x80\x97e\xd9?\x01\x00\x00\xff\xffjB\xe5G\x9d\x11\x00\x00") +// Data size: 1131 bytes. +var cuegenInstanceData = []byte("\x01\x1f\x8b\b\x00\x00\x00\x00\x00\x00\xff\xd4W_\x8b\xe4D\x10O\u03eeb7\xa7\xe8\a\x10b\x16\x8es\xc0y\x15\x06\x8e}9\x0f\xeeE\xc4\xd7\xe5Xz\x93N6^&\x1d\x92\x8e\xec\xe2\u0383z\x9e~Y\xbf\u008dTW\xffIw2w\xee\u0081\xce\xcbn~\xd5\xf5\xbf\xaa\xbb\xea\xd3\u00df+\xb2:\xfc\x95\x90\xc3oI\xf2\xed\xe1\xd7\x13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80\xa50\x99\xd0\xcc\\^6\xfch\xfeP\xcbl\xa4\xf5\xc9\xc0\xb8C\x06`\x94\u017fz3\x88\x06,\xdbrAv\x96\xb3b\xd10\xf2\xef6<\x8c\xf4r\x84\xe3\xd8E\xb3\xb8^\xaa\\\x19\xd9!/\x8cL\xdc\xcc\xf1Z\xe4\xb73\x1c\xec\xc3A\xd296\x1d \x17c\xb0\x18\x82E\xaf\xe2\xf66\vc4\xf8@o\x99\xdf%\xf3CP\x9a\xc2\xfa\xa0\xbf\x98\x9fo\f\n_\u030d.1\n\xaf\x86GsH.\x8a\u0547\x9dX}\xb6)\x8c\xbe\x10n\x97au\xa3&\x92\xe1\xfe\x03\xb4\x92\xc6\x05\x8dV\x120\xbc\xa7\xac:\xfd\x057\x8e^\xe2\xdd\xf5\x12o\x05Y)\xe5\u01ac\x98\x93U\xd7n({\x16\x8eg\x0f\xb8\xbb\xfc\xcau\xa4\x9d\x8e/T\xe1\x18w\x84\xfd\xc8\x02\xf5\x1e^\x96$\xff\x04\x00\x00\xff\xff^\x83Y\u0756\x11\x00\x00")