|
15 | 15 | package filetypes
|
16 | 16 |
|
17 | 17 | import (
|
18 |
| - "fmt" |
19 |
| - "sync" |
20 |
| - |
21 |
| - "cuelang.org/go/cue" |
22 | 18 | "cuelang.org/go/cue/build"
|
23 |
| - "cuelang.org/go/cue/cuecontext" |
24 |
| - "cuelang.org/go/cue/errors" |
25 |
| - "cuelang.org/go/cue/token" |
26 |
| - "github.com/google/go-cmp/cmp" |
27 | 19 | )
|
28 | 20 |
|
29 |
| -// evalMu guards against concurrent execution of the CUE evaluator. |
30 |
| -// See issue https://cuelang.org/issue/2733 |
31 |
| -var evalMu sync.Mutex |
32 |
| - |
33 | 21 | //go:generate go run -tags bootstrap ./generate.go
|
34 | 22 |
|
35 | 23 | func toFile(mode Mode, sc *scope, filename string) (*build.File, error) {
|
36 |
| - f0, err0 := toFileGenerated(mode, sc, filename) |
37 |
| - f1, err1 := toFileOrig(mode, sc, filename) |
38 |
| - if (err0 != nil) != (err1 != nil) { |
39 |
| - panic(fmt.Errorf("toFile discrepancy on error return; mode %v; scope %v; filename %v:\nold: %v\nnew: %v", mode, sc, filename, err1, err0)) |
40 |
| - } else if diff := cmp.Diff(f0, f1); diff != "" { |
41 |
| - panic(fmt.Errorf("toFile result discrepancy; mode %v; scope %v; filename %v:\n%s", mode, sc, filename, diff)) |
42 |
| - } |
43 |
| - |
44 |
| - return f0, err0 |
45 |
| -} |
46 |
| - |
47 |
| -func toFileOrig(mode Mode, sc *scope, filename string) (*build.File, error) { |
48 |
| - fileVal := cuecontext.New().CompileString("{}") |
49 |
| - for tagName := range sc.topLevel { |
50 |
| - info := lookup(typesValue, "tagInfo", tagName) |
51 |
| - if info.Exists() { |
52 |
| - fileVal = fileVal.Unify(info) |
53 |
| - } else { |
54 |
| - return nil, errors.Newf(token.NoPos, "unknown filetype %s", tagName) |
55 |
| - } |
56 |
| - } |
57 |
| - modeVal := lookup(typesValue, "modes", mode.String()) |
58 |
| - fileVal = fileVal.Unify(lookup(modeVal, "FileInfo")) |
59 |
| - return toFile1(modeVal, fileVal, filename, sc) |
60 |
| -} |
61 |
| - |
62 |
| -func toFile1(modeVal, fileVal cue.Value, filename string, sc *scope) (*build.File, error) { |
63 |
| - if !lookup(fileVal, "encoding").Exists() { |
64 |
| - if ext := fileExt(filename); ext != "" { |
65 |
| - extFile := lookup(modeVal, "extensions", ext) |
66 |
| - if !extFile.Exists() { |
67 |
| - return nil, errors.Newf(token.NoPos, "unknown file extension %s", ext) |
68 |
| - } |
69 |
| - fileVal = fileVal.Unify(extFile) |
70 |
| - } else { |
71 |
| - return nil, errors.Newf(token.NoPos, "no encoding specified for file %q", filename) |
72 |
| - } |
73 |
| - } |
74 |
| - allowedSubsidiaryBool := lookup(fileVal, "boolTags") |
75 |
| - for tagName, val := range sc.subsidiaryBool { |
76 |
| - if !lookup(allowedSubsidiaryBool, tagName).Exists() { |
77 |
| - return nil, errors.Newf(token.NoPos, "tag %s is not allowed in this context", tagName) |
78 |
| - } |
79 |
| - fileVal = fileVal.FillPath(cue.MakePath(cue.Str("boolTags"), cue.Str(tagName)), val) |
80 |
| - } |
81 |
| - allowedSubsidiaryString := lookup(fileVal, "tags") |
82 |
| - for tagName, val := range sc.subsidiaryString { |
83 |
| - if !lookup(allowedSubsidiaryString, tagName).Exists() { |
84 |
| - return nil, errors.Newf(token.NoPos, "tag %s is not allowed in this context", tagName) |
85 |
| - } |
86 |
| - fileVal = fileVal.FillPath(cue.MakePath(cue.Str("tags"), cue.Str(tagName)), val) |
87 |
| - } |
88 |
| - |
89 |
| - // Note that the filename is only filled in the Go value, and not the CUE value. |
90 |
| - // This makes no difference to the logic, but saves a non-trivial amount of evaluator work. |
91 |
| - f := &build.File{Filename: filename} |
92 |
| - if err := fileVal.Decode(f); err != nil { |
93 |
| - return nil, errors.Wrapf(err, token.NoPos, |
94 |
| - "could not determine file type") |
95 |
| - } |
96 |
| - return f, nil |
| 24 | + return toFileGenerated(mode, sc, filename) |
97 | 25 | }
|
98 | 26 |
|
99 | 27 | // FromFile returns detailed file info for a given build file. It ignores b.Tags and
|
100 | 28 | // b.BoolTags, instead assuming that any tag handling has already been processed
|
101 | 29 | // by [ParseArgs] or similar.
|
102 | 30 | // The b.Encoding field must be non-empty.
|
103 | 31 | func FromFile(b *build.File, mode Mode) (*FileInfo, error) {
|
104 |
| - fi0, err0 := fromFileGenerated(b, mode) |
105 |
| - fi1, err1 := fromFileOrig(b, mode) |
106 |
| - if (err0 != nil) != (err1 != nil) { |
107 |
| - panic(fmt.Errorf("toFile discrepancy on error return; mode %v; file %#v:\nold: %v\nnew: %v", mode, b, err1, err0)) |
108 |
| - } else if diff := cmp.Diff(fi1, fi0); diff != "" { |
109 |
| - panic(fmt.Errorf("toFile result discrepancy; mode %v; file %#v\n%s", mode, b, diff)) |
110 |
| - } |
111 |
| - |
112 |
| - return fi0, err0 |
113 |
| -} |
114 |
| - |
115 |
| -func fromFileOrig(b *build.File, mode Mode) (*FileInfo, error) { |
116 |
| - evalMu.Lock() |
117 |
| - defer evalMu.Unlock() |
118 |
| - typesInit() |
119 |
| - modeVal := lookup(typesValue, "modes", mode.String()) |
120 |
| - fileVal := lookup(modeVal, "FileInfo") |
121 |
| - if b.Encoding != "" { |
122 |
| - fileVal = fileVal.FillPath(cue.MakePath(cue.Str("encoding")), b.Encoding) |
123 |
| - } |
124 |
| - if b.Interpretation != "" { |
125 |
| - fileVal = fileVal.FillPath(cue.MakePath(cue.Str("interpretation")), b.Interpretation) |
126 |
| - } |
127 |
| - if b.Form != "" { |
128 |
| - fileVal = fileVal.FillPath(cue.MakePath(cue.Str("form")), b.Form) |
129 |
| - } |
130 |
| - if b.Encoding == "" { |
131 |
| - return nil, errors.Newf(token.NoPos, "no encoding specified") |
132 |
| - } |
133 |
| - var errs errors.Error |
134 |
| - interpretation, _ := lookup(fileVal, "interpretation").String() |
135 |
| - if b.Form != "" { |
136 |
| - fileVal, errs = unifyWith(errs, fileVal, typesValue, "forms", string(b.Form)) |
137 |
| - // may leave some encoding-dependent options open in data mode. |
138 |
| - } else if interpretation != "" { |
139 |
| - // always sets form=*schema |
140 |
| - fileVal, errs = unifyWith(errs, fileVal, typesValue, "interpretations", interpretation) |
141 |
| - } |
142 |
| - if interpretation == "" { |
143 |
| - s, err := lookup(fileVal, "encoding").String() |
144 |
| - if err != nil { |
145 |
| - return nil, err |
146 |
| - } |
147 |
| - fileVal, errs = unifyWith(errs, fileVal, modeVal, "encodings", s) |
148 |
| - } |
149 |
| - |
150 |
| - fi := &FileInfo{} |
151 |
| - if err := fileVal.Decode(fi); err != nil { |
152 |
| - return nil, errors.Wrapf(err, token.NoPos, "could not parse arguments") |
153 |
| - } |
154 |
| - fi.Filename = b.Filename |
155 |
| - return fi, errs |
156 |
| -} |
157 |
| - |
158 |
| -// unifyWith returns the equivalent of `v1 & v2[field][value]`. |
159 |
| -func unifyWith(errs errors.Error, v1, v2 cue.Value, field, value string) (cue.Value, errors.Error) { |
160 |
| - v1 = v1.Unify(lookup(v2, field, value)) |
161 |
| - if err := v1.Err(); err != nil { |
162 |
| - errs = errors.Append(errs, |
163 |
| - errors.Newf(token.NoPos, "unknown %s %s", field, value)) |
164 |
| - } |
165 |
| - return v1, errs |
166 |
| -} |
167 |
| - |
168 |
| -// lookup looks up the given string field path in v. |
169 |
| -func lookup(v cue.Value, elems ...string) cue.Value { |
170 |
| - sels := make([]cue.Selector, len(elems)) |
171 |
| - for i := range elems { |
172 |
| - sels[i] = cue.Str(elems[i]) |
173 |
| - } |
174 |
| - return v.LookupPath(cue.MakePath(sels...)) |
| 32 | + return fromFileGenerated(b, mode) |
175 | 33 | }
|
0 commit comments