cuelang.org/go@v0.13.0/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/build" 19 "cuelang.org/go/cue/errors" 20 "cuelang.org/go/internal/core/adt" 21 "cuelang.org/go/internal/core/runtime" 22 ) 23 24 // A Runtime is used for creating CUE Values. 25 // 26 // Any operation that involves two Values or Instances should originate from 27 // the same Runtime. 28 // 29 // The zero value of Runtime works for legacy reasons, but 30 // should not be used. It may panic at some point. 31 // 32 // Deprecated: use [Context]. 33 type Runtime runtime.Runtime 34 35 func (r *Runtime) runtime() *runtime.Runtime { 36 rt := (*runtime.Runtime)(r) 37 rt.Init() 38 return rt 39 } 40 41 type hiddenRuntime = Runtime 42 43 func (r *Runtime) complete(p *build.Instance, v *adt.Vertex) (*Instance, error) { 44 idx := r.runtime() 45 inst := getImportFromBuild(idx, p, v) 46 inst.ImportPath = p.ImportPath 47 if inst.Err != nil { 48 return nil, inst.Err 49 } 50 return inst, nil 51 } 52 53 // Compile compiles the given source into an Instance. The source code may be 54 // provided as a string, byte slice, io.Reader. The name is used as the file 55 // name in position information. The source may import builtin packages. Use 56 // Build to allow importing non-builtin packages. 57 // 58 // Deprecated: use [Context] with methods like [Context.CompileString] or [Context.CompileBytes]. 59 // The use of [Instance] is being phased out. 60 func (r *hiddenRuntime) Compile(filename string, source interface{}) (*Instance, error) { 61 cfg := &runtime.Config{Filename: filename} 62 v, p := r.runtime().Compile(cfg, source) 63 return r.complete(p, v) 64 } 65 66 // Deprecated: use [Context.BuildInstances]. The use of [Instance] is being phased out. 67 func Build(instances []*build.Instance) []*Instance { 68 if len(instances) == 0 { 69 panic("cue: list of instances must not be empty") 70 } 71 var r Runtime 72 a, _ := r.BuildInstances(instances) 73 return a 74 } 75 76 // Deprecated: use [Context.BuildInstances]. The use of [Instance] is being phased out. 77 func (r *hiddenRuntime) BuildInstances(instances []*build.Instance) ([]*Instance, error) { 78 index := r.runtime() 79 80 loaded := []*Instance{} 81 82 var errs errors.Error 83 84 for _, p := range instances { 85 v, _ := index.Build(nil, p) 86 i := getImportFromBuild(index, p, v) 87 errs = errors.Append(errs, i.Err) 88 loaded = append(loaded, i) 89 } 90 91 // TODO: insert imports 92 return loaded, errs 93 }