cuelang.org/go@v0.10.1/cue/build.go (about) 1 // Copyright 2018 The CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cue 16 17 import ( 18 "cuelang.org/go/cue/ast" 19 "cuelang.org/go/cue/ast/astutil" 20 "cuelang.org/go/cue/build" 21 "cuelang.org/go/cue/errors" 22 "cuelang.org/go/internal/core/adt" 23 "cuelang.org/go/internal/core/runtime" 24 ) 25 26 // A Runtime is used for creating CUE Values. 27 // 28 // Any operation that involves two Values or Instances should originate from 29 // the same Runtime. 30 // 31 // The zero value of Runtime works for legacy reasons, but 32 // should not be used. It may panic at some point. 33 // 34 // Deprecated: use [Context]. 35 type Runtime runtime.Runtime 36 37 func (r *Runtime) runtime() *runtime.Runtime { 38 rt := (*runtime.Runtime)(r) 39 rt.Init() 40 return rt 41 } 42 43 type hiddenRuntime = Runtime 44 45 func (r *Runtime) complete(p *build.Instance, v *adt.Vertex) (*Instance, error) { 46 idx := r.runtime() 47 inst := getImportFromBuild(idx, p, v) 48 inst.ImportPath = p.ImportPath 49 if inst.Err != nil { 50 return nil, inst.Err 51 } 52 return inst, nil 53 } 54 55 // Compile compiles the given source into an Instance. The source code may be 56 // provided as a string, byte slice, io.Reader. The name is used as the file 57 // name in position information. The source may import builtin packages. Use 58 // Build to allow importing non-builtin packages. 59 // 60 // Deprecated: use [Context] with methods like [Context.CompileString] or [Context.CompileBytes]. 61 // The use of [Instance] is being phased out. 62 func (r *hiddenRuntime) Compile(filename string, source interface{}) (*Instance, error) { 63 cfg := &runtime.Config{Filename: filename} 64 v, p := r.runtime().Compile(cfg, source) 65 return r.complete(p, v) 66 } 67 68 // CompileFile compiles the given source file into an Instance. The source may 69 // import builtin packages. Use Build to allow importing non-builtin packages. 70 // 71 // Deprecated: use [Context.BuildFile]. The use of [Instance] is being phased out. 72 func (r *hiddenRuntime) CompileFile(file *ast.File) (*Instance, error) { 73 v, p := r.runtime().CompileFile(nil, file) 74 return r.complete(p, v) 75 } 76 77 // CompileExpr compiles the given source expression into an Instance. The source 78 // may import builtin packages. Use Build to allow importing non-builtin 79 // packages. 80 // 81 // Deprecated: use [Context.BuildExpr]. The use of [Instance] is being phased out. 82 func (r *hiddenRuntime) CompileExpr(expr ast.Expr) (*Instance, error) { 83 f, err := astutil.ToFile(expr) 84 if err != nil { 85 return nil, err 86 } 87 runtime := r.runtime() 88 v := (*Context)(runtime).BuildExpr(expr) 89 err = v.Err() 90 inst := &Instance{ 91 index: runtime, 92 root: v.v, 93 inst: &build.Instance{ 94 Files: []*ast.File{f}, 95 }, 96 Err: errors.Promote(err, ""), 97 Incomplete: err != nil, 98 } 99 return inst, err 100 } 101 102 // Parse parses a CUE source value into a CUE Instance. The source code may be 103 // provided as a string, byte slice, or io.Reader. The name is used as the file 104 // name in position information. The source may import builtin packages. 105 // 106 // Deprecated: use [Context.CompileString] or [Context.CompileBytes]. 107 // The use of [Instance] is being phased out. 108 func (r *hiddenRuntime) Parse(name string, source interface{}) (*Instance, error) { 109 return r.Compile(name, source) 110 } 111 112 // Build creates an Instance from the given build.Instance. A returned Instance 113 // may be incomplete, in which case its Err field is set. 114 // 115 // Deprecated: use [Context.BuildInstance]. The use of [Instance] is being phased out. 116 func (r *hiddenRuntime) Build(p *build.Instance) (*Instance, error) { 117 v, _ := r.runtime().Build(nil, p) 118 return r.complete(p, v) 119 } 120 121 // Deprecated: use [Context.BuildInstances]. The use of [Instance] is being phased out. 122 func Build(instances []*build.Instance) []*Instance { 123 if len(instances) == 0 { 124 panic("cue: list of instances must not be empty") 125 } 126 var r Runtime 127 a, _ := r.build(instances) 128 return a 129 } 130 131 func (r *hiddenRuntime) build(instances []*build.Instance) ([]*Instance, error) { 132 index := r.runtime() 133 134 loaded := []*Instance{} 135 136 var errs errors.Error 137 138 for _, p := range instances { 139 v, _ := index.Build(nil, p) 140 i := getImportFromBuild(index, p, v) 141 errs = errors.Append(errs, i.Err) 142 loaded = append(loaded, i) 143 } 144 145 // TODO: insert imports 146 return loaded, errs 147 } 148 149 // FromExpr creates an instance from an expression. 150 // Any references must be resolved beforehand. 151 // 152 // Deprecated: use [Context.BuildExpr]. The use of [Instance] is being phased out. 153 func (r *hiddenRuntime) FromExpr(expr ast.Expr) (*Instance, error) { 154 return r.CompileFile(&ast.File{ 155 Decls: []ast.Decl{&ast.EmbedDecl{Expr: expr}}, 156 }) 157 }