github.com/solo-io/cue@v0.4.7/internal/value/value.go (about)

     1  // Copyright 2021 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 value contains functions for converting values to internal types
    16  // and various other Value-related utilities.
    17  package value
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/solo-io/cue/cue"
    23  	"github.com/solo-io/cue/cue/errors"
    24  	"github.com/solo-io/cue/internal/core/adt"
    25  	"github.com/solo-io/cue/internal/core/convert"
    26  	"github.com/solo-io/cue/internal/core/eval"
    27  	"github.com/solo-io/cue/internal/core/runtime"
    28  	"github.com/solo-io/cue/internal/types"
    29  )
    30  
    31  func ConvertToRuntime(c *cue.Context) *cue.Runtime {
    32  	return (*cue.Runtime)(c)
    33  }
    34  
    35  func ConvertToContext(r *cue.Runtime) *cue.Context {
    36  	(*runtime.Runtime)(r).Init()
    37  	return (*cue.Context)(r)
    38  }
    39  
    40  func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) {
    41  	var t types.Value
    42  	v.Core(&t)
    43  	return t.R, t.V
    44  }
    45  
    46  // Make wraps cue.MakeValue.
    47  func Make(ctx *adt.OpContext, v adt.Value) cue.Value {
    48  	return (*cue.Context)(ctx.Impl().(*runtime.Runtime)).Encode(v)
    49  }
    50  
    51  func MakeError(r *runtime.Runtime, err errors.Error) cue.Value {
    52  	b := &adt.Bottom{Err: err}
    53  	node := &adt.Vertex{BaseValue: b}
    54  	node.UpdateStatus(adt.Finalized)
    55  	node.AddConjunct(adt.MakeRootConjunct(nil, b))
    56  	return (*cue.Context)(r).Encode(node)
    57  }
    58  
    59  // UnifyBuiltin returns the given Value unified with the given builtin template.
    60  func UnifyBuiltin(v cue.Value, kind string) cue.Value {
    61  	p := strings.Split(kind, ".")
    62  	pkg, name := p[0], p[1]
    63  	s, _ := runtime.SharedRuntime.LoadImport(pkg)
    64  	if s == nil {
    65  		return v
    66  	}
    67  
    68  	ctx := v.Context()
    69  	a := s.Lookup((*runtime.Runtime)(ctx).Label(name, false))
    70  	if a == nil {
    71  		return v
    72  	}
    73  
    74  	return v.Unify(ctx.Encode(a))
    75  }
    76  
    77  func FromGoValue(r *cue.Context, x interface{}, nilIsTop bool) cue.Value {
    78  	rt := (*runtime.Runtime)(r)
    79  	rt.Init()
    80  	ctx := eval.NewContext(rt, nil)
    81  	v := convert.GoValueToValue(ctx, x, nilIsTop)
    82  	n := adt.ToVertex(v)
    83  	return r.Encode(n)
    84  }
    85  
    86  func FromGoType(r *cue.Context, x interface{}) cue.Value {
    87  	rt := (*runtime.Runtime)(r)
    88  	rt.Init()
    89  	ctx := eval.NewContext(rt, nil)
    90  	expr, err := convert.GoTypeToExpr(ctx, x)
    91  	if err != nil {
    92  		expr = &adt.Bottom{Err: err}
    93  	}
    94  	n := &adt.Vertex{}
    95  	n.AddConjunct(adt.MakeRootConjunct(nil, expr))
    96  	return r.Encode(n)
    97  }