在golang中将[] byte变成“虚拟”文件对象的简单方法?

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 fulfil the File interface.

翻译翻译:
我知道有Go库可以创建整个文件系统,例如 VFS 。 但我只想将字节数组制成可以满足 File 接口的对象

解决方法:

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:

机翻:

在标准库中没有现成的解决方案,但自己动手并不难。

我们需要的是这个http.File接口:

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.Seekerio.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:

机翻:

请注意,我们可以利用bytes.Reader来完成繁重的任务,因为只有它才能实现io.Reader和io.Seeker。io.Closer可以是noop,Readdir()可能返回nil,nil,因为我们模拟的是文件而不是目录,它的Readdir()甚至不会被调用。

“最难”的部分是模拟Stat()以返回实现os.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:

机翻:

有了这些,我们就可以创建模拟的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

当然如果不使用bytes.Reader,我们就得自己处理Read、Seek、Stat、Readdir等方法,参考下面的例子:

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}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值