Documentation ¶
Overview ¶
Example (ForConfig) ¶
package main import ( "encoding/json" "io/fs" "io/ioutil" "log" "reflect" "github.com/laher/marshalfs" ) func main() { // Given a config which is usually loaded from a file, ... type myconfig struct { I int `json:"i"` S string `json:"s"` } // Here is the code under test // NOTE: production code would invoke it with os.DirFS // `config := loadConfig(os.DirFS("./config"))` var loadMyconfig = func(myfs fs.FS) (*myconfig, error) { f, err := myfs.Open("config.json") if err != nil { return nil, err } b, err := ioutil.ReadAll(f) if err != nil { return nil, err } c := &myconfig{} err = json.Unmarshal(b, c) return c, err } // Set up ... input := &myconfig{S: "string", I: 3} mfs, err := marshalfs.New(json.Marshal, marshalfs.FileSpecs{"config.json": marshalfs.NewFile(input)}) if err != nil { log.Fatalf("unexpected error: %v", err) } // Run the code output, err := loadMyconfig(mfs) // Verify file is loaded OK and content matches ... if err != nil { log.Fatalf("unexpected error: %v", err) } if !reflect.DeepEqual(input, output) { log.Fatal("loadConfig did not parse files as expected") } }
Output:
Index ¶
- Variables
- type FS
- func (mfs *FS) Glob(pattern string) ([]string, error)
- func (mfs *FS) Open(name string) (fs.File, error)
- func (mfs *FS) ReadDir(name string) ([]fs.DirEntry, error)
- func (mfs *FS) ReadFile(name string) ([]byte, error)
- func (mfs *FS) Remove(filename string)
- func (mfs *FS) ReplaceAll(files FileSpecs) error
- func (mfs *FS) SetFile(filename string, item FileSpec) error
- func (mfs *FS) Stat(name string) (fs.FileInfo, error)
- func (mfs *FS) Sub(dir string) (fs.FS, error)
- type FileCommon
- type FileOption
- type FileSpec
- type FileSpecs
- type MarshalFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrPathConflict = errors.New("path conflict")
Functions ¶
This section is empty.
Types ¶
type FS ¶
type FS struct {
// contains filtered or unexported fields
}
An FS is a simple filesystem backed by objects and some serialization function. Given that files are backed by objects, this is only writable using non-standard methods - see "SetFile"/"Remove"/"ReplaceAll"
func (*FS) ReplaceAll ¶ added in v0.8.0
type FileCommon ¶ added in v0.5.0
type FileOption ¶ added in v0.1.1
type FileOption func(*FileCommon)
func WithMarshaler ¶ added in v0.7.0
func WithMarshaler(mf MarshalFunc) FileOption
func WithModTime ¶ added in v0.1.1
func WithModTime(t time.Time) FileOption
func WithMode ¶ added in v0.1.1
func WithMode(mode fs.FileMode) FileOption
type FileSpec ¶ added in v0.7.0
type FileSpec interface { Common() FileCommon // contains filtered or unexported methods }
func NewFile ¶
func NewFile(value interface{}, opts ...FileOption) FileSpec
NewFile creates a new FileSpec
type MarshalFunc ¶
Click to show internal directories.
Click to hide internal directories.