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
import (
"io/fs"
iofs "io/fs"
)
var ErrNotExist = fs.ErrNotExist
type FS interface {
Metadata() Metadata
Open(name string) (File, error)
}
type File interface {
fs.File
iofs.File
Metadata() Metadata
}
type wrapperFS struct {
fs.FS
metadata Metadata
immutable bool
}
// Alias for "io/fs"
var (
ErrInvalid = iofs.ErrInvalid // "invalid argument"
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 {
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{})
}
// Alias for "io/fs"
func FormatDirEntry(dir DirEntry) string { return iofs.FormatDirEntry(dir) }
return &wrapperFS{
FS: f,
metadata: m,
immutable: i,
}
}
// Alias for "io/fs"
func FormatFileInfo(info FileInfo) string { return iofs.FormatFileInfo(info) }
func (f *wrapperFS) Metadata() Metadata {
return f.metadata
}
// TODO: func Glob(fsys FS, pattern string) (matches []string, err error) { return iofs.Glob(fsys, pattern) }
// TODO: func ReadFile(fsys FS, name string) ([]byte, error) {return iofs.ReadFile(fsys, name)}
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
}
// Alias for "io/fs"
func ValidPath(name string) bool { return iofs.ValidPath(name) }
type wrapperFile struct {
fs.File
metadata Metadata
}
// TODO: func WalkDir(fsys FS, root string, fn WalkDirFunc) error { return iofs.WalkDir(fsys, root, fn) }
func FsFile(f fs.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,
}
}
func (f *wrapperFile) Metadata() Metadata {
return f.metadata
}
// Alias for "io/fs"
type (
DirEntry = iofs.DirEntry
FileInfo = iofs.FileInfo
FileMode = iofs.FileMode
GlobFS = iofs.GlobFS
PathError = iofs.PathError
ReadDirFS = iofs.ReadDirFS
ReadDirFile = iofs.ReadDirFile
ReadFileFS = iofs.ReadFileFS
StatFS = iofs.StatFS
SubFS = iofs.StatFS
WalkDirFunc = iofs.WalkDirFunc
)

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
}