feat(storage): Make Option fields exported

This makes it possible to have external packages implement the
storage.FS, storage.Container, and storage.File interfaces and correctly
inspect the various options.
This commit is contained in:
Timothy Messier committed 2023-05-31 12:18:10 +00:00
1 parent 7632143861
commit 897b70f090
2 files changed
+25 -25

No files matched your search

+11 -11
View File
@@ -37,8 +37,8 @@ const (
ReadWrite
)
// getOpts - iterate the inbound Options and return a struct.
func getOpts(opt ...Option) Options {
// GetOpts - iterate the inbound Options and return a struct.
func GetOpts(opt ...Option) Options {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
@@ -48,17 +48,17 @@ func getOpts(opt ...Option) Options {
func getDefaultOptions() Options {
return Options{
withCloseSyncMode: Asynchronous,
withFileAccessMode: ReadOnly,
withCreateFile: false,
WithCloseSyncMode: Asynchronous,
WithFileAccessMode: ReadOnly,
WithCreateFile: false,
}
}
// Options are storage options.
type Options struct {
withCloseSyncMode SyncMode
withFileAccessMode AccessMode
withCreateFile bool
WithCloseSyncMode SyncMode
WithFileAccessMode AccessMode
WithCreateFile bool
}
// Option is a storage option.
@@ -67,20 +67,20 @@ type Option func(*Options)
// WithCloseSyncMode sets how a file is synced when closed.
func WithCloseSyncMode(m SyncMode) Option {
return func(o *Options) {
o.withCloseSyncMode = m
o.WithCloseSyncMode = m
}
}
// WithCreateFile indicates a file should be created when opening.
func WithCreateFile() Option {
return func(o *Options) {
o.withCreateFile = true
o.WithCreateFile = true
}
}
// WithFileAccessMode sets the access mode when a file is opened.
func WithFileAccessMode(m AccessMode) Option {
return func(o *Options) {
o.withFileAccessMode = m
o.WithFileAccessMode = m
}
}
+14 -14
View File
@@ -15,43 +15,43 @@ func Test_getOpts(t *testing.T) {
assert := assert.New(t)
t.Run("WithCloseSyncMode", func(t *testing.T) {
testOpts := getDefaultOptions()
opts := getOpts(WithCloseSyncMode(Asynchronous))
opts := GetOpts(WithCloseSyncMode(Asynchronous))
assert.Equal(testOpts, opts)
testOpts = getDefaultOptions()
opts = getOpts(WithCloseSyncMode(Synchronous))
testOpts.withCloseSyncMode = Synchronous
opts = GetOpts(WithCloseSyncMode(Synchronous))
testOpts.WithCloseSyncMode = Synchronous
assert.Equal(opts, testOpts)
testOpts = getDefaultOptions()
opts = getOpts(WithCloseSyncMode(NoSync))
testOpts.withCloseSyncMode = NoSync
opts = GetOpts(WithCloseSyncMode(NoSync))
testOpts.WithCloseSyncMode = NoSync
assert.Equal(opts, testOpts)
})
t.Run("WithFileAccessMode", func(t *testing.T) {
testOpts := getDefaultOptions()
opts := getOpts(WithFileAccessMode(ReadOnly))
opts := GetOpts(WithFileAccessMode(ReadOnly))
assert.Equal(testOpts, opts)
testOpts = getDefaultOptions()
opts = getOpts(WithFileAccessMode(WriteOnly))
testOpts.withFileAccessMode = WriteOnly
opts = GetOpts(WithFileAccessMode(WriteOnly))
testOpts.WithFileAccessMode = WriteOnly
assert.Equal(opts, testOpts)
testOpts = getDefaultOptions()
opts = getOpts(WithFileAccessMode(ReadWrite))
testOpts.withFileAccessMode = ReadWrite
opts = GetOpts(WithFileAccessMode(ReadWrite))
testOpts.WithFileAccessMode = ReadWrite
assert.Equal(opts, testOpts)
})
t.Run("WithCreateFile", func(t *testing.T) {
testOpts := getDefaultOptions()
opts := getOpts()
testOpts.withCreateFile = false
opts := GetOpts()
testOpts.WithCreateFile = false
assert.Equal(testOpts, opts)
testOpts = getDefaultOptions()
opts = getOpts(WithCreateFile())
testOpts.withCreateFile = true
opts = GetOpts(WithCreateFile())
testOpts.WithCreateFile = true
assert.Equal(opts, testOpts)
})
}