Simples way to make a []byte into a "virtual" File object in golang?
Asked Answered
H

3

6

I know there are Go libraries that create entire filesystems like VFS. But I only want to make a byte array into something that can fulfill the File interface.

Heterophyte answered 8/10, 2018 at 7:21 Comment(1)
Do you actually have to mock an entire file? I'm not sure how a slice of bytes would logically implement Readdir (other than []os.FileInfo{}, someErr)Tympany
A
9

There is no ready solution for this in the standard library, but it's not that hard to do it yourself.

What we need is this http.File interface:

type File interface {
        io.Closer
        io.Reader
        io.Seeker
        Readdir(count int) ([]os.FileInfo, error)
        Stat() (os.FileInfo, error)
}

Please note that we can utilize bytes.Reader to do the heavy task, as that alone implements io.Reader and io.Seeker. io.Closer can be a noop, and Readdir() may return nil, nil as we're mocking a file not a directory, its Readdir() won't even be called.

The "hardest" part is to mock Stat() to return a value that implements os.FileInfo.

Here's a simple mocked FileInfo:

type myFileInfo struct {
    name string
    data []byte
}

func (mif myFileInfo) Name() string       { return mif.name }
func (mif myFileInfo) Size() int64        { return int64(len(mif.data)) }
func (mif myFileInfo) Mode() os.FileMode  { return 0444 }        // Read for all
func (mif myFileInfo) ModTime() time.Time { return time.Time{} } // Return anything
func (mif myFileInfo) IsDir() bool        { return false }
func (mif myFileInfo) Sys() interface{}   { return nil }

And with that we have everything to create our mocked http.File:

type MyFile struct {
    *bytes.Reader
    mif myFileInfo
}

func (mf *MyFile) Close() error { return nil } // Noop, nothing to do

func (mf *MyFile) Readdir(count int) ([]os.FileInfo, error) {
    return nil, nil // We are not a directory but a single file
}

func (mf *MyFile) Stat() (os.FileInfo, error) {
    return mf.mif, nil
}

Example using it (try it on the Go Playground):

data := []byte{0, 1, 2, 3}

mf := &MyFile{
    Reader: bytes.NewReader(data),
    mif: myFileInfo{
        name: "somename.txt",
        data: data,
    },
}

var f http.File = mf
_ = f
Avebury answered 8/10, 2018 at 8:5 Comment(0)
T
0

Seems simple enough to mock this yourself.

type MockFile struct {
    data    []byte
    isOpen  bool
    offset  int64
}

type MockFileInfo struct {
    mockFile *MockFile
}
func (mfi *MockFileInfo) Name() string       { return "MockFile" }
func (mfi *MockFileInfo) Size() int64        { return len(mfi.data) }
func (mfi *MockFileInfo) Mode() os.FileMode  { return os.ModeIrregular }
func (mfi *MockFileInfo) ModTime() time.Time { return time.Now() }
func (mfi *MockFileInfo) IsDir() bool        { return false }
func (mfi *MockFileInfo) Sys() interface     { return nil }

func (mf *MockFile) Read(p []byte) (n int, err error) {
    if mf.isOpen {
        n = copy(p, mf.data[mf.offset:])
        mf.offset += n
    } else {
        err = errors.New("Cannot read from closed MockFile")
    }
    return
}

func (mf *MockFile) Close() error {
    if !mf.isOpen {
        return errors.New("Cannot close an already closed MockFile")
    mf.isOpen = false
    return nil
}

func (mf *MockFile) Seek(offset int64, whence int) (ret int64, err error) {
    var relativeTo int64
    switch whence {
        case 0:
            relativeTo = 0
        case 1:
            relativeTo = mf.offset
        case 2:
            relativeTo = len(mf.data)
    }
    ret := relativeTo + offset
    if ret < 0 || ret > len(mf.data) {
        return -1, errors.New("New offset would fall outside of the MockFile")
    }
    mf.offset = ret
    return
}

func (mf *MockFile) Readdir(count int) ([]os.FileInfo, error) {
    if count <= 0 {
        return []os.FileInfo{}, nil
    }
    return []os.FileInfo{}, errors.New("MockFiles have no associated directory")
}

func (mf *MockFile) Stat() (os.FileInfo, error) {
    return MockFileInfo{mf}
}

func OpenMockFile(data []byte) *MockFile {
    mf := MockFile{data, true, 0}
}
Tympany answered 8/10, 2018 at 7:56 Comment(1)
admittedly I'm not sure if MockFile.Stat() quite lives up to its expectation. It could be that MockFileInfo should take a MockFile rather than a *MockFile. What happens if you os.Stat a real file, then change its size? Does the file size change reflect in the os.FileInfo?Tympany
P
0

If you are looking this answer to gzip http2 push, you can do it with just options:

options := &http.PushOptions{
                Header: http.Header{
                    "Accept-Encoding": r.Header["Accept-Encoding"],
                },
            }
            if err := pusher.Push(filePush, options); err != nil {
                log.Printf("Failed to push: %v", err)
                return
            }
Prowel answered 20/4, 2020 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.