ent

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 27 Imported by: 1

Documentation

Overview

Example
package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"github.com/protobom/protobom/pkg/reader"
	"google.golang.org/protobuf/encoding/protojson"

	"github.com/protobom/storage/backends/ent"
)

func main() {
	cwd, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	dbFile := filepath.Join(cwd, "example.db")

	// Remove example.db if it already exists.
	if err := os.Remove(dbFile); err != nil && !errors.Is(err, os.ErrNotExist) {
		panic(err)
	}

	rdr := reader.New()
	backend := ent.NewBackend().WithDatabaseFile(dbFile)

	if err := backend.InitClient(); err != nil {
		panic(err)
	}

	defer backend.CloseClient()

	sbom, err := rdr.ParseFile(filepath.Join(cwd, "testdata", "sbom.cdx.json"))
	if err != nil {
		panic(err)
	}

	if err := backend.Store(sbom, nil); err != nil {
		panic(err)
	}

	retrieved, err := backend.Retrieve(sbom.GetMetadata().GetId(), nil)
	if err != nil {
		panic(err)
	}

	// Remove source data URI to allow comparison.
	retrieved.GetMetadata().GetSourceData().Uri = nil

	// Produces a different output than the standard [encoding/json] package,
	// which does not operate correctly on protocol buffer messages.
	output, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(retrieved)
	if err != nil {
		panic(err)
	}

	// The output produced by the protojson package is intentionally non-deterministic.
	// Format with standard encoding/json package for consistent whitespace.
	formatted := bytes.NewBuffer([]byte{})
	if err := json.Indent(formatted, output, "", "  "); err != nil {
		panic(err)
	}

	fmt.Println(formatted.String())
}
Output:
{
  "metadata": {
    "id": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
    "version": "1",
    "date": "2020-04-13T20:20:39Z",
    "source_data": {
      "format": "application/vnd.cyclonedx+json;version=1.5",
      "hashes": {
        "2": "1ecc17c081f9a0b452b1d8a0d846901bcc40508f",
        "3": "71a3948e45c0bcd83a617ed94674079778d10a0578932e6e536533339b1bbea5",
        "5": "2cc9ac5ab13a8074463e85996e91aa96916a08d33fc3aff9129dd44b24b850884f6176898a21d48dabd9f3824a2dd6bcc1f350e8f13d4be1c564211d1108e43c"
      },
      "size": "5263"
    }
  },
  "node_list": {
    "nodes": [
      {
        "id": "protobom-auto--000000001",
        "name": "Acme Application",
        "version": "9.1.1",
        "primary_purpose": [
          "APPLICATION"
        ]
      },
      {
        "id": "pkg:npm/acme/component@1.0.0",
        "name": "tomcat-catalina",
        "version": "9.0.14",
        "licenses": [
          "Apache-2.0"
        ],
        "license_concluded": "Apache-2.0",
        "identifiers": {
          "1": "pkg:npm/acme/component@1.0.0"
        },
        "hashes": {
          "1": "3942447fac867ae5cdb3229b658f4d48",
          "2": "e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a",
          "3": "f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b",
          "5": "e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282"
        },
        "primary_purpose": [
          "LIBRARY"
        ]
      },
      {
        "id": "protobom-auto--000000003",
        "name": "mylibrary",
        "version": "1.0.0",
        "primary_purpose": [
          "LIBRARY"
        ]
      }
    ],
    "edges": [
      {
        "type": "contains",
        "from": "protobom-auto--000000001",
        "to": [
          "pkg:npm/acme/component@1.0.0",
          "protobom-auto--000000003"
        ]
      },
      {
        "type": "dependsOn",
        "from": "pkg:npm/acme/component@1.0.0",
        "to": [
          "pkg:npm/acme/component@1.0.0"
        ]
      }
    ],
    "root_elements": [
      "protobom-auto--000000001"
    ]
  }
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMultipleDocuments = errors.New("multiple documents matching ID")
	ErrMissingDocument   = errors.New("no documents matching IDs")
)

Functions

func GenerateUUID added in v0.2.0

func GenerateUUID(msg proto.Message) (uuid.UUID, error)

GenerateUUID returns a deterministic UUID derived from the hash of a protobuf message.

Types

type Annotation added in v0.1.5

type Annotation = ent.Annotation

Annotation is the model entity for the Annotation schema.

type Annotations added in v0.1.5

type Annotations = ent.Annotations

Annotations is a parsable slice of Annotation.

type Backend

type Backend struct {

	// Options is the set of options common to all ent Backends.
	Options *BackendOptions
	// contains filtered or unexported fields
}

Backend implements the protobom.pkg.storage.Backend interface.

func NewBackend

func NewBackend(opts ...Option) *Backend

func (*Backend) AddAnnotationToDocuments added in v0.1.4

func (backend *Backend) AddAnnotationToDocuments(name, value string, documentIDs ...string) error

AddAnnotationToDocuments applies a single named annotation value to multiple documents.

func (*Backend) AddAnnotationToNodes added in v0.2.0

func (backend *Backend) AddAnnotationToNodes(name, value string, nodeIDs ...string) error

AddAnnotationToNodes applies a single named annotation value to multiple nodes.

func (*Backend) AddDocumentAnnotations added in v0.2.0

func (backend *Backend) AddDocumentAnnotations(documentID, name string, values ...string) error

AddDocumentAnnotations applies multiple named annotation values to a single document.

func (*Backend) AddNodeAnnotations added in v0.2.0

func (backend *Backend) AddNodeAnnotations(nodeID, name string, values ...string) error

AddNodeAnnotations applies multiple named annotation values to a single node.

func (*Backend) ClearDocumentAnnotations added in v0.2.0

func (backend *Backend) ClearDocumentAnnotations(documentIDs ...string) error

ClearDocumentAnnotations removes all annotations from the specified documents.

func (*Backend) ClearNodeAnnotations added in v0.2.0

func (backend *Backend) ClearNodeAnnotations(nodeIDs ...string) error

ClearNodeAnnotations removes all annotations from the specified nodes.

func (*Backend) CloseClient

func (backend *Backend) CloseClient()

func (*Backend) Debug

func (backend *Backend) Debug() *Backend

func (*Backend) Ent added in v0.2.0

func (backend *Backend) Ent() *ent.Client

func (*Backend) GetDocumentAnnotations added in v0.1.4

func (backend *Backend) GetDocumentAnnotations(documentID string, names ...string) (ent.Annotations, error)

GetDocumentAnnotations gets all annotations for the specified document, limited to a set of annotation names if specified.

func (*Backend) GetDocumentUniqueAnnotation added in v0.1.5

func (backend *Backend) GetDocumentUniqueAnnotation(documentID, name string) (string, error)

GetDocumentUniqueAnnotation gets the value for a unique annotation.

func (*Backend) GetDocumentsByAnnotation added in v0.1.4

func (backend *Backend) GetDocumentsByAnnotation(name string, values ...string) ([]*sbom.Document, error)

GetDocumentsByAnnotation gets all documents having the specified named annotation, limited to a set of annotation values if specified.

func (*Backend) GetDocumentsByID added in v0.1.1

func (backend *Backend) GetDocumentsByID(ids ...string) ([]*sbom.Document, error)

func (*Backend) GetExternalReferencesByDocumentID added in v0.1.1

func (backend *Backend) GetExternalReferencesByDocumentID(
	id string, types ...string,
) ([]*sbom.ExternalReference, error)

func (*Backend) GetNodeAnnotations added in v0.2.0

func (backend *Backend) GetNodeAnnotations(nodeID string, names ...string) (ent.Annotations, error)

GetNodeAnnotations gets all annotations for the specified node, limited to a set of annotation names if specified.

func (*Backend) GetNodeUniqueAnnotation added in v0.2.0

func (backend *Backend) GetNodeUniqueAnnotation(nodeID, name string) (string, error)

GetNodeUniqueAnnotation gets the value for a unique annotation.

func (*Backend) GetNodesByAnnotation added in v0.2.0

func (backend *Backend) GetNodesByAnnotation(name string, values ...string) ([]*sbom.Node, error)

GetNodesByAnnotation gets all nodes having the specified named annotation, limited to a set of annotation values if specified.

func (*Backend) GetNodesByID added in v0.2.0

func (backend *Backend) GetNodesByID(ids ...string) ([]*sbom.Node, error)

func (*Backend) InitClient

func (backend *Backend) InitClient() error

func (*Backend) RemoveDocumentAnnotations added in v0.2.0

func (backend *Backend) RemoveDocumentAnnotations(documentID, name string, values ...string) error

RemoveDocumentAnnotations removes all annotations with the specified name from the document, limited to a set of annotation values if specified.

func (*Backend) RemoveNodeAnnotations added in v0.2.0

func (backend *Backend) RemoveNodeAnnotations(nodeID, name string, values ...string) error

RemoveNodeAnnotations removes all annotations with the specified name from the node, limited to a set of annotation values if specified.

func (*Backend) Retrieve

func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (doc *sbom.Document, err error)

Retrieve implements the storage.Retriever interface.

func (*Backend) SetDocumentAnnotations added in v0.2.0

func (backend *Backend) SetDocumentAnnotations(documentID, name string, values ...string) error

SetDocumentAnnotations explicitly sets the named annotations for the specified document.

func (*Backend) SetDocumentUniqueAnnotation added in v0.2.0

func (backend *Backend) SetDocumentUniqueAnnotation(documentID, name, value string) error

SetDocumentUniqueAnnotation sets a named annotation value that is unique to the specified document.

func (*Backend) SetNodeAnnotations added in v0.2.0

func (backend *Backend) SetNodeAnnotations(nodeID, name string, values ...string) error

SetNodeAnnotations explicitly sets the named annotations for the specified node.

func (*Backend) SetNodeUniqueAnnotation added in v0.2.0

func (backend *Backend) SetNodeUniqueAnnotation(nodeID, name, value string) error

SetNodeUniqueAnnotation sets a named annotation value that is unique to the specified node.

func (*Backend) Store

func (backend *Backend) Store(doc *sbom.Document, opts *storage.StoreOptions) error

Store implements the storage.Storer interface.

func (*Backend) WithAnnotation added in v0.1.5

func (backend *Backend) WithAnnotation(name, value string, unique bool) *Backend

func (*Backend) WithAnnotations added in v0.1.5

func (backend *Backend) WithAnnotations(annotations Annotations) *Backend

func (*Backend) WithBackendOptions

func (backend *Backend) WithBackendOptions(opts *BackendOptions) *Backend

func (*Backend) WithDatabaseFile

func (backend *Backend) WithDatabaseFile(file string) *Backend

type BackendOptions

type BackendOptions struct {
	// DatabaseFile is the file path of the SQLite database to be created.
	DatabaseFile string

	// Annotations is a slice of annotations to apply to stored document.
	Annotations

	// Debug configures the ent client to output all SQL statements during execution.
	Debug bool
}

BackendOptions contains options specific to the protobom ent backend.

func NewBackendOptions

func NewBackendOptions() *BackendOptions

NewBackendOptions creates a new BackendOptions for the backend.

type Option

type Option func(*Backend)

Option represents a single configuration option for the ent backend.

func Debug

func Debug() Option

func WithBackendOptions

func WithBackendOptions(opts *BackendOptions) Option

func WithDatabaseFile

func WithDatabaseFile(file string) Option

type TxFunc added in v0.2.0

type TxFunc func(*ent.Tx) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL