Skip to content
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

proposal: os: Provide the inverse of DirFS(string) #57546

Open
livingsilver94 opened this issue Jan 2, 2023 · 4 comments
Open

proposal: os: Provide the inverse of DirFS(string) #57546

livingsilver94 opened this issue Jan 2, 2023 · 4 comments
Labels
Milestone

Comments

@livingsilver94
Copy link

Just as os.DirFS(string) returns an fs.FS implementation based on the path passed, the inverse operation (fs.FS -> string) should be provided.

Possible use case

Suppose we have a repository-like struct. This repo returns a different path depending on whether the fs.FS is a real OS file system, or any other "virtual" file systems (e.g. a testing one, or a network fs like S3).

type Repo struct {
    fs fs.FS
}

func (r Repo) Files() (string, error) {
    switch {
        case aRealPath:
            return "/the/actual/path", nil
        default:
            // Copy or download files in /tmp, for example.
            return "/tmp/randomPath", nil
    }
}

Possible solution

Simply export the os.dirFS. This way, we can path, ok := fs.(os.DirFS).

@gopherbot gopherbot added this to the Proposal milestone Jan 2, 2023
@earthboundkid
Copy link
Contributor

ISTM you could make a struct that records the original file path but embeds an fs.FS and just use that. It doesn’t seem like a common enough use case that you need to worry about someone outside your application passing you an fs.FS of unknown provenance.

That said, maybe there could be an optional method URI() for an FS to implement that returns s3://bucket mem:// file:///path etc.

@seankhliao
Copy link
Member

this looks a bit like poor design, where the thing you test isn't the same as the thing running in production.

fwiw, reflect works

@livingsilver94
Copy link
Author

ISTM you could make a struct that records the original file path but embeds an fs.FS and just use that.

This is exactly what I ended up with before filing this issue. I thought it could be useful to have a common way to check if the FS is local and real.

That said, maybe there could be an optional method URI() for an FS to implement that returns s3://bucket mem:// file:///path etc.

Or a Kind() string returning "file", "s3", "testing" or whatever else depending on the implementation.

this looks a bit like poor design, where the thing you test isn't the same as the thing running in production.

Uh, is that a bad thing? After all testing/fstest is a thing. Also, a piece of code may act differently depending on whether the FS is local, instead of Samba, NFS, S3 or any other network file system.

@ianlancetaylor
Copy link
Contributor

This is easy to do for specific uses. Is it really such a common use case that it should be added to the standard library? Can you show some existing examples that would benefit form this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants