Skip to content

Commit

Permalink
internal/encoding: remove Decoder.ID method
Browse files Browse the repository at this point in the history
This method is not used by anything inside CUE, only
applies to JSON Schema (and that not very well) and
does not seem worth keeping around in general.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Iab491b2faee65cd33a365ce67377ee1e5cfcbb0e
Dispatch-Trailer: {"type":"trybot","CL":1200898,"patchset":2,"ref":"refs/changes/98/1200898/2","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Sep 10, 2024
1 parent 6236d08 commit 15731c3
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions internal/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package encoding
import (
"fmt"
"io"
"net/url"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
Expand Down Expand Up @@ -57,19 +55,13 @@ type Decoder struct {
expr ast.Expr
file *ast.File
filename string // may change on iteration for some formats
id string
index int
err error
}

type interpretFunc func(cue.Value) (file *ast.File, id string, err error)
type interpretFunc func(cue.Value) (file *ast.File, err error)
type rewriteFunc func(*ast.File) (file *ast.File, err error)

// ID returns a canonical identifier for the decoded object or "" if no such
// identifier could be found.
func (i *Decoder) ID() string {
return i.id
}
func (i *Decoder) Filename() string { return i.filename }

// Interpretation returns the current interpretation detected by Detect.
Expand Down Expand Up @@ -110,7 +102,7 @@ func (i *Decoder) doInterpret() {
i.err = err
return
}
i.file, i.id, i.err = i.interpretFunc(v)
i.file, i.err = i.interpretFunc(v)
}
}

Expand Down Expand Up @@ -206,14 +198,14 @@ func NewDecoder(ctx *cue.Context, f *build.File, cfg *Config) *Decoder {
case build.Auto:
openAPI := openAPIFunc(cfg, f)
jsonSchema := jsonSchemaFunc(cfg, f)
i.interpretFunc = func(v cue.Value) (file *ast.File, id string, err error) {
i.interpretFunc = func(v cue.Value) (file *ast.File, err error) {
switch i.interpretation = Detect(v); i.interpretation {
case build.JSONSchema:
return jsonSchema(v)
case build.OpenAPI:
return openAPI(v)
}
return i.file, "", i.err
return i.file, i.err
}
case build.OpenAPI:
i.interpretation = build.OpenAPI
Expand Down Expand Up @@ -291,39 +283,26 @@ func NewDecoder(ctx *cue.Context, f *build.File, cfg *Config) *Decoder {
}

func jsonSchemaFunc(cfg *Config, f *build.File) interpretFunc {
return func(v cue.Value) (file *ast.File, id string, err error) {
id = f.Tags["id"]
if id == "" {
id, _ = v.LookupPath(cue.MakePath(cue.Str("$id"))).String()
}
if id != "" {
u, err := url.Parse(id)
if err != nil {
return nil, "", errors.Wrapf(err, token.NoPos, "invalid id")
}
u.Scheme = ""
id = strings.TrimPrefix(u.String(), "//")
}
return func(v cue.Value) (file *ast.File, err error) {
cfg := &jsonschema.Config{
ID: id,
PkgName: cfg.PkgName,

Strict: cfg.Strict,
}
file, err = jsonschema.Extract(v, cfg)
// TODO: simplify currently erases file line info. Reintroduce after fix.
// file, err = simplify(file, err)
return file, id, err
return file, err
}
}

func openAPIFunc(c *Config, f *build.File) interpretFunc {
cfg := &openapi.Config{PkgName: c.PkgName}
return func(v cue.Value) (file *ast.File, id string, err error) {
return func(v cue.Value) (file *ast.File, err error) {
file, err = openapi.Extract(v, cfg)
// TODO: simplify currently erases file line info. Reintroduce after fix.
// file, err = simplify(file, err)
return file, "", err
return file, err
}
}

Expand Down

0 comments on commit 15731c3

Please sign in to comment.