migrate json schema generation (#4270)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2025-10-10 10:16:28 -04:00 committed by GitHub
parent 18e789c4fd
commit 4ae8f73583
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
35 changed files with 48 additions and 40 deletions

View File

@ -502,7 +502,7 @@ tasks:
generate-json-schema:
desc: Generate a new JSON schema
cmds:
- "cd syft/internal && go generate . && cd jsonschema && go run . && go fmt ../..."
- "cd ./internal && go generate . && cd ./jsonschema && go run . && go fmt ../..."
generate-license-list:
desc: Generate an updated license processing code off of the latest available SPDX license list

View File

@ -0,0 +1 @@
Please see [schema/json/README.md](../../schema/json/README.md) for more information on the JSON schema files in this directory.

View File

@ -15,8 +15,8 @@ import (
"github.com/invopop/jsonschema"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/packagemetadata"
syftJsonModel "github.com/anchore/syft/syft/format/syftjson/model"
"github.com/anchore/syft/syft/internal/packagemetadata"
)
/*
@ -26,6 +26,17 @@ are not captured (empty interfaces). This means that pkg.Package.Metadata is not
can be extended to include specific package metadata struct shapes in the future.
*/
var repoRoot string
func init() {
var err error
repoRoot, err = packagemetadata.RepoRoot()
if err != nil {
fmt.Println("unable to determine repo root")
os.Exit(1)
}
}
func main() {
write(encode(build()))
}
@ -60,7 +71,7 @@ func assembleTypeContainer(items []any) (any, map[string]string) {
}
if len(typesMissingNames) > 0 {
fmt.Println("the following types are missing JSON names (manually curated in ./syft/internal/packagemetadata/names.go):")
fmt.Println("the following types are missing JSON names (manually curated in ./internal/packagemetadata/names.go):")
for _, t := range typesMissingNames {
fmt.Println(" - ", t.Name())
}
@ -86,26 +97,31 @@ func build() *jsonschema.Schema {
// note: AddGoComments parses from the module root and creates keys like "syft/pkg.TypeName",
// but the reflector expects fully qualified paths like "github.com/anchore/syft/syft/pkg.TypeName".
// We fix up the keys after extraction to match the expected format.
if err := reflector.AddGoComments("github.com/anchore/syft", "../../.."); err != nil {
if err := reflector.AddGoComments("github.com/anchore/syft", repoRoot); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to extract Go comments: %v\n", err)
} else {
// fix up comment map keys to use fully qualified import paths
// note: AddGoComments includes the absolute repo path WITHOUT the leading slash
repoRootNoSlash := strings.TrimPrefix(repoRoot, "/")
fixedMap := make(map[string]string)
for k, v := range reflector.CommentMap {
newKey := k
if !strings.HasPrefix(k, "github.com/") {
// key doesn't have module prefix, add it
newKey = "github.com/anchore/syft/" + k
} else if strings.Contains(k, repoRootNoSlash) {
// key has the absolute repo path embedded, strip it
// format: github.com/anchore/syft/Users/wagoodman/code/syft-manual/syft/pkg.Type
// should be: github.com/anchore/syft/syft/pkg.Type
newKey = strings.Replace(k, repoRootNoSlash+"/", "", 1)
}
fixedMap[newKey] = v
}
reflector.CommentMap = fixedMap
// copy field comments for type aliases (e.g., type RpmArchive RpmDBEntry)
repoRoot, err := packagemetadata.RepoRoot()
if err == nil {
copyAliasFieldComments(reflector.CommentMap, repoRoot)
}
}
pkgMetadataContainer, pkgMetadataMapping := assembleTypeContainer(packagemetadata.AllTypes())
pkgMetadataContainerType := reflect.TypeOf(pkgMetadataContainer)
@ -178,11 +194,6 @@ func encode(schema *jsonschema.Schema) []byte {
}
func write(schema []byte) {
repoRoot, err := packagemetadata.RepoRoot()
if err != nil {
fmt.Println("unable to determine repo root")
os.Exit(1)
}
schemaPath := filepath.Join(repoRoot, "schema", "json", fmt.Sprintf("schema-%s.json", internal.JSONSchemaVersion))
latestSchemaPath := filepath.Join(repoRoot, "schema", "json", "schema-latest.json")

View File

@ -5,9 +5,8 @@ import (
"os"
"strings"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/dave/jennifer/jen"
"github.com/anchore/syft/syft/internal/packagemetadata"
)
// This program is invoked from syft/internal and generates packagemetadata/generated.go
@ -31,7 +30,7 @@ func main() {
fmt.Printf("updating package metadata type list with %+v types\n", len(typeNames))
f := jen.NewFile("packagemetadata")
f.HeaderComment("DO NOT EDIT: generated by syft/internal/packagemetadata/generate/main.go")
f.HeaderComment("DO NOT EDIT: generated by internal/packagemetadata/generate/main.go")
f.ImportName(pkgImport, "pkg")
f.Comment("AllTypes returns a list of all pkg metadata types that syft supports (that are represented in the pkg.Package.Metadata field).")

View File

@ -1,4 +1,4 @@
// DO NOT EDIT: generated by syft/internal/packagemetadata/generate/main.go
// DO NOT EDIT: generated by internal/packagemetadata/generate/main.go
package packagemetadata

View File

@ -4,9 +4,8 @@ import (
"fmt"
"os"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/dave/jennifer/jen"
"github.com/anchore/syft/syft/internal/sourcemetadata"
)
// This program is invoked from syft/internal and generates sourcemetadata/generated.go
@ -25,7 +24,7 @@ func main() {
fmt.Printf("updating source metadata type list with %+v types\n", len(typeNames))
f := jen.NewFile("sourcemetadata")
f.HeaderComment("DO NOT EDIT: generated by syft/internal/sourcemetadata/generate/main.go")
f.HeaderComment("DO NOT EDIT: generated by internal/sourcemetadata/generate/main.go")
f.ImportName(srcImport, "source")
f.Comment("AllTypes returns a list of all source metadata types that syft supports (that are represented in the source.Description.Metadata field).")

View File

@ -1,4 +1,4 @@
// DO NOT EDIT: generated by syft/internal/sourcemetadata/generate/main.go
// DO NOT EDIT: generated by internal/sourcemetadata/generate/main.go
package sourcemetadata

View File

@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spdx/tools-golang/spdx"
@ -18,7 +19,6 @@ import (
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format/internal/spdxutil/helpers"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"

View File

@ -3,13 +3,13 @@ package model
import (
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/anchore/packageurl-go"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/linux"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sbom"

View File

@ -6,11 +6,11 @@ import (
"strings"
"github.com/CycloneDX/cyclonedx-go"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/anchore/packageurl-go"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format/internal"
"github.com/anchore/syft/syft/internal/packagemetadata"
"github.com/anchore/syft/syft/pkg"
)

View File

@ -5,9 +5,9 @@ import (
"strings"
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/source"
)

View File

@ -5,9 +5,9 @@ import (
"strings"
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
)

View File

@ -3,9 +3,9 @@ package helpers
import (
"testing"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/internal/packagemetadata"
"github.com/anchore/syft/syft/pkg"
)

View File

@ -8,8 +8,8 @@ import (
"strings"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/internal/packagemetadata"
"github.com/anchore/syft/syft/license"
"github.com/anchore/syft/syft/pkg"
)

View File

@ -7,7 +7,7 @@ import (
"strconv"
"strings"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/source"
)

View File

@ -4,12 +4,12 @@ import (
"encoding/json"
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/source"
)

View File

@ -6,10 +6,9 @@ import (
"path/filepath"
"testing"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/iancoleman/strcase"
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft/internal/packagemetadata"
)
type schema struct {

View File

@ -8,12 +8,12 @@ import (
stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
formatInternal "github.com/anchore/syft/syft/format/internal"
"github.com/anchore/syft/syft/format/syftjson/model"
"github.com/anchore/syft/syft/internal/packagemetadata"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/linux"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sbom"

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
@ -12,7 +13,6 @@ import (
stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format/syftjson/model"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)

View File

@ -7,6 +7,7 @@ import (
"os"
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -14,7 +15,6 @@ import (
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format/syftjson/model"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"

View File

@ -3,10 +3,10 @@ package syft
import (
"testing"
"github.com/anchore/syft/internal/sourcemetadata"
"github.com/stretchr/testify/require"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/syft/source"
"github.com/anchore/syft/syft/source/sourceproviders"
)

View File

@ -1 +0,0 @@
Please see [schema/json/README.md](../../../schema/json/README.md) for more information on the JSON schema files in this directory.

View File

@ -3,8 +3,8 @@ package testutil
import (
"testing"
"github.com/anchore/syft/syft/internal/packagemetadata"
"github.com/anchore/syft/syft/internal/sourcemetadata"
"github.com/anchore/syft/internal/packagemetadata"
"github.com/anchore/syft/internal/sourcemetadata"
)
type PackageMetadataCompletionTester struct {