encoding/jsonschema: require explicit package name

It is not what it should be by default or that it
is required at all, so don't specify by default.

Also add file documentation.

Change-Id: Ic7d508853f6743324b70da3d8141b98f0682d232
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5183
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/jsonschema/decode.go b/encoding/jsonschema/decode.go
index 56fd6cc..2bac410 100644
--- a/encoding/jsonschema/decode.go
+++ b/encoding/jsonschema/decode.go
@@ -20,8 +20,6 @@
 
 import (
 	"fmt"
-	"path"
-	"path/filepath"
 	"sort"
 	"strings"
 
@@ -60,17 +58,14 @@
 
 	var a []ast.Decl
 
-	filename := d.cfg.ID
-	name := filepath.ToSlash(filename)
-	if state.id != "" {
-		name = strings.Trim(name, "#")
-	}
-	pkgName := path.Base(name)
-	pkgName = pkgName[:len(pkgName)-len(path.Ext(pkgName))]
-	pkg := &ast.Package{Name: ast.NewIdent(pkgName)}
-	state.doc(pkg)
+	if pkgName := d.cfg.PkgName; pkgName != "" {
+		pkg := &ast.Package{Name: ast.NewIdent(pkgName)}
+		state.doc(pkg)
 
-	a = append(a, pkg)
+		a = append(a, pkg)
+	} else if doc := state.comment(); doc != nil {
+		a = append(a, doc)
+	}
 
 	var imports []string
 	for k := range d.imports {
@@ -263,7 +258,7 @@
 	return e
 }
 
-func (s *state) doc(n ast.Node) {
+func (s *state) comment() *ast.CommentGroup {
 	// Create documentation.
 	doc := strings.TrimSpace(s.title)
 	if s.description != "" {
@@ -273,13 +268,18 @@
 		doc += s.description
 		doc = strings.TrimSpace(doc)
 	}
-	if doc != "" {
-		ast.SetComments(n, []*ast.CommentGroup{
-			internal.NewComment(true, doc),
-		})
-	}
-
 	// TODO: add examples as well?
+	if doc == "" {
+		return nil
+	}
+	return internal.NewComment(true, doc)
+}
+
+func (s *state) doc(n ast.Node) {
+	doc := s.comment()
+	if doc != nil {
+		ast.SetComments(n, []*ast.CommentGroup{doc})
+	}
 }
 
 func (s *state) add(e ast.Expr) {
diff --git a/encoding/jsonschema/jsonschema.go b/encoding/jsonschema/jsonschema.go
index 8c496e6..f685c46 100644
--- a/encoding/jsonschema/jsonschema.go
+++ b/encoding/jsonschema/jsonschema.go
@@ -53,6 +53,8 @@
 
 // A Config configures a JSON Schema encoding or decoding.
 type Config struct {
+	PkgName string
+
 	ID string // URL of the original source, corresponding to the $id field.
 
 	// TODO: configurability to make it compatible with OpenAPI, such as
diff --git a/encoding/jsonschema/testdata/basic.txtar b/encoding/jsonschema/testdata/basic.txtar
index 0c1b255..fe55c38 100644
--- a/encoding/jsonschema/testdata/basic.txtar
+++ b/encoding/jsonschema/testdata/basic.txtar
@@ -35,7 +35,6 @@
 // Main schema
 // 
 // Specify who you are and all.
-package basic
 
 Schema :: _ @jsonschema(schema="http://json-schema.org/draft-07/schema#")
 Schema :: {
diff --git a/encoding/jsonschema/testdata/def.txtar b/encoding/jsonschema/testdata/def.txtar
index d5d92e9..e8836fa 100644
--- a/encoding/jsonschema/testdata/def.txtar
+++ b/encoding/jsonschema/testdata/def.txtar
@@ -39,8 +39,6 @@
 }
 
 -- out.cue --
-package def
-
 Schema :: _ @jsonschema(schema="http://json-schema.org/draft-07/schema#",id="http://cuelang.org/go/encoding/openapi/testdata/order.json")
 Schema :: {
 	person?:           def.person
diff --git a/encoding/jsonschema/testdata/list.txtar b/encoding/jsonschema/testdata/list.txtar
index d3046dd..e76ca8e 100644
--- a/encoding/jsonschema/testdata/list.txtar
+++ b/encoding/jsonschema/testdata/list.txtar
@@ -24,8 +24,6 @@
 additionalProperties: false
 
 -- out.cue --
-package list
-
 import "list"
 
 Schema :: {
diff --git a/encoding/jsonschema/testdata/num.txtar b/encoding/jsonschema/testdata/num.txtar
index 5652628..77236a1 100644
--- a/encoding/jsonschema/testdata/num.txtar
+++ b/encoding/jsonschema/testdata/num.txtar
@@ -24,8 +24,6 @@
 }
 
 -- out.cue --
-package num
-
 import "math"
 
 Schema :: {
diff --git a/encoding/jsonschema/testdata/object.txtar b/encoding/jsonschema/testdata/object.txtar
index 59bbbe3..7fb15c3 100644
--- a/encoding/jsonschema/testdata/object.txtar
+++ b/encoding/jsonschema/testdata/object.txtar
@@ -60,7 +60,6 @@
 
 -- out.cue --
 // Main schema
-package object
 
 import "struct"
 
diff --git a/encoding/jsonschema/testdata/type.txtar b/encoding/jsonschema/testdata/type.txtar
index 2c13d3f..a1144d1 100644
--- a/encoding/jsonschema/testdata/type.txtar
+++ b/encoding/jsonschema/testdata/type.txtar
@@ -28,7 +28,6 @@
 
 -- out.cue --
 // Main schema
-package type
 
 Schema :: {
 	// an integer or string.