compiler: simplify InstanceMerge

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-04-08 17:22:25 -07:00
parent 10e676923c
commit 8b0eea6e7f

View File

@ -29,7 +29,7 @@ func Wrap(v cue.Value, inst *cue.Instance) *Value {
return DefaultCompiler.Wrap(v, inst) return DefaultCompiler.Wrap(v, inst)
} }
func InstanceMerge(src ...interface{}) (*Value, error) { func InstanceMerge(src ...*Value) (*Value, error) {
return DefaultCompiler.InstanceMerge(src...) return DefaultCompiler.InstanceMerge(src...)
} }
@ -102,7 +102,7 @@ func (c *Compiler) Compile(name string, src interface{}) (*Value, error) {
// FIXME: AVOID THIS AT ALL COST // FIXME: AVOID THIS AT ALL COST
// Special case: we must return an instance with the same // Special case: we must return an instance with the same
// contents as v, for the purposes of cueflow. // contents as v, for the purposes of cueflow.
func (c *Compiler) InstanceMerge(src ...interface{}) (*Value, error) { func (c *Compiler) InstanceMerge(src ...*Value) (*Value, error) {
var ( var (
v = c.NewValue() v = c.NewValue()
inst = v.CueInst() inst = v.CueInst()
@ -113,18 +113,12 @@ func (c *Compiler) InstanceMerge(src ...interface{}) (*Value, error) {
defer c.unlock() defer c.unlock()
for _, s := range src { for _, s := range src {
// If calling Fill() with a Value, we want to use the underlying inst, err = inst.Fill(s.val)
// cue.Value to fill.
if val, ok := s.(*Value); ok {
inst, err = inst.Fill(val.val)
if err != nil {
return nil, fmt.Errorf("merge failed: %w", err)
}
} else {
inst, err = inst.Fill(s)
if err != nil { if err != nil {
return nil, fmt.Errorf("merge failed: %w", err) return nil, fmt.Errorf("merge failed: %w", err)
} }
if err := inst.Value().Err(); err != nil {
return nil, err
} }
} }