Skip to content

Commit

Permalink
storage: allow implementations to override MakeDir
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Mar 30, 2022
1 parent ac7bb1f commit fc49420
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions storage/interface.go
Expand Up @@ -36,6 +36,12 @@ type Store interface {
Abort(context.Context, Transaction)
}

// MakeDirer defines the interface a Store could realize to override the
// generic MakeDir functionality in storage.MakeDir
type MakeDirer interface {
MakeDir(context.Context, Transaction, Path) error
}

// TransactionParams describes a new transaction.
type TransactionParams struct {

Expand Down
10 changes: 6 additions & 4 deletions storage/storage.go
Expand Up @@ -51,14 +51,18 @@ func WriteOne(ctx context.Context, store Store, op PatchOp, path Path, value int

// MakeDir inserts an empty object at path. If the parent path does not exist,
// MakeDir will create it recursively.
func MakeDir(ctx context.Context, store Store, txn Transaction, path Path) (err error) {
func MakeDir(ctx context.Context, store Store, txn Transaction, path Path) error {

// Allow the Store implementation to deal with this in its own way.
if md, ok := store.(MakeDirer); ok {
return md.MakeDir(ctx, txn, path)
}

if len(path) == 0 {
return nil
}

node, err := store.Read(ctx, txn, path)

if err != nil {
if !IsNotFound(err) {
return err
Expand All @@ -74,9 +78,7 @@ func MakeDir(ctx context.Context, store Store, txn Transaction, path Path) (err
if _, ok := node.(map[string]interface{}); ok {
return nil
}

return writeConflictError(path)

}

// Txn is a convenience function that executes f inside a new transaction
Expand Down

0 comments on commit fc49420

Please sign in to comment.