feat(blogo,fs): alises to "io/fs" functions and types

This commit is contained in:
Guz
2025-01-22 19:34:44 -03:00
parent f12d0cc66b
commit 3df712e1b3
2 changed files with 112 additions and 56 deletions

View File

@@ -16,76 +16,53 @@
package fs package fs
import ( import (
"io/fs" iofs "io/fs"
) )
var ErrNotExist = fs.ErrNotExist
type FS interface { type FS interface {
Metadata() Metadata Metadata() Metadata
Open(name string) (File, error) Open(name string) (File, error)
} }
type File interface { type File interface {
fs.File iofs.File
Metadata() Metadata Metadata() Metadata
} }
type wrapperFS struct { // Alias for "io/fs"
fs.FS var (
metadata Metadata ErrInvalid = iofs.ErrInvalid // "invalid argument"
immutable bool ErrPermission = iofs.ErrPermission // "permission denied"
} ErrExist = iofs.ErrExist // "file already exists"
ErrNotExist = iofs.ErrNotExist // "file does not exist"
ErrClosed = iofs.ErrClosed // "file already closed"
)
func FsFS(f fs.FS, immutable ...bool) FS { // Alias for "io/fs"
var m Metadata func FormatDirEntry(dir DirEntry) string { return iofs.FormatDirEntry(dir) }
var i bool
if len(immutable) > 0 && immutable[0] {
i = true
m = ImmutableMetadata(MetadataMap(map[string]any{}))
} else {
i = false
m = MetadataMap(map[string]any{})
}
return &wrapperFS{ // Alias for "io/fs"
FS: f, func FormatFileInfo(info FileInfo) string { return iofs.FormatFileInfo(info) }
metadata: m,
immutable: i,
}
}
func (f *wrapperFS) Metadata() Metadata { // TODO: func Glob(fsys FS, pattern string) (matches []string, err error) { return iofs.Glob(fsys, pattern) }
return f.metadata // TODO: func ReadFile(fsys FS, name string) ([]byte, error) {return iofs.ReadFile(fsys, name)}
}
func (f *wrapperFS) Open(name string) (File, error) { // Alias for "io/fs"
file, err := f.FS.Open(name) func ValidPath(name string) bool { return iofs.ValidPath(name) }
if err != nil {
return nil, err
}
return FsFile(file, f.immutable), nil
}
type wrapperFile struct { // TODO: func WalkDir(fsys FS, root string, fn WalkDirFunc) error { return iofs.WalkDir(fsys, root, fn) }
fs.File
metadata Metadata
}
func FsFile(f fs.File, immutable ...bool) File { // Alias for "io/fs"
var m Metadata type (
if len(immutable) > 0 && immutable[0] { DirEntry = iofs.DirEntry
m = ImmutableMetadata(MetadataMap(map[string]any{})) FileInfo = iofs.FileInfo
} else { FileMode = iofs.FileMode
m = MetadataMap(map[string]any{}) GlobFS = iofs.GlobFS
} PathError = iofs.PathError
ReadDirFS = iofs.ReadDirFS
return &wrapperFile{ ReadDirFile = iofs.ReadDirFile
File: f, ReadFileFS = iofs.ReadFileFS
metadata: m, StatFS = iofs.StatFS
} SubFS = iofs.StatFS
} WalkDirFunc = iofs.WalkDirFunc
)
func (f *wrapperFile) Metadata() Metadata {
return f.metadata
}

79
blogo/fs/iofs.go Normal file
View File

@@ -0,0 +1,79 @@
// Copyright 2025-present Gustavo "Guz" L. de Mello
// Copyright 2025-present The Lored.dev Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fs
import (
iofs "io/fs"
)
func FsFS(f iofs.FS, immutable ...bool) FS {
var m Metadata
var i bool
if len(immutable) > 0 && immutable[0] {
i = true
m = ImmutableMetadata(MetadataMap(map[string]any{}))
} else {
i = false
m = MetadataMap(map[string]any{})
}
return &wrapperFS{
FS: f,
metadata: m,
immutable: i,
}
}
type wrapperFS struct {
iofs.FS
metadata Metadata
immutable bool
}
func (f *wrapperFS) Metadata() Metadata {
return f.metadata
}
func (f *wrapperFS) Open(name string) (File, error) {
file, err := f.FS.Open(name)
if err != nil {
return nil, err
}
return FsFile(file, f.immutable), nil
}
func FsFile(f iofs.File, immutable ...bool) File {
var m Metadata
if len(immutable) > 0 && immutable[0] {
m = ImmutableMetadata(MetadataMap(map[string]any{}))
} else {
m = MetadataMap(map[string]any{})
}
return &wrapperFile{
File: f,
metadata: m,
}
}
type wrapperFile struct {
iofs.File
metadata Metadata
}
func (f *wrapperFile) Metadata() Metadata {
return f.metadata
}