Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: hide deprecated methods from docs
Browse files Browse the repository at this point in the history
Change-Id: I4e7cdc00b54a88bb17a1c1e77441224257e09e54
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9426
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 21, 2021
1 parent 618c10c commit 4937cb9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 60 deletions.
34 changes: 18 additions & 16 deletions cue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ import (
// Deprecated: use Context.
type Runtime runtime.Runtime

func (r *Runtime) index() *runtime.Runtime {
func (r *Runtime) runtime() *runtime.Runtime {
rt := (*runtime.Runtime)(r)
rt.Init()
return rt
}

type hiddenRuntime = Runtime

func (r *Runtime) complete(p *build.Instance, v *adt.Vertex) (*Instance, error) {
idx := r.index()
idx := r.runtime()
inst := getImportFromBuild(idx, p, v)
inst.ImportPath = p.ImportPath
if inst.Err != nil {
Expand All @@ -55,18 +57,18 @@ func (r *Runtime) complete(p *build.Instance, v *adt.Vertex) (*Instance, error)
// Build to allow importing non-builtin packages.
//
// Deprecated: use Parse or ParseBytes. The use of Instance is being phased out.
func (r *Runtime) Compile(filename string, source interface{}) (*Instance, error) {
func (r *hiddenRuntime) Compile(filename string, source interface{}) (*Instance, error) {
cfg := &runtime.Config{Filename: filename}
v, p := r.index().Compile(cfg, source)
v, p := r.runtime().Compile(cfg, source)
return r.complete(p, v)
}

// CompileFile compiles the given source file into an Instance. The source may
// import builtin packages. Use Build to allow importing non-builtin packages.
//
// Deprecated: use BuildFile. The use of Instance is being phased out.
func (r *Runtime) CompileFile(file *ast.File) (*Instance, error) {
v, p := r.index().CompileFile(nil, file)
func (r *hiddenRuntime) CompileFile(file *ast.File) (*Instance, error) {
v, p := r.runtime().CompileFile(nil, file)
return r.complete(p, v)
}

Expand All @@ -75,8 +77,8 @@ func (r *Runtime) CompileFile(file *ast.File) (*Instance, error) {
// packages.
//
// Deprecated: use BuildExpr. The use of Instance is being phased out.
func (r *Runtime) CompileExpr(expr ast.Expr) (*Instance, error) {
v, p, err := r.index().CompileExpr(nil, expr)
func (r *hiddenRuntime) CompileExpr(expr ast.Expr) (*Instance, error) {
v, p, err := r.runtime().CompileExpr(nil, expr)
if err != nil {
return nil, err
}
Expand All @@ -89,17 +91,17 @@ func (r *Runtime) CompileExpr(expr ast.Expr) (*Instance, error) {
//
// Deprecated: use ParseString or ParseBytes. The use of Instance is being
// phased out.
func (r *Runtime) Parse(name string, source interface{}) (*Instance, error) {
func (r *hiddenRuntime) Parse(name string, source interface{}) (*Instance, error) {
return r.Compile(name, source)
}

// Build creates an Instance from the given build.Instance. A returned Instance
// may be incomplete, in which case its Err field is set.
//
// Deprecated: use Runtime.BuildInstance. The use of Instance is being phased
// Deprecated: use Context.BuildInstance. The use of Instance is being phased
// out.
func (r *Runtime) Build(p *build.Instance) (*Instance, error) {
v, _ := r.index().Build(nil, p)
func (r *hiddenRuntime) Build(p *build.Instance) (*Instance, error) {
v, _ := r.runtime().Build(nil, p)
return r.complete(p, v)
}

Expand All @@ -109,7 +111,7 @@ func (r *Runtime) Build(p *build.Instance) (*Instance, error) {
// Example:
// inst := cue.Build(load.Instances(args))
//
// Deprecated: use Runtime.BuildInstances. The use of Instance is being phased
// Deprecated: use Context.BuildInstances. The use of Instance is being phased
// out.
func Build(instances []*build.Instance) []*Instance {
if len(instances) == 0 {
Expand All @@ -120,8 +122,8 @@ func Build(instances []*build.Instance) []*Instance {
return a
}

func (r *Runtime) build(instances []*build.Instance) ([]*Instance, error) {
index := r.index()
func (r *hiddenRuntime) build(instances []*build.Instance) ([]*Instance, error) {
index := r.runtime()

loaded := []*Instance{}

Expand All @@ -142,7 +144,7 @@ func (r *Runtime) build(instances []*build.Instance) ([]*Instance, error) {
// Any references must be resolved beforehand.
//
// Deprecated: use CompileExpr
func (r *Runtime) FromExpr(expr ast.Expr) (*Instance, error) {
func (r *hiddenRuntime) FromExpr(expr ast.Expr) (*Instance, error) {
return r.CompileFile(&ast.File{
Decls: []ast.Decl{&ast.EmbedDecl{Expr: expr}},
})
Expand Down
20 changes: 10 additions & 10 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ import (
// to create a new Context.
type Context runtime.Runtime

func (c *Context) index() *runtime.Runtime {
func (c *Context) runtime() *runtime.Runtime {
rt := (*runtime.Runtime)(c)
return rt
}

func (c *Context) ctx() *adt.OpContext {
return newContext(c.index())
return newContext(c.runtime())
}

// Context reports the Context with which this value was created.
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *Context) parseOptions(options []BuildOption) (cfg runtime.Config) {
// error occurred.
func (c *Context) BuildInstance(i *build.Instance, options ...BuildOption) Value {
cfg := c.parseOptions(options)
v, err := c.index().Build(&cfg, i)
v, err := c.runtime().Build(&cfg, i)
if err != nil {
return c.makeError(err)
}
Expand All @@ -113,7 +113,7 @@ func (c *Context) BuildInstances(instances []*build.Instance) ([]Value, error) {
var errs errors.Error
var a []Value
for _, b := range instances {
v, err := c.index().Build(nil, b)
v, err := c.runtime().Build(nil, b)
if err != nil {
errs = errors.Append(errs, err)
}
Expand All @@ -128,7 +128,7 @@ func (c *Context) BuildInstances(instances []*build.Instance) ([]Value, error) {
// error occurred.
func (c *Context) BuildFile(f *ast.File, options ...BuildOption) Value {
cfg := c.parseOptions(options)
return c.compile(c.index().CompileFile(&cfg, f))
return c.compile(c.runtime().CompileFile(&cfg, f))
}

func (c *Context) compile(v *adt.Vertex, p *build.Instance) Value {
Expand All @@ -144,7 +144,7 @@ func (c *Context) compile(v *adt.Vertex, p *build.Instance) Value {
// error occurred.
func (c *Context) BuildExpr(x ast.Expr, options ...BuildOption) Value {
cfg := c.parseOptions(options)
v, p, err := c.index().CompileExpr(&cfg, x)
v, p, err := c.runtime().CompileExpr(&cfg, x)
if err != nil {
return c.makeError(p.Err)
}
Expand All @@ -157,7 +157,7 @@ func (c *Context) BuildExpr(x ast.Expr, options ...BuildOption) Value {
// error occurred.
func (c *Context) CompileString(src string, options ...BuildOption) Value {
cfg := c.parseOptions(options)
return c.compile(c.index().Compile(&cfg, src))
return c.compile(c.runtime().Compile(&cfg, src))
}

// ParseString parses and build a Value from the given source bytes.
Expand All @@ -166,7 +166,7 @@ func (c *Context) CompileString(src string, options ...BuildOption) Value {
// error occurred.
func (c *Context) CompileBytes(b []byte, options ...BuildOption) Value {
cfg := c.parseOptions(options)
return c.compile(c.index().Compile(&cfg, b))
return c.compile(c.runtime().Compile(&cfg, b))
}

// TODO: fs.FS or custom wrapper?
Expand All @@ -183,7 +183,7 @@ func (c *Context) CompileBytes(b []byte, options ...BuildOption) Value {
// }

func (c *Context) make(v *adt.Vertex) Value {
return newValueRoot(c.index(), newContext(c.index()), v)
return newValueRoot(c.runtime(), newContext(c.runtime()), v)
}

// An EncodeOption defines options for the various encoding-related methods of
Expand Down Expand Up @@ -214,7 +214,7 @@ func NilIsAny(isAny bool) EncodeOption {
func (c *Context) Encode(x interface{}, option ...EncodeOption) Value {
switch v := x.(type) {
case adt.Value:
return newValueRoot(c.index(), c.ctx(), v)
return newValueRoot(c.runtime(), c.ctx(), v)
}
var options encodeOptions
options.process(option)
Expand Down
24 changes: 17 additions & 7 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Instance struct {
// complete bool // for cycle detection
}

type hiddenInstance = Instance

func addInst(x *runtime.Runtime, p *Instance) *Instance {
if p.inst == nil {
p.inst = &build.Instance{
Expand Down Expand Up @@ -260,7 +262,7 @@ func (inst *Instance) ID() string {
// Doc returns the package comments for this instance.
//
// Deprecated: use inst.Value().Doc()
func (inst *Instance) Doc() []*ast.CommentGroup {
func (inst *hiddenInstance) Doc() []*ast.CommentGroup {
return inst.Value().Doc()
}

Expand All @@ -276,7 +278,7 @@ func (inst *Instance) Value() Value {
// Eval evaluates an expression within an existing instance.
//
// Expressions may refer to builtin packages if they can be uniquely identified.
func (inst *Instance) Eval(expr ast.Expr) Value {
func (inst *hiddenInstance) Eval(expr ast.Expr) Value {
ctx := newContext(inst.index)
v := inst.root
v.Finalize(ctx)
Expand Down Expand Up @@ -311,7 +313,9 @@ func Merge(inst ...*Instance) *Instance {
// Build creates a new instance from the build instances, allowing unbound
// identifier to bind to the top-level field in inst. The top-level fields in
// inst take precedence over predeclared identifier and builtin functions.
func (inst *Instance) Build(p *build.Instance) *Instance {
//
// Deprecated: use Context.Build
func (inst *hiddenInstance) Build(p *build.Instance) *Instance {
p.Complete()

idx := inst.index
Expand Down Expand Up @@ -350,14 +354,18 @@ func (inst *Instance) value() Value {
// exist. The Err method reports if any error occurred during evaluation. The
// empty path returns the top-level configuration struct. Use LookupDef for definitions or LookupField for
// any kind of field.
func (inst *Instance) Lookup(path ...string) Value {
//
// Deprecated: use Value.LookupPath
func (inst *hiddenInstance) Lookup(path ...string) Value {
return inst.value().Lookup(path...)
}

// LookupDef reports the definition with the given name within struct v. The
// Exists method of the returned value will report false if the definition did
// not exist. The Err method reports if any error occurred during evaluation.
func (inst *Instance) LookupDef(path string) Value {
//
// Deprecated: use Value.LookupPath
func (inst *hiddenInstance) LookupDef(path string) Value {
return inst.value().LookupDef(path)
}

Expand All @@ -368,7 +376,9 @@ func (inst *Instance) LookupDef(path string) Value {
//
// Deprecated: this API does not work with new-style definitions. Use
// FieldByName defined on inst.Value().
func (inst *Instance) LookupField(path ...string) (f FieldInfo, err error) {
//
// Deprecated: use Value.LookupPath
func (inst *hiddenInstance) LookupField(path ...string) (f FieldInfo, err error) {
v := inst.value()
for _, k := range path {
s, err := v.Struct()
Expand Down Expand Up @@ -396,7 +406,7 @@ func (inst *Instance) LookupField(path ...string) (f FieldInfo, err error) {
// Runtime.
//
// Deprecated: use Value.FillPath()
func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
func (inst *hiddenInstance) Fill(x interface{}, path ...string) (*Instance, error) {
v := inst.Value().Fill(x, path...)

inst = addInst(inst.index, &Instance{
Expand Down
4 changes: 2 additions & 2 deletions cue/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
return p
}
// TODO: support exporting instance
file, _ := export.Def(r.index(), i.inst.ID(), i.root)
file, _ := export.Def(r.runtime(), i.inst.ID(), i.root)
imports := []string{}
file.VisitImports(func(i *ast.ImportDecl) {
for _, spec := range i.Specs {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
p := len(staged) - 1

for _, imp := range imports {
i := getImportFromPath(r.index(), imp)
i := getImportFromPath(r.runtime(), imp)
if i == nil || !strings.Contains(imp, ".") {
continue // a builtin package.
}
Expand Down
Loading

0 comments on commit 4937cb9

Please sign in to comment.