Skip to content

Commit 4f8b741

Browse files
committed
encoding/jsonschema: respect Config.PkgName
This restores the `Config.PkgName` functionality broken by previous updates. Signed-off-by: Roger Peppe <rogpeppe@gmail.com> Change-Id: I135df49c58f7ac88cdcf47563bbd31c8e20c7ab2 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1205904 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
1 parent d5de8d4 commit 4f8b741

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

encoding/jsonschema/decode.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,6 @@ func (d *decoder) addImport(n cue.Value, pkg string) *ast.Ident {
119119
}
120120

121121
func (d *decoder) decode(v cue.Value) *ast.File {
122-
f := &ast.File{}
123-
124-
if pkgName := d.cfg.PkgName; pkgName != "" {
125-
pkg := &ast.Package{Name: ast.NewIdent(pkgName)}
126-
f.Decls = append(f.Decls, pkg)
127-
}
128-
129122
var defsRoot cue.Value
130123
if d.cfg.Root != "" {
131124
defsPath, err := parseRootRef(d.cfg.Root)
@@ -230,20 +223,23 @@ func (d *decoder) decode(v cue.Value) *ast.File {
230223
d.errf(v, "cannot build final syntax: %v", err)
231224
return nil
232225
}
233-
var attrs []ast.Decl
226+
var preamble []ast.Decl
227+
if d.cfg.PkgName != "" {
228+
preamble = append(preamble, &ast.Package{Name: ast.NewIdent(d.cfg.PkgName)})
229+
}
234230
if rootInfo.schemaVersionPresent {
235231
// TODO use cue/literal.String
236232
// TODO is this actually useful information: why is knowing the schema
237233
// version of the input useful?
238-
attrs = append(attrs, &ast.Attribute{
234+
preamble = append(preamble, &ast.Attribute{
239235
Text: fmt.Sprintf("@jsonschema(schema=%q)", rootInfo.schemaVersion),
240236
})
241237
}
242238
if rootInfo.deprecated {
243-
attrs = append(attrs, &ast.Attribute{Text: "@deprecated()"})
239+
preamble = append(preamble, &ast.Attribute{Text: "@deprecated()"})
244240
}
245-
if len(attrs) > 0 {
246-
f.Decls = append(attrs, f.Decls...)
241+
if len(preamble) > 0 {
242+
f.Decls = append(preamble, f.Decls...)
247243
}
248244
return f
249245
}

encoding/jsonschema/testdata/txtar/pkgname.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
// Main schema
2323
//
2424
// Specify who you are and all.
25+
package somepkg
26+
2527
@jsonschema(schema="https://json-schema.org/draft/2019-09/schema")
2628

2729
// A person is a human being.

0 commit comments

Comments
 (0)