Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: embed UUIDMixin into ProtoMessageMixin, auto-generate UUID fields #58

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions backends/ent/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"fmt"
"slices"

"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql/schema"
sqlite "github.com/glebarez/go-sqlite"
"github.com/protobom/protobom/pkg/storage"

"github.com/protobom/storage/internal/backends/ent"
"github.com/protobom/storage/internal/backends/ent/migrate"
_ "github.com/protobom/storage/internal/backends/ent/runtime" // Prevents circular imports in generated code
)

// Backend implements the protobom.pkg.storage.Backend interface.
Expand Down Expand Up @@ -53,7 +55,7 @@ func (backend *Backend) InitClient() error {
}

// Register the SQLite driver as "sqlite3".
if !slices.Contains(sql.Drivers(), "sqlite3") {
if !slices.Contains(sql.Drivers(), dialect.SQLite) {
sqlite.RegisterAsSQLITE3()
}

Expand All @@ -62,7 +64,7 @@ func (backend *Backend) InitClient() error {
clientOpts = append(clientOpts, ent.Debug())
}

client, err := ent.Open("sqlite3", backend.Options.DatabaseFile+dsnParams, clientOpts...)
client, err := ent.Open(dialect.SQLite, backend.Options.DatabaseFile+dsnParams, clientOpts...)
if err != nil {
return fmt.Errorf("failed opening connection to sqlite: %w", err)
}
Expand Down
39 changes: 2 additions & 37 deletions backends/ent/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,9 @@ func (backend *Backend) saveAnnotations(annotations ...*ent.Annotation) TxFunc {
func (backend *Backend) saveDocumentTypes(docTypes []*sbom.DocumentType) TxFunc {
return func(tx *ent.Tx) error {
for _, docType := range docTypes {
id, err := GenerateUUID(docType)
if err != nil {
return err
}

typeName := documenttype.Type(docType.GetType().String())

newDocType := tx.DocumentType.Create().
SetID(id).
SetProtoMessage(docType).
SetNillableType(&typeName).
SetNillableName(docType.Name). //nolint:protogetter
Expand All @@ -154,14 +148,8 @@ func (backend *Backend) saveEdges(edges []*sbom.Edge) TxFunc { //nolint:gocognit
}

for _, edge := range edges {
edgeID, err := GenerateUUID(edge)
if err != nil {
return fmt.Errorf("generating UUID: %w", err)
}

for _, toID := range edge.GetTo() {
newEdgeType := tx.EdgeType.Create().
SetID(edgeID).
SetProtoMessage(edge).
SetType(edgetype.Type(edge.GetType().String())).
SetFromID(nativeIDMap[edge.GetFrom()]).
Expand Down Expand Up @@ -195,7 +183,6 @@ func (backend *Backend) saveExternalReferences(refs []*sbom.ExternalReference, o
}

newRef := tx.ExternalReference.Create().
SetID(extRefID).
SetProtoMessage(ref).
SetURL(ref.GetUrl()).
SetComment(ref.GetComment()).
Expand Down Expand Up @@ -296,14 +283,13 @@ func (backend *Backend) saveIdentifiers(idents map[int32]string, opts ...func(*e
}

func (backend *Backend) saveMetadata(metadata *sbom.Metadata) TxFunc {
mdUUID, err := GenerateUUID(metadata)
id, err := GenerateUUID(metadata)
if err != nil {
return nil
}

return func(tx *ent.Tx) error {
newMetadata := tx.Metadata.Create().
SetID(mdUUID).
SetNativeID(metadata.GetId()).
SetProtoMessage(metadata).
SetVersion(metadata.GetVersion()).
Expand All @@ -317,7 +303,7 @@ func (backend *Backend) saveMetadata(metadata *sbom.Metadata) TxFunc {
return fmt.Errorf("saving metadata: %w", err)
}

backend.ctx = context.WithValue(backend.ctx, metadataIDKey{}, mdUUID)
backend.ctx = context.WithValue(backend.ctx, metadataIDKey{}, id)

for _, fn := range []TxFunc{
backend.savePersons(metadata.GetAuthors()),
Expand All @@ -342,7 +328,6 @@ func (backend *Backend) saveNodeList(nodeList *sbom.NodeList) TxFunc {
}

newNodeList := tx.NodeList.Create().
SetID(id).
SetProtoMessage(nodeList).
SetRootElements(nodeList.GetRootElements())

Expand Down Expand Up @@ -383,7 +368,6 @@ func (backend *Backend) saveNodes(nodes []*sbom.Node) TxFunc { //nolint:funlen,g

backend.ctx = context.WithValue(backend.ctx, nodeIDKey{}, nodeID)
newNode := tx.Node.Create().
SetID(nodeID).
SetNativeID(srcNode.GetId()).
SetProtoMessage(srcNode).
SetAttribution(srcNode.GetAttribution()).
Expand Down Expand Up @@ -459,7 +443,6 @@ func (backend *Backend) savePersons(persons []*sbom.Person) TxFunc { //nolint:go
}

newPerson := tx.Person.Create().
SetID(id).
SetProtoMessage(person).
SetName(person.GetName()).
SetEmail(person.GetEmail()).
Expand Down Expand Up @@ -496,13 +479,7 @@ func (backend *Backend) saveProperties(properties []*sbom.Property, nodeID uuid.
builders := []*ent.PropertyCreate{}

for _, prop := range properties {
id, err := GenerateUUID(prop)
if err != nil {
return err
}

newProp := tx.Property.Create().
SetID(id).
SetProtoMessage(prop).
SetNodeID(nodeID).
SetName(prop.GetName()).
Expand Down Expand Up @@ -554,13 +531,7 @@ func (backend *Backend) savePurposes(purposes []sbom.Purpose, nodeID uuid.UUID)

func (backend *Backend) saveSourceData(sourceData *sbom.SourceData) TxFunc {
return func(tx *ent.Tx) error {
id, err := GenerateUUID(sourceData)
if err != nil {
return err
}

newSourceData := tx.SourceData.Create().
SetID(id).
SetProtoMessage(sourceData).
SetFormat(sourceData.GetFormat()).
SetHashes(sourceData.GetHashes()).
Expand All @@ -583,13 +554,7 @@ func (backend *Backend) saveTools(tools []*sbom.Tool) TxFunc {
builders := []*ent.ToolCreate{}

for _, tool := range tools {
id, err := GenerateUUID(tool)
if err != nil {
return err
}

newTool := tx.Tool.Create().
SetID(id).
SetProtoMessage(tool).
SetName(tool.GetName()).
SetVersion(tool.GetVersion()).
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/google/uuid v1.6.0
github.com/protobom/protobom v0.5.0
github.com/stretchr/testify v1.10.0
google.golang.org/protobuf v1.36.2
google.golang.org/protobuf v1.36.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this downgrade expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, good catch! I'll fix it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll roll it into the next PR

)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
30 changes: 20 additions & 10 deletions internal/backends/ent/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/backends/ent/documenttype/documenttype.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions internal/backends/ent/documenttype_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/backends/ent/edgetype/edgetype.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions internal/backends/ent/edgetype_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading