Skip to content

Commit

Permalink
encoding/jsonschema: respect Config.PkgName
Browse files Browse the repository at this point in the history
This restores the `Config.PkgName` functionality broken by previous
updates.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I135df49c58f7ac88cdcf47563bbd31c8e20c7ab2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205904
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Dec 17, 2024
1 parent d5de8d4 commit 4f8b741
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 8 additions & 12 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ func (d *decoder) addImport(n cue.Value, pkg string) *ast.Ident {
}

func (d *decoder) decode(v cue.Value) *ast.File {
f := &ast.File{}

if pkgName := d.cfg.PkgName; pkgName != "" {
pkg := &ast.Package{Name: ast.NewIdent(pkgName)}
f.Decls = append(f.Decls, pkg)
}

var defsRoot cue.Value
if d.cfg.Root != "" {
defsPath, err := parseRootRef(d.cfg.Root)
Expand Down Expand Up @@ -230,20 +223,23 @@ func (d *decoder) decode(v cue.Value) *ast.File {
d.errf(v, "cannot build final syntax: %v", err)
return nil
}
var attrs []ast.Decl
var preamble []ast.Decl
if d.cfg.PkgName != "" {
preamble = append(preamble, &ast.Package{Name: ast.NewIdent(d.cfg.PkgName)})
}
if rootInfo.schemaVersionPresent {
// TODO use cue/literal.String
// TODO is this actually useful information: why is knowing the schema
// version of the input useful?
attrs = append(attrs, &ast.Attribute{
preamble = append(preamble, &ast.Attribute{
Text: fmt.Sprintf("@jsonschema(schema=%q)", rootInfo.schemaVersion),
})
}
if rootInfo.deprecated {
attrs = append(attrs, &ast.Attribute{Text: "@deprecated()"})
preamble = append(preamble, &ast.Attribute{Text: "@deprecated()"})
}
if len(attrs) > 0 {
f.Decls = append(attrs, f.Decls...)
if len(preamble) > 0 {
f.Decls = append(preamble, f.Decls...)
}
return f
}
Expand Down
2 changes: 2 additions & 0 deletions encoding/jsonschema/testdata/txtar/pkgname.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// Main schema
//
// Specify who you are and all.
package somepkg

@jsonschema(schema="https://json-schema.org/draft/2019-09/schema")

// A person is a human being.
Expand Down

0 comments on commit 4f8b741

Please sign in to comment.