Skip to content

Commit 4ae8f73

Browse files
authored
migrate json schema generation (#4270)
Signed-off-by: Alex Goodman <[email protected]>
1 parent 18e789c commit 4ae8f73

35 files changed

+48
-40
lines changed

Taskfile.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ tasks:
502502
generate-json-schema:
503503
desc: Generate a new JSON schema
504504
cmds:
505-
- "cd syft/internal && go generate . && cd jsonschema && go run . && go fmt ../..."
505+
- "cd ./internal && go generate . && cd ./jsonschema && go run . && go fmt ../..."
506506

507507
generate-license-list:
508508
desc: Generate an updated license processing code off of the latest available SPDX license list
File renamed without changes.

internal/jsonschema/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Please see [schema/json/README.md](../../schema/json/README.md) for more information on the JSON schema files in this directory.
File renamed without changes.
File renamed without changes.

syft/internal/jsonschema/main.go renamed to internal/jsonschema/main.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"github.com/invopop/jsonschema"
1616

1717
"github.com/anchore/syft/internal"
18+
"github.com/anchore/syft/internal/packagemetadata"
1819
syftJsonModel "github.com/anchore/syft/syft/format/syftjson/model"
19-
"github.com/anchore/syft/syft/internal/packagemetadata"
2020
)
2121

2222
/*
@@ -26,6 +26,17 @@ are not captured (empty interfaces). This means that pkg.Package.Metadata is not
2626
can be extended to include specific package metadata struct shapes in the future.
2727
*/
2828

29+
var repoRoot string
30+
31+
func init() {
32+
var err error
33+
repoRoot, err = packagemetadata.RepoRoot()
34+
if err != nil {
35+
fmt.Println("unable to determine repo root")
36+
os.Exit(1)
37+
}
38+
}
39+
2940
func main() {
3041
write(encode(build()))
3142
}
@@ -60,7 +71,7 @@ func assembleTypeContainer(items []any) (any, map[string]string) {
6071
}
6172

6273
if len(typesMissingNames) > 0 {
63-
fmt.Println("the following types are missing JSON names (manually curated in ./syft/internal/packagemetadata/names.go):")
74+
fmt.Println("the following types are missing JSON names (manually curated in ./internal/packagemetadata/names.go):")
6475
for _, t := range typesMissingNames {
6576
fmt.Println(" - ", t.Name())
6677
}
@@ -86,25 +97,30 @@ func build() *jsonschema.Schema {
8697
// note: AddGoComments parses from the module root and creates keys like "syft/pkg.TypeName",
8798
// but the reflector expects fully qualified paths like "github.com/anchore/syft/syft/pkg.TypeName".
8899
// We fix up the keys after extraction to match the expected format.
89-
if err := reflector.AddGoComments("github.com/anchore/syft", "../../.."); err != nil {
100+
if err := reflector.AddGoComments("github.com/anchore/syft", repoRoot); err != nil {
90101
fmt.Fprintf(os.Stderr, "warning: failed to extract Go comments: %v\n", err)
91102
} else {
92103
// fix up comment map keys to use fully qualified import paths
104+
// note: AddGoComments includes the absolute repo path WITHOUT the leading slash
105+
repoRootNoSlash := strings.TrimPrefix(repoRoot, "/")
93106
fixedMap := make(map[string]string)
94107
for k, v := range reflector.CommentMap {
95108
newKey := k
96109
if !strings.HasPrefix(k, "github.com/") {
110+
// key doesn't have module prefix, add it
97111
newKey = "github.com/anchore/syft/" + k
112+
} else if strings.Contains(k, repoRootNoSlash) {
113+
// key has the absolute repo path embedded, strip it
114+
// format: github.com/anchore/syft/Users/wagoodman/code/syft-manual/syft/pkg.Type
115+
// should be: github.com/anchore/syft/syft/pkg.Type
116+
newKey = strings.Replace(k, repoRootNoSlash+"/", "", 1)
98117
}
99118
fixedMap[newKey] = v
100119
}
101120
reflector.CommentMap = fixedMap
102121

103122
// copy field comments for type aliases (e.g., type RpmArchive RpmDBEntry)
104-
repoRoot, err := packagemetadata.RepoRoot()
105-
if err == nil {
106-
copyAliasFieldComments(reflector.CommentMap, repoRoot)
107-
}
123+
copyAliasFieldComments(reflector.CommentMap, repoRoot)
108124
}
109125

110126
pkgMetadataContainer, pkgMetadataMapping := assembleTypeContainer(packagemetadata.AllTypes())
@@ -178,11 +194,6 @@ func encode(schema *jsonschema.Schema) []byte {
178194
}
179195

180196
func write(schema []byte) {
181-
repoRoot, err := packagemetadata.RepoRoot()
182-
if err != nil {
183-
fmt.Println("unable to determine repo root")
184-
os.Exit(1)
185-
}
186197
schemaPath := filepath.Join(repoRoot, "schema", "json", fmt.Sprintf("schema-%s.json", internal.JSONSchemaVersion))
187198
latestSchemaPath := filepath.Join(repoRoot, "schema", "json", "schema-latest.json")
188199

syft/internal/packagemetadata/generate/main.go renamed to internal/packagemetadata/generate/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"os"
66
"strings"
77

8+
"github.com/anchore/syft/internal/packagemetadata"
89
"github.com/dave/jennifer/jen"
9-
10-
"github.com/anchore/syft/syft/internal/packagemetadata"
1110
)
1211

1312
// This program is invoked from syft/internal and generates packagemetadata/generated.go
@@ -31,7 +30,7 @@ func main() {
3130
fmt.Printf("updating package metadata type list with %+v types\n", len(typeNames))
3231

3332
f := jen.NewFile("packagemetadata")
34-
f.HeaderComment("DO NOT EDIT: generated by syft/internal/packagemetadata/generate/main.go")
33+
f.HeaderComment("DO NOT EDIT: generated by internal/packagemetadata/generate/main.go")
3534
f.ImportName(pkgImport, "pkg")
3635
f.Comment("AllTypes returns a list of all pkg metadata types that syft supports (that are represented in the pkg.Package.Metadata field).")
3736

0 commit comments

Comments
 (0)