Compare commits
No commits in common. "2f802298343f73c9539d660392fe1f1ac89a0f0a" and "63ba97e7ff918e0aea54edfe5eef4a463747837e" have entirely different histories.
2f80229834
...
63ba97e7ff
27
README.md
27
README.md
|
@ -4,29 +4,6 @@ Package **strfs** provides a virtual file-system, whre a `fs.File` can be create
|
||||||
|
|
||||||
## Documention
|
## Documention
|
||||||
|
|
||||||
Online documentation, which includes examples, can be found at: http://godoc.org/sourcecode.social/reiver/go-strfs
|
Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-strfs
|
||||||
|
|
||||||
[![GoDoc](https://godoc.org/sourcecode.social/reiver/go-strfs?status.svg)](https://godoc.org/sourcecode.social/reiver/go-strfs)
|
[![GoDoc](https://godoc.org/github.com/reiver/go-strfs?status.svg)](https://godoc.org/github.com/reiver/go-strfs)
|
||||||
|
|
||||||
## Example fs.File
|
|
||||||
|
|
||||||
Here is an example of turning a Go `string` into a `fs.File`:
|
|
||||||
|
|
||||||
```go
|
|
||||||
import "sourcecode.social/reiver/go-strfs"
|
|
||||||
|
|
||||||
// ...
|
|
||||||
|
|
||||||
var s string = "<!DOCTYPE html>"+"\n"+"<html><body>Hello world!</body></html>"
|
|
||||||
|
|
||||||
var content strfs.Content = strfs.CreateContent(s)
|
|
||||||
|
|
||||||
var regularfile strfs.RegularFile = strfs.RegularFile{
|
|
||||||
FileContent: content,
|
|
||||||
FileName: "helloworld.html",
|
|
||||||
FileModTime: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
|
|
||||||
}
|
|
||||||
|
|
||||||
var file fs.FS = ®ularfile
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
23
content.go
23
content.go
|
@ -18,10 +18,9 @@ import (
|
||||||
// var regularfile strfs.RegularFile = strfs.RegularFile{
|
// var regularfile strfs.RegularFile = strfs.RegularFile{
|
||||||
// FileContent: content,
|
// FileContent: content,
|
||||||
// FileName: "notice.html",
|
// FileName: "notice.html",
|
||||||
// FileModTime: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
|
// FileModTIme: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
|
||||||
// }
|
// }
|
||||||
type Content struct{
|
type Content struct{
|
||||||
value string
|
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
size int64
|
size int64
|
||||||
closed bool
|
closed bool
|
||||||
|
@ -40,14 +39,13 @@ var _ io.ReadCloser = &Content{}
|
||||||
// var regularfile strfs.RegularFile = strfs.RegularFile{
|
// var regularfile strfs.RegularFile = strfs.RegularFile{
|
||||||
// FileContent: content,
|
// FileContent: content,
|
||||||
// FileName: "message.md",
|
// FileName: "message.md",
|
||||||
// FileModTime: time.Now(),
|
// FileModTIme: time.Now(),
|
||||||
// }
|
// }
|
||||||
func CreateContent(value string) Content {
|
func CreateContent(s string) Content {
|
||||||
var reader io.Reader = strings.NewReader(value)
|
var reader io.Reader = strings.NewReader(s)
|
||||||
var size int64 = int64(len(value))
|
var size int64 = int64(len(s))
|
||||||
|
|
||||||
return Content{
|
return Content{
|
||||||
value:value,
|
|
||||||
reader:reader,
|
reader:reader,
|
||||||
size:size,
|
size:size,
|
||||||
}
|
}
|
||||||
|
@ -157,14 +155,3 @@ func (receiver *Content) Size() int64 {
|
||||||
|
|
||||||
return receiver.size
|
return receiver.size
|
||||||
}
|
}
|
||||||
|
|
||||||
// String retusn the value of the string that strfs.Content is wrapping.
|
|
||||||
//
|
|
||||||
// String makes *strfs.Content fit the fmt.Stringer interface.
|
|
||||||
func (receiver *Content) String() string {
|
|
||||||
if nil == receiver {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return receiver.value
|
|
||||||
}
|
|
||||||
|
|
13
errors.go
13
errors.go
|
@ -1,14 +1,13 @@
|
||||||
package strfs
|
package strfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sourcecode.social/reiver/go-erorr"
|
"github.com/reiver/go-fck"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
errClosed = erorr.Error("closed")
|
errClosed = fck.Error("closed")
|
||||||
errEmptyContent = erorr.Error("empty content")
|
errInternalError = fck.Error("internal error")
|
||||||
errInternalError = erorr.Error("internal error")
|
errNilByteSlice = fck.Error("nil byte slice")
|
||||||
errNilByteSlice = erorr.Error("nil byte slice")
|
errNilReader = fck.Error("nil reader")
|
||||||
errNilReader = erorr.Error("nil reader")
|
errNilReceiver = fck.Error("nil receiver")
|
||||||
errNilReceiver = erorr.Error("nil receiver")
|
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type internalFileInfo struct {
|
type internalFileInfo struct {
|
||||||
sys string
|
|
||||||
mode fs.FileMode
|
mode fs.FileMode
|
||||||
modtime time.Time
|
modtime time.Time
|
||||||
name string
|
name string
|
||||||
|
@ -36,5 +35,5 @@ func (receiver internalFileInfo) Size() int64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (receiver internalFileInfo) Sys() any {
|
func (receiver internalFileInfo) Sys() any {
|
||||||
return receiver.sys
|
return nil
|
||||||
}
|
}
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -1,5 +0,0 @@
|
||||||
module sourcecode.social/reiver/go-strfs
|
|
||||||
|
|
||||||
go 1.18
|
|
||||||
|
|
||||||
require sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 // indirect
|
|
2
go.sum
2
go.sum
|
@ -1,2 +0,0 @@
|
||||||
sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 h1:wpnz4JicQBLWrgGphYBls7DysIFCcnWgDz/vce/sY8E=
|
|
||||||
sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1/go.mod h1:NFtd7fzEf0r6A6R7JXYZfayRhPaJy0zt/18VWoLzrxA=
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
// var regularfile strfs.RegularFile = strfs.RegularFile{
|
// var regularfile strfs.RegularFile = strfs.RegularFile{
|
||||||
// FileContent: content,
|
// FileContent: content,
|
||||||
// FileName: "helloworld.html",
|
// FileName: "helloworld.html",
|
||||||
// FileModTime: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
|
// FileModTIme: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
|
||||||
// }
|
// }
|
||||||
type RegularFile struct {
|
type RegularFile struct {
|
||||||
FileContent Content
|
FileContent Content
|
||||||
|
@ -89,14 +89,9 @@ func (receiver *RegularFile) Stat() (fs.FileInfo, error) {
|
||||||
return nil, errNilReceiver
|
return nil, errNilReceiver
|
||||||
}
|
}
|
||||||
|
|
||||||
if EmptyContent() == receiver.FileContent {
|
|
||||||
return nil, errEmptyContent
|
|
||||||
}
|
|
||||||
|
|
||||||
const modeRegularFile = 0
|
const modeRegularFile = 0
|
||||||
|
|
||||||
return internalFileInfo{
|
return internalFileInfo{
|
||||||
sys: receiver.FileContent.String(),
|
|
||||||
name: receiver.FileName,
|
name: receiver.FileName,
|
||||||
size: receiver.FileContent.Size(),
|
size: receiver.FileContent.Size(),
|
||||||
mode: modeRegularFile,
|
mode: modeRegularFile,
|
||||||
|
|
|
@ -1,265 +0,0 @@
|
||||||
package strfs_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sourcecode.social/reiver/go-strfs"
|
|
||||||
|
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestRegularFile(t *testing.T) {
|
|
||||||
|
|
||||||
tests := []struct{
|
|
||||||
FileContent string
|
|
||||||
FileName string
|
|
||||||
FileModTime time.Time
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
FileContent: "",
|
|
||||||
FileName: "empty.txt",
|
|
||||||
FileModTime: time.Now(),
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{
|
|
||||||
FileContent: "once",
|
|
||||||
FileName: "file1.txt",
|
|
||||||
FileModTime: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
FileContent: "once twice",
|
|
||||||
FileName: "file2.html",
|
|
||||||
FileModTime: time.Date(1984, 01, 14, 9, 10, 11, 12, time.Local),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
FileContent: "once twice thrice",
|
|
||||||
FileName: "file3.gmni",
|
|
||||||
FileModTime: time.Date(1974, 12, 18, 4, 5, 6, 7, time.Local),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
FileContent: "once twice thrice fource",
|
|
||||||
FileName: "file4.fngr",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for testNumber, test := range tests {
|
|
||||||
|
|
||||||
var regularfile strfs.RegularFile
|
|
||||||
{
|
|
||||||
fileinfo, err := regularfile.Stat()
|
|
||||||
if nil == err {
|
|
||||||
t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if expected, actual := "empty content", err.Error(); expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual error is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED ERROR: %q", expected)
|
|
||||||
t.Logf("ACTUAL ERR: %q", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if nil != fileinfo {
|
|
||||||
t.Errorf("For test #%d, expected returned fileinfo to be nil but actually wasn't.", testNumber)
|
|
||||||
t.Logf("FILEINFO: %#v", fileinfo)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
var b [1]byte
|
|
||||||
var p []byte = b[:]
|
|
||||||
|
|
||||||
n, err := regularfile.Read(p)
|
|
||||||
if expected, actual := 0, n; expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual number-of-bytes read is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED NUMBER-BYTES-READ: %d", expected)
|
|
||||||
t.Logf("ACTUAL NUMBER-BYTES-READ: %d", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if nil == err {
|
|
||||||
t.Errorf("For test #%d, expected an error but did not actually get one.", testNumber)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if expected, actual := "closed", err.Error(); expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual error is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED ERROR: %q", expected)
|
|
||||||
t.Logf("ACTUAL ERR: %q", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
regularfile = strfs.RegularFile{
|
|
||||||
FileContent: strfs.CreateContent(test.FileContent),
|
|
||||||
FileName: test.FileName,
|
|
||||||
FileModTime: test.FileModTime,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var file fs.File = ®ularfile
|
|
||||||
if nil == file {
|
|
||||||
t.Errorf("For test #%d, did not expect file to be nil but actually was.", testNumber)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
actualBytes, err := io.ReadAll(file)
|
|
||||||
if nil != err {
|
|
||||||
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
|
|
||||||
t.Logf("ERROR: (%T) %s", err, err)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var actual string = string(actualBytes)
|
|
||||||
var expected string = test.FileContent
|
|
||||||
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual file-content is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED FILE-CONTENT: %q", expected)
|
|
||||||
t.Logf("ACTUAL FILE-CONTENT: %q", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileinfo fs.FileInfo
|
|
||||||
{
|
|
||||||
var err error
|
|
||||||
|
|
||||||
fileinfo, err = file.Stat()
|
|
||||||
if nil != err {
|
|
||||||
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
|
|
||||||
t.Logf("ERROR: (%T) %s", err, err)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if nil == fileinfo {
|
|
||||||
t.Errorf("For test #%d, did not expect file-info to be nil but actually was.", testNumber)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
var expected string = test.FileName
|
|
||||||
var actual string = fileinfo.Name()
|
|
||||||
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual file-name is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED FILE-NAME: %q", expected)
|
|
||||||
t.Logf("ACTUAL FILE-NAME: %q", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
var expected int64 = int64(len(test.FileContent))
|
|
||||||
var actual int64 = fileinfo.Size()
|
|
||||||
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual file-size is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED FILE-SIZE: %d", expected)
|
|
||||||
t.Logf("ACTUAL FILE-SIZE: %d", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
const modeRegularFile = 0
|
|
||||||
|
|
||||||
var expected fs.FileMode = modeRegularFile
|
|
||||||
var actual fs.FileMode = fileinfo.Mode()
|
|
||||||
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual file-mode is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED FILE-MODE: %d", expected)
|
|
||||||
t.Logf("ACTUAL FILE-MODE: %d", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
var expected bool = false
|
|
||||||
var actual bool = fileinfo.IsDir()
|
|
||||||
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual file-is-directory is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED FILE-MODE: %t", expected)
|
|
||||||
t.Logf("ACTUAL FILE-MODE: %t", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
var expected time.Time = test.FileModTime
|
|
||||||
var actual time.Time = fileinfo.ModTime()
|
|
||||||
|
|
||||||
if !expected.Equal(actual) {
|
|
||||||
t.Errorf("For test #%d, the actual mod-time is not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED FILE-MOD-TIME: %v", expected)
|
|
||||||
t.Logf("ACTUAL FILE-MOD-TIME: %v", actual)
|
|
||||||
t.Logf("REGULARFILE-NAME: %q", test.FileName)
|
|
||||||
t.Logf("REGULARFILE-MODTIME: %v", test.FileModTime)
|
|
||||||
t.Logf("REGULARFILE-CONTENT: %q", test.FileContent)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
var expected string = test.FileContent
|
|
||||||
var actual string = fileinfo.Sys().(string)
|
|
||||||
|
|
||||||
if expected != actual {
|
|
||||||
t.Errorf("For test #%d, the actual value for sys was not what was expected.", testNumber)
|
|
||||||
t.Logf("EXPECTED SYS: %q", expected)
|
|
||||||
t.Logf("ACTUAL SYS: %q", actual)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue