Skip to content

Commit

Permalink
Work on BagReader interface
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Jan 16, 2024
1 parent cb46ecf commit 6b31095
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
13 changes: 12 additions & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const (
AlgSha512 = "sha512"
BagItProfileBTR = "btr-v1.0.json"
BagItProfileDefault = "aptrust-v2.2.json"
BagWriterTypeTar = "tar"
BagReaderTypeFileSystem = "filesystem"
BagReaderTypeTar = "tar"
BagWriterTypeFileSystem = "filesystem"
BagWriterTypeTar = "tar"
BTRProfileIdentifier = "https://github.com/dpscollaborative/btr_bagit_profile/releases/download/1.0/btr-bagit-profile.json"
DefaultProfileIdentifier = "https://raw.githubusercontent.com/APTrust/preservation-services/master/profiles/aptrust-v2.2.json"
EmptyProfileIdentifier = "https://raw.githubusercontent.com/APTrust/dart/tree/master/profiles/empty_profile.json"
Expand Down Expand Up @@ -112,6 +114,15 @@ var BagWriterTypeFor = map[string]string{
SerialFormatTar: BagWriterTypeTar,
}

// BagReaderTypeFor maps a BagIt serialization format to the
// type of reader that can write that format.
var BagReaderTypeFor = map[string]string{
"": BagReaderTypeFileSystem,
".tar": BagReaderTypeTar,
SerialFormatNone: BagReaderTypeFileSystem,
SerialFormatTar: BagReaderTypeTar,
}

var SerializationOptions = []string{
SerializationForbidden,
SerializationOptional,
Expand Down
16 changes: 16 additions & 0 deletions core/bag_reader.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core

import "github.com/APTrust/dart-runner/constants"

// BagReader describes the interface that bag readers must implement,
// whether reading from a file system, tar file, zip file, etc.
type BagReader interface {
Expand All @@ -21,3 +23,17 @@ type BagReader interface {
// Close closes the underlying reader.
Close()
}

// GetBagReader returns a bag writer of the specified type.
// Valid types include constants.BagWriterTypeFileSystem and
// constants.BagWriterTypeTar.
func GetBagReader(readerType string, validator *Validator) (BagReader, error) {
switch readerType {
case constants.BagReaderTypeFileSystem:
return NewFileSystemBagReader(validator)
case constants.BagWriterTypeTar:
return NewTarredBagReader(validator)
default:
return nil, constants.ErrUnknownType
}
}
42 changes: 42 additions & 0 deletions core/bag_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package core_test

import (
"os"
"path/filepath"
"testing"

"github.com/APTrust/dart-runner/constants"
"github.com/APTrust/dart-runner/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetBagReader(t *testing.T) {
pathToBag := filepath.Join(os.TempDir(), "testbag.tar")
_, err := os.Create(pathToBag)
defer os.Remove(pathToBag)
require.NoError(t, err)

profile := loadProfile(t, "aptrust-v2.2.json")
validator, err := core.NewValidator(pathToBag, profile)
require.Nil(t, err)

// This should get us a file system bag reader
fsReader, err := core.GetBagReader(constants.BagWriterTypeFileSystem, validator)
require.NoError(t, err)
require.NotNil(t, fsReader)
_, ok := fsReader.(*core.FileSystemBagReader)
assert.True(t, ok)

// And this should get us a tarred bag writer
tarReader, err := core.GetBagReader(constants.BagReaderTypeTar, validator)
require.NoError(t, err)
require.NotNil(t, fsReader)
_, ok = tarReader.(*core.TarredBagReader)
assert.True(t, ok)

// And this should get us an error
noWriter, err := core.GetBagWriter("bad type", filepath.Join(os.TempDir(), "test.tar"), []string{})
require.Error(t, err)
require.Nil(t, noWriter)
}
22 changes: 22 additions & 0 deletions core/file_system_bag_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ package core

// TODO: Define bag reader interface, similar to bag writer interface.
// Then implement this so it's interchangeable with TarredBagReader.

type FileSystemBagReader struct {
validator *Validator
}

func NewFileSystemBagReader(validator *Validator) (*FileSystemBagReader, error) {
return &FileSystemBagReader{
validator: validator,
}, nil
}

func (reader *FileSystemBagReader) ScanMetadata() error {
return nil
}

func (reader *FileSystemBagReader) ScanPayload() error {
return nil
}

func (reader *FileSystemBagReader) Close() {
// No op?
}

0 comments on commit 6b31095

Please sign in to comment.