-
Notifications
You must be signed in to change notification settings - Fork 9
/
cue.go
284 lines (235 loc) · 8.14 KB
/
cue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// Implements loading of Namespace-specific dialect of Cue which includes:
// * a Golang-like module system where modules are loaded from source transparently when needed;
// * support for @fn() attributes allowing to access runtime data from the environment.
package fncue
import (
"context"
"fmt"
"io/fs"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"k8s.io/utils/strings/slices"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
)
type KeyAndPath struct {
Key string
Target cue.Path
}
func (v *CueV) LookupPath(path string) *CueV {
return &CueV{Val: v.Val.LookupPath(cue.ParsePath(path))}
}
func (v *CueV) Exists() bool { return v.Val.Exists() }
func (v *CueV) FillPath(path cue.Path, rightSide interface{}) *CueV {
return &CueV{Val: v.Val.FillPath(path, rightSide)}
}
// Entry point to load Cue packages from a Namespace workspace.
type EvalCtx struct {
cache *snapshotCache
loader WorkspaceLoader
scope any
}
type snapshotCache struct {
mu sync.Mutex // Protects cuectx.
cuectx *cue.Context
bldctx *build.Context
parsed map[string]*build.Instance
built map[string]*Partial
}
// If set, "scope" are passed as a "Scope" BuildOption to "BuildInstance".
func NewEvalCtx(loader WorkspaceLoader, scope any) *EvalCtx {
return &EvalCtx{
cache: newSnapshotCache(),
loader: loader,
scope: scope,
}
}
func newSnapshotCache() *snapshotCache {
return &snapshotCache{
cuectx: cuecontext.New(),
bldctx: build.NewContext(),
parsed: map[string]*build.Instance{},
built: map[string]*Partial{},
}
}
func (ev *EvalCtx) EvalPackage(ctx context.Context, pkgname string) (*Partial, error) {
// We work around Cue's limited package management. Rather than maintaining package copies under
// a top-level cue.mod directory, we want instead a system more similar to Go's, with explicit
// version locking, and downloads into a common shared cache.
collectedImports := map[string]*CuePackage{}
if err := CollectImports(ctx, ev.loader, pkgname, collectedImports); err != nil {
return nil, err
}
pkg, ok := collectedImports[pkgname]
if !ok || len(pkg.Files) == 0 {
return nil, fnerrors.New("no cue package at %s?", pkgname)
}
// A foundation package definition has no package statement, which we refer to as the "_"
// import here.
return ev.cache.Eval(ctx, *pkg, pkgname+":_", collectedImports, ev.scope)
}
func (sc *snapshotCache) Eval(ctx context.Context, pkg CuePackage, pkgname string, collectedImports map[string]*CuePackage, scope any) (*Partial, error) {
sc.mu.Lock()
defer sc.mu.Unlock()
if _, has := sc.built[pkgname]; !has {
info, _ := astutil.ParseImportSpec(ast.NewImport(nil, pkgname))
p := sc.parseAndCacheInstance(ctx, pkg, info, collectedImports)
if len(p.DepsErrors) > 0 {
return nil, multierr.New(p.DepsErrors...)
}
partial, err := finishInstance(sc, sc.cuectx, p, pkg, collectedImports, scope)
if err != nil {
return partial, err
}
sc.built[pkgname] = partial
}
return sc.built[pkgname], nil
}
func finishInstance(sc *snapshotCache, cuectx *cue.Context, p *build.Instance, pkg CuePackage, collectedImports map[string]*CuePackage, scope any) (*Partial, error) {
buildOptions := []cue.BuildOption{}
if scope != nil {
buildOptions = append(buildOptions, cue.Scope(cuectx.Encode(scope)))
}
vv := cuectx.BuildInstance(p, buildOptions...)
partial := &Partial{Ctx: sc}
partial.Package = pkg
partial.Val = vv
var err error
partial.Left, err = parseTags(&partial.CueV)
if err != nil {
return nil, err
}
for _, dep := range collectedImports {
partial.CueImports = append(partial.CueImports, *dep)
}
sort.Slice(partial.CueImports, func(i, j int) bool {
return strings.Compare(partial.CueImports[i].ModuleName, partial.CueImports[j].ModuleName) < 0
})
if vv.Err() != nil {
// Even if there are errors, return the partially valid Cue value.
// This is useful to provide language features in LSP for not fully valid files.
return partial, WrapCueError(vv.Err(), func(p string) string {
// VSCode only supports linking of absolute paths in Output Channels.
// Also in the Terminal it surely does not support module paths (it will link
// example.com/module/package/path, but won't find example.com in the workspace).
// So currently we must print absolute paths here.
// Alternatives: print relative paths for workspace files and install a
// DocumentLinkProvider to resolve them.
// See https://github.com/microsoft/vscode/issues/586.
return absPathForModulePath(collectedImports, p)
})
}
return partial, nil
}
func (sc *snapshotCache) parseAndCacheInstance(ctx context.Context, pkg CuePackage, info astutil.ImportInfo, collectedImports map[string]*CuePackage) *build.Instance {
if p := sc.parsed[info.ID]; p != nil {
return p
}
p := sc.parseInstance(ctx, collectedImports, info, pkg)
sc.parsed[info.ID] = p
return p
}
func (sc *snapshotCache) parseInstance(ctx context.Context, collectedImports map[string]*CuePackage, info astutil.ImportInfo, pkg CuePackage) *build.Instance {
p := sc.bldctx.NewInstance(join(pkg.ModuleName, pkg.RelPath), func(pos token.Pos, path string) *build.Instance {
if IsStandardImportPath(path) {
return nil // Builtin.
}
info, _ := astutil.ParseImportSpec(ast.NewImport(nil, path))
if pkg, ok := collectedImports[info.Dir]; ok {
return sc.parseAndCacheInstance(ctx, *pkg, info, collectedImports)
}
return nil
})
if err := parseSources(ctx, p, info.PkgName, pkg); err != nil {
fmt.Fprintln(console.Errors(ctx), "internal error: ", err)
}
return p
}
func parseSources(ctx context.Context, p *build.Instance, expectedPkg string, pkg CuePackage) error {
for _, f := range pkg.Files {
contents, err := fs.ReadFile(pkg.Snapshot, filepath.Join(pkg.RelPath, f))
if err != nil {
p.Err = errors.Append(p.Err, errors.Promote(err, "ReadFile"))
continue
}
// Filename recorded is "example.com/module/package/file.cue".
importPath := filepath.Join(pkg.ModuleName, pkg.RelPath, f)
parsed, err := parser.ParseFile(importPath, contents, parser.ParseComments)
if err != nil {
p.Err = errors.Append(p.Err, errors.Promote(err, "ParseFile"))
continue
}
if pkgName := parsed.PackageName(); pkgName == "" {
if expectedPkg != "_" {
continue
}
} else if expectedPkg != pkgName {
continue
}
if err := p.AddSyntax(parsed); err != nil {
return err
}
}
return nil
}
func join(dir, base string) string {
if base == "." {
return dir
}
return fmt.Sprintf("%s/%s", dir, base)
}
func absPathForModulePath(collectedImports map[string]*CuePackage, p string) string {
for _, pkg := range collectedImports {
pkgRoot := path.Join(pkg.ModuleName, pkg.RelPath) + "/"
if relPath := strings.TrimPrefix(p, pkgRoot); relPath != p {
return path.Join(pkg.AbsPath, relPath)
}
}
return p
}
func IsStandardImportPath(path string) bool {
i := strings.Index(path, "/")
if i < 0 {
return true
}
elem := path[:i]
// Does it look like a domain name?
return !strings.Contains(elem, ".")
}
func parseTags(vv *CueV) ([]KeyAndPath, error) {
var recorded []KeyAndPath
if err := WalkAttrs(vv.Val, func(v cue.Value, key, value string) error {
switch key {
case InputKeyword:
if !slices.Contains(knownInputs, value) {
return fnerrors.InternalError("%s is a not a supported value of @fn(%s)", value, InputKeyword)
}
recorded = append(recorded, KeyAndPath{Key: value, Target: v.Path()})
case AllocKeyword:
if !slices.Contains(knownAllocs, value) {
return fnerrors.InternalError("%s is a not a supported value of @fn(%s)", value, AllocKeyword)
}
default:
return fnerrors.InternalError("%s is not a supported @fn keyword", key)
}
return nil
}); err != nil {
return nil, err
}
return recorded, nil
}