douxian9060 2018-05-17 07:58
浏览 30
已采纳

如何实现自己的文件类型?

I am having a function that opens a file and writes to it using the os.OpenFile function.

What I basically want to do is mock the File that is getting returned by it in order to write tests. Because I want to better understand Go, I'd like to do it without using third party packages.

Here is what I have tried:

My Package

package logger

import (
    "fmt"
    "time"
    "sync"
    "os"
    "strings"
    "path/filepath"
    "io"
)

const timestampFormat = "2006-01-02 15:04:05.999999999"

type FileOpener interface {
    OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
}

type RotateWriter struct {
    fileOpener FileOpener
    lock       sync.Mutex
    filename   string
    fp         *os.File
}

type defaultFileOpener struct{}

func (fo defaultFileOpener) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
    return os.OpenFile(name, flag, perm)
}

func CreateRotateWriter(filename string, fileOpener FileOpener) (RotateWriter) {
    if (fileOpener == nil) {
        return RotateWriter{filename: filename, fileOpener: defaultFileOpener{}}
    }
    return RotateWriter{filename: filename, fileOpener: fileOpener}
}

func (writer RotateWriter) Write(bytes []byte) (int, error) {
    writer.lock.Lock()
    defer writer.lock.Unlock()

    extension := filepath.Ext(writer.filename)
    filename := strings.TrimSuffix(writer.filename, extension)

    // There is a specific constants that are used for formatting dates.
    // For example 2006 is the YYYYY, 01 is MM and 02 is DD
    // Check https://golang.org/src/time/format.go line 88 for reference
    fullFilename := filename + time.Now().UTC().Format("-2006-01-02") + extension

    // Open the file for the first time if we don't already did so
    if writer.fp == nil {
        fp, err := os.OpenFile(fullFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
            return 0, err
        }
        writer.fp = fp;
    }

    // We are now pointing to a different file. Close the previous one and open a new one
    if fullFilename != writer.fp.Name() {
        writer.fp.Close()
        fp, err := os.OpenFile(fullFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
            return 0, err
        }
        writer.fp = fp;
    }

    return writer.fp.Write([]byte("[" + time.Now().UTC().Format(timestampFormat) + "] " + string(bytes)))
}

What I was hoping to do in my test package is something like this

type file interface {
    io.Closer
    io.Reader
    io.ReaderAt
    io.Seeker
    Stat() (os.FileInfo, error)
}

type fileType struct{
    fd      int
    name    string
    contents string // Where I'll keep the in-memory contents maybe
}
type defaultFileOpener struct{

}
func (fo defaultFileOpener) OpenFile(name string, flag int, perm os.FileMode) (*file, error){
    return &fileType{1, name, ""}, nil //Cannot use &fileType{1, name, ""}(type *fileType) as type *file
}

func (f fileType) Write(bytes []byte) (int, error){
    f.contents += string(bytes)
    return len(bytes), nil
}

I'm most probably misunderstand something, is it even possible to create my own File type in go?

  • 写回答

1条回答 默认 最新

  • doujuchuan9915 2018-05-17 08:53
    关注

    From the snippet it's not clear whether or not the *fileType implements all of the methods of the file interface, but if it doesn't you should first make sure that it does. Because if it doesn't you'll not be able to use it in your tests as you might intend.


    Your file opener's OpenFile method should have the interface as its return type, not the pointer to the interface. That is:

    func (fo defaultFileOpener) OpenFile(name string, flag int, perm os.FileMode) (file, error) {
    

    This is because *file is not the same type as file, and a value of a type that implements the file interface (e.g. yours *fileType) cannot be used where the pointer to that interface is expected.


    And returning a pointer to an interface is almost never what you actually want, maybe it would make sense to do that if you wanted switch the interface value for another using pointer indirection, but that does not seem to be the case here. If you scan through the standard library you'll have a hard time finding functions/methods that return pointers to interface types...

    But let's say that that's what you want to do then you have to return a pointer to a value of the interface type, not a pointer to a type that implements the interface.

    E.g.

    var f file = &fileType{} // f is of type file
    return &f, nil           // &f is of type *file
    

    Keep in mind that f's type has to be file for the return to work, if it's just *fileType it will not compile.

    var f = &fileType{} // f is of type *fileType
    return &f, nil      // &f is of type **fileType
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)