cue: deprecate Value.Elem and Value.Template
LookupPath now supports both use cases.
Change-Id: I3ba7ab32f24bda79a63331190a8351508c50658b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9348
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cue/types.go b/cue/types.go
index dbe5f4f..bfcc02c 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -1208,22 +1208,15 @@
}
// Elem returns the value of undefined element types of lists and structs.
+//
+// Deprecated: use LookupPath in combination with "AnyString" or "AnyIndex".
func (v Value) Elem() (Value, bool) {
- if v.v == nil {
- return Value{}, false
+ sel := AnyString
+ if v.v.IsList() {
+ sel = AnyIndex
}
- ctx := v.ctx().opCtx
- x := &adt.Vertex{
- Parent: v.v,
- Label: 0,
- }
- v.v.Finalize(ctx)
- v.v.MatchAndInsert(ctx, x)
- if len(x.Conjuncts) == 0 {
- return Value{}, false
- }
- x.Finalize(ctx)
- return makeValue(v.idx, x), true
+ x := v.LookupPath(MakePath(sel))
+ return x, x.Exists()
}
// List creates an iterator over the values of a list or reports an error if
@@ -1663,8 +1656,9 @@
//
// The returned function returns the value that would be unified with field
// given its name.
+//
+// Deprecated: use LookupPath in combination with using optional selectors.
func (v Value) Template() func(label string) Value {
- // TODO: rename to optional.
if v.v == nil {
return nil
}
@@ -1674,17 +1668,8 @@
return nil
}
- parent := v.v
- ctx := v.ctx().opCtx
return func(label string) Value {
- f := ctx.StringLabel(label)
- arc := &adt.Vertex{Parent: parent, Label: f}
- v.v.MatchAndInsert(ctx, arc)
- if len(arc.Conjuncts) == 0 {
- return Value{}
- }
- arc.Finalize(ctx)
- return makeValue(v.idx, arc)
+ return v.LookupPath(MakePath(Str(label).Optional()))
}
}