strfs

package module
v0.0.0-...-a22d8df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 5 Imported by: 1

README

go-strfs

Package strfs provides a virtual file-system, whre a fs.File can be created from a Go string.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-strfs

GoDoc

Example fs.File

Here is an example of turning a Go string into a fs.File:

import "github.com/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 = &regularfile

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

type Content struct {
	// contains filtered or unexported fields
}

Content represents the content part of a file.

Content does NOT map to anything in Go's built-in "fs" package.

But, for example, is used to create a RegularFile (which maps to a fs.File).

Example usage:

var content strfs.Content = strfs.CreateContent("<!DOCTYPE html>"+"\n"+"<html></html>")

var regularfile strfs.RegularFile = strfs.RegularFile{
	FileContent: content,
	FileName:    "notice.html",
	FileModTime: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
}

func CreateContent

func CreateContent(value string) Content

CreateContent returns a strfs.Content whose content is the string given to it.

Example usage:

var content strfs.Content = strfs.CreateContent("# Hello world!"+"\r\r"+"Welcome to my document."+"\n")

var regularfile strfs.RegularFile = strfs.RegularFile{
	FileContent: content,
	FileName:    "message.md",
	FileModTime: time.Now(),
}

func EmptyContent

func EmptyContent() Content

EmptyContent is used to see if a given strfs.Content is empty.

Note that a strfs.Content being empty is NOT the same as containing the empty string!

A strfs.Content is empty when it hasn't been initialized.

Example usage:

var content strfs.Content

// ...

if strfs.EmptyContent() == content {
	//@TODO
}

func (*Content) Close

func (receiver *Content) Close() error

Close will stop the Read method from working.

Close can safely be called more than once.

Close makes strfs.Content fit the io.Closer interface.

func (*Content) Closed

func (receiver *Content) Closed() bool

Closed returns whether a strfs.Content is closed or not.

func (*Content) Read

func (receiver *Content) Read(p []byte) (int, error)

Read reads up to len(p) bytes into 'p'. Read returns the number of bytes actually read, and any errors it encountered.

Read makes strfs.Content fit the io.Reader interface.

Example usage:

var content strfs.Content = strfs.CreateContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

var b1 [5]byte
var p1 []byte = b1[:]

n, err := content.Read(p1)

// n == 5
// b1 == [5]byte{'A', 'B', 'C', 'D', 'E'}

// ...

var b2 [4]byte
var p2 []byte = b2[:]

n, err := content.Read(p2)

// n == 4
// b2 == [5]byte{'F', 'G', 'H', 'I'}

func (*Content) Size

func (receiver *Content) Size() int64

Size returns the of the strnig given to it as the number of bytes.

func (*Content) String

func (receiver *Content) String() string

String retusn the value of the string that strfs.Content is wrapping.

String makes *strfs.Content fit the fmt.Stringer interface.

type RegularFile

type RegularFile struct {
	FileContent Content
	FileName    string
	FileModTime time.Time
}

RegularFile lets you turn a string into a fs.File.

Example usage:

var content strfs.Content = strfs.CreateContent("<!DOCTYPE html>"+"\n"+"<html><body>Hello world!</body></html>")

var regularfile strfs.RegularFile = strfs.RegularFile{
	FileContent: content,
	FileName:    "helloworld.html",
	FileModTime: time.Date(2022, 12, 12, 10, 30, 14, 2, time.UTC),
}

func (*RegularFile) Close

func (receiver *RegularFile) Close() error

Close will stop the Read method from working.

Close can safely be called more than once.

Close helps strfs.RegularFile fit the fs.File interface. Close makes strfs.RegularFile fit the io.Closer interface.

func (*RegularFile) Read

func (receiver *RegularFile) Read(p []byte) (int, error)

Read reads up to len(p) bytes into 'p'. Read returns the number of bytes actually read, and any errors it encountered.

Read helps strfs.RegularFile fit the fs.File interface. Read makes strfs.Content fit the io.Reader interface.

Example usage:

var content strfs.Content = strfs.CreateContent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

var regularfile strfs.RegularFile = strfs.RegularFile{
	FileContent: content,
	FileName: "alphabet.txt",
	FileModTime: time.Now(),
}

var b1 [5]byte
var p1 []byte = b1[:]

n, err := regularfile.Read(p1)

// n == 5
// b1 == [5]byte{'A', 'B', 'C', 'D', 'E'}

// ...

var b2 [4]byte
var p2 []byte = b2[:]

n, err := regularfile.Read(p2)

// n == 4
// b2 == [5]byte{'F', 'G', 'H', 'I'}

func (*RegularFile) Stat

func (receiver *RegularFile) Stat() (fs.FileInfo, error)

Stat returns a fs.FileInfo for a *strfs.RegularFile.

Stat helps strfs.RegularFile fit the fs.File interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL