...

Source file src/cuelang.org/go/cue/build.go

Documentation: cuelang.org/go/cue

     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 Parse or ParseBytes. The use of Instance is being phased out.
    61  func (r *hiddenRuntime) Compile(filename string, source interface{}) (*Instance, error) {
    62  	cfg := &runtime.Config{Filename: filename}
    63  	v, p := r.runtime().Compile(cfg, source)
    64  	return r.complete(p, v)
    65  }
    66  
    67  // CompileFile compiles the given source file into an Instance. The source may
    68  // import builtin packages. Use Build to allow importing non-builtin packages.
    69  //
    70  // Deprecated: use BuildFile. The use of Instance is being phased out.
    71  func (r *hiddenRuntime) CompileFile(file *ast.File) (*Instance, error) {
    72  	v, p := r.runtime().CompileFile(nil, file)
    73  	return r.complete(p, v)
    74  }
    75  
    76  // CompileExpr compiles the given source expression into an Instance. The source
    77  // may import builtin packages. Use Build to allow importing non-builtin
    78  // packages.
    79  //
    80  // Deprecated: use BuildExpr. The use of Instance is being phased out.
    81  func (r *hiddenRuntime) CompileExpr(expr ast.Expr) (*Instance, error) {
    82  	f, err := astutil.ToFile(expr)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	runtime := r.runtime()
    87  	v := (*Context)(runtime).BuildExpr(expr)
    88  	err = v.Err()
    89  	inst := &Instance{
    90  		index: runtime,
    91  		root:  v.v,
    92  		inst: &build.Instance{
    93  			Files: []*ast.File{f},
    94  		},
    95  		Err:        errors.Promote(err, ""),
    96  		Incomplete: err != nil,
    97  	}
    98  	return inst, err
    99  }
   100  
   101  // Parse parses a CUE source value into a CUE Instance. The source code may be
   102  // provided as a string, byte slice, or io.Reader. The name is used as the file
   103  // name in position information. The source may import builtin packages.
   104  //
   105  // Deprecated: use [Context.CompileString] or [Context.CompileBytes].
   106  // The use of [Instance] is being phased out.
   107  func (r *hiddenRuntime) Parse(name string, source interface{}) (*Instance, error) {
   108  	return r.Compile(name, source)
   109  }
   110  
   111  // Build creates an Instance from the given build.Instance. A returned Instance
   112  // may be incomplete, in which case its Err field is set.
   113  //
   114  // Deprecated: use [Context.BuildInstance]. The use of [Instance] is being phased out.
   115  func (r *hiddenRuntime) Build(p *build.Instance) (*Instance, error) {
   116  	v, _ := r.runtime().Build(nil, p)
   117  	return r.complete(p, v)
   118  }
   119  
   120  // Deprecated: use [Context.BuildInstances]. The use of [Instance] is being phased out.
   121  func Build(instances []*build.Instance) []*Instance {
   122  	if len(instances) == 0 {
   123  		panic("cue: list of instances must not be empty")
   124  	}
   125  	var r Runtime
   126  	a, _ := r.build(instances)
   127  	return a
   128  }
   129  
   130  func (r *hiddenRuntime) build(instances []*build.Instance) ([]*Instance, error) {
   131  	index := r.runtime()
   132  
   133  	loaded := []*Instance{}
   134  
   135  	var errs errors.Error
   136  
   137  	for _, p := range instances {
   138  		v, _ := index.Build(nil, p)
   139  		i := getImportFromBuild(index, p, v)
   140  		errs = errors.Append(errs, i.Err)
   141  		loaded = append(loaded, i)
   142  	}
   143  
   144  	// TODO: insert imports
   145  	return loaded, errs
   146  }
   147  
   148  // FromExpr creates an instance from an expression.
   149  // Any references must be resolved beforehand.
   150  //
   151  // Deprecated: use CompileExpr
   152  func (r *hiddenRuntime) FromExpr(expr ast.Expr) (*Instance, error) {
   153  	return r.CompileFile(&ast.File{
   154  		Decls: []ast.Decl{&ast.EmbedDecl{Expr: expr}},
   155  	})
   156  }
   157  

View as plain text