-
Notifications
You must be signed in to change notification settings - Fork 7
Interface->fs.FS translation logic #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ahouene
wants to merge
9
commits into
main
Choose a base branch
from
fs-wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f05962d
add Interface->fs.FS translation logic
ahouene 36ae907
comply with golangci-lint for tester
ahouene aad3981
fix ineffassign, continuing prev commit
ahouene 7f96f7b
fix FileMode being in decimal to octal
ahouene 58ec331
reset underlying reader for `fs.File` implementation on Close only
ahouene 30f8c03
remove optional fs interfaces implementation where they're not added …
ahouene 3c22efa
bring fs tests together with backend tests, add context to wrapper
ahouene be29704
format fs.go
ahouene 6935a52
Merge branch 'main' into fs-wrapper
ahouene File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package simpleblob | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io/fs" | ||
"time" | ||
) | ||
|
||
// fsInterfaceWrapper wraps an Interface and implements fs.FS. | ||
type fsInterfaceWrapper struct { | ||
Interface | ||
ctx context.Context | ||
} | ||
|
||
// fsBlobWrapper represents data upstream and implements both fs.File | ||
// and fs.FileInfo for convenience. | ||
type fsBlobWrapper struct { | ||
b *Blob | ||
parent *fsInterfaceWrapper | ||
r *bytes.Reader | ||
} | ||
|
||
// AsFS casts the provided interface to a fs.FS interface if supported, | ||
// else it wraps it to replicate its functionalities. | ||
func AsFS(ctx context.Context, st Interface) fs.FS { | ||
if fsys, ok := st.(fs.FS); ok { | ||
return fsys | ||
} | ||
return &fsInterfaceWrapper{st, ctx} | ||
} | ||
|
||
// Open retrieves a Blob, wrapped as a fs.File, from the underlying Interface. | ||
func (stw *fsInterfaceWrapper) Open(name string) (fs.File, error) { | ||
b, err := stw.Load(stw.ctx, name) | ||
if err != nil { | ||
return nil, &fs.PathError{Op: "open", Path: name, Err: err} | ||
} | ||
return &fsBlobWrapper{&Blob{name, int64(len(b))}, stw, nil}, nil | ||
} | ||
|
||
// ReadDir satisfies fs.ReadDirFS | ||
func (stw *fsInterfaceWrapper) ReadDir(name string) ([]fs.DirEntry, error) { | ||
ls, err := stw.List(stw.ctx, "") | ||
if err != nil { | ||
return nil, &fs.PathError{Op: "readdir", Path: name, Err: err} | ||
} | ||
ret := make([]fs.DirEntry, len(ls)) | ||
for i, entry := range ls { | ||
blob := entry | ||
ret[i] = &fsBlobWrapper{&blob, stw, nil} | ||
} | ||
return ret, nil | ||
} | ||
|
||
// ReadFile implements fs.ReadFileFS on top of an Interface wrapped as a fs.FS. | ||
func (stw *fsInterfaceWrapper) ReadFile(name string) ([]byte, error) { | ||
return stw.Load(stw.ctx, name) | ||
} | ||
|
||
// fs.FileInfo implementation | ||
|
||
func (*fsBlobWrapper) IsDir() bool { return false } | ||
func (*fsBlobWrapper) ModTime() time.Time { return time.Time{} } | ||
func (*fsBlobWrapper) Mode() fs.FileMode { return 0777 } | ||
func (bw *fsBlobWrapper) Name() string { return bw.b.Name } | ||
func (bw *fsBlobWrapper) Sys() interface{} { return bw.parent } | ||
func (bw *fsBlobWrapper) Size() int64 { return bw.b.Size } | ||
|
||
// fs.File implementation | ||
|
||
func (bw *fsBlobWrapper) Stat() (fs.FileInfo, error) { | ||
return bw, nil | ||
} | ||
func (bw *fsBlobWrapper) Read(p []byte) (int, error) { | ||
if bw.r == nil { | ||
b, err := bw.parent.Interface.Load(bw.parent.ctx, bw.b.Name) | ||
if err != nil { | ||
return 0, err | ||
} | ||
bw.r = bytes.NewReader(b) | ||
} | ||
return bw.r.Read(p) | ||
} | ||
func (bw *fsBlobWrapper) Close() error { | ||
if bw.r != nil { | ||
bw.r = nil | ||
} | ||
return nil | ||
} | ||
Comment on lines
+75
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: use |
||
|
||
// fs.DirEntry implementation | ||
|
||
func (bw *fsBlobWrapper) Type() fs.FileMode { return bw.Mode() } | ||
func (bw *fsBlobWrapper) Info() (fs.FileInfo, error) { return bw, nil } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be best if we do not implement extra FS interface and instead stick to the basic
fs.FS
, but ReadDirFS could be a useful addition and corresponds to theList
call.I'm not sure if the single root directory restriction is problematic or not, I can imagine that we may want to store blocs with
/
in the future. Currently we do not clearly describe what kind of names are allowed and this could vary by backend, which is probably not a good thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove the name checking. I feel like
fs.ReadFileFS
should stay too because it matches whatLoad
does. What do you think?