github.com/bazelbuild/remote-apis-sdks@v0.0.0-20240425170053-8a36686a6350/go/pkg/uploadinfo/entry.go (about)

     1  // Package uploadinfo provides a way to move metadata and/or actual data on blobs
     2  // to be uploaded.
     3  package uploadinfo
     4  
     5  import (
     6  	"github.com/bazelbuild/remote-apis-sdks/go/pkg/digest"
     7  	"google.golang.org/protobuf/proto"
     8  )
     9  
    10  const (
    11  	ueBlob = iota
    12  	uePath
    13  )
    14  
    15  // Entry should remain immutable upon creation.
    16  // Should be created using constructor. Only Contents or Path must be set.
    17  // In case of a malformed entry, Contents takes precedence over Path.
    18  type Entry struct {
    19  	Digest   digest.Digest
    20  	Contents []byte
    21  	Path     string
    22  
    23  	ueType      int
    24  	virtualFile bool
    25  }
    26  
    27  // IsBlob returns whether this Entry is for a blob in memory.
    28  func (ue *Entry) IsBlob() bool {
    29  	return ue.ueType == ueBlob
    30  }
    31  
    32  // IsFile returns whether this Entry is for a file in disk.
    33  func (ue *Entry) IsFile() bool {
    34  	return ue.ueType == uePath
    35  }
    36  
    37  // IsVirtualFile returns whether this Entry is a virtual file.
    38  func (ue *Entry) IsVirtualFile() bool {
    39  	return ue.virtualFile
    40  }
    41  
    42  // EntryFromBlob creates an Entry from an in memory blob.
    43  func EntryFromBlob(blob []byte) *Entry {
    44  	return &Entry{
    45  		Contents: blob,
    46  		Digest:   digest.NewFromBlob(blob),
    47  		ueType:   ueBlob,
    48  	}
    49  }
    50  
    51  // EntryFromProto creates an Entry from an in memory proto.
    52  func EntryFromProto(msg proto.Message) (*Entry, error) {
    53  	blob, err := proto.Marshal(msg)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return EntryFromBlob(blob), nil
    58  }
    59  
    60  // EntryFromFile creates an entry from a file in disk.
    61  func EntryFromFile(dg digest.Digest, path string) *Entry {
    62  	return &Entry{
    63  		Digest: dg,
    64  		Path:   path,
    65  		ueType: uePath,
    66  	}
    67  }
    68  
    69  // EntryFromVirtualFile creates an entry from a file not on disk.
    70  // The digest is expected to exist in the CAS.
    71  func EntryFromVirtualFile(dg digest.Digest, path string) *Entry {
    72  	return &Entry{
    73  		Digest:      dg,
    74  		Path:        path,
    75  		ueType:      uePath,
    76  		virtualFile: true,
    77  	}
    78  }