revert(blogo,fs): remove fs package, metadata is now a "superset" of fs.FS
This commit is contained in:
106
blogo/fs/fs.go
106
blogo/fs/fs.go
@@ -1,106 +0,0 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
// 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"
|
||||
)
|
||||
|
||||
// Provides access to a hierarchical file system, similar to [iofs.FS]. Implementations
|
||||
// can load files, fetch them from network, read from disk, as they are opened/on demand
|
||||
// if they seem fit to do so.
|
||||
//
|
||||
// A file system may implement additional interfaces such as [ReadFileFS].
|
||||
type FS interface {
|
||||
// Returns [Metadata] about the file system.
|
||||
//
|
||||
// Implementations should return a empty [Metadata] instead of a nil value for when
|
||||
// the file system doesn't have any additional metadata about it or when a error
|
||||
// occurs when getting said metadata.
|
||||
//
|
||||
// [plugin.Sourcer] may add prefixes to their metadata keys.
|
||||
Metadata() Metadata
|
||||
// Open, similar to [iofs.File.Open], opens the named file.
|
||||
//
|
||||
// When it returns an error, it should be of type [*PathError] with the Op field
|
||||
// set to "open", the Path field set to name, and the Err field describing the problem.
|
||||
//
|
||||
// It should reject attempts of opening names that do not satisfy [ValidPath], returning
|
||||
// a [*PathError] with Err set to [ErrInvalid] or [ErrNotExist]
|
||||
//
|
||||
// Implementations may find the file on demand or fetch them via http request, depending on
|
||||
// the underlying source of the file.
|
||||
Open(name string) (File, error)
|
||||
}
|
||||
|
||||
// Provides access to a single file, similar to [iofs.FS]. Implementations may read the file
|
||||
// on demand and/or fetch it's contents and data from a outside source such as via HTTP requests,
|
||||
// depending on the underlying source of the file.
|
||||
//
|
||||
// Directory files should also implement [ReadDirFile]. A file may implement [io.ReaderAt] or
|
||||
// [io.Seeker] as optimizations.
|
||||
type File interface {
|
||||
// Returns [Metadata] about the file.
|
||||
//
|
||||
// Implementations should return a empty [Metadata] instead of a nil value for when
|
||||
// the file doesn't have any additional metadata about it or when a error occurs while getting
|
||||
// said metadata.
|
||||
//
|
||||
// [plugin.Sourcer] may add prefixes to their metadata keys.
|
||||
Metadata() Metadata
|
||||
|
||||
Stat() (FileInfo, error)
|
||||
Read([]byte) (int, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Alias for "io/fs"
|
||||
func FormatDirEntry(dir DirEntry) string { return iofs.FormatDirEntry(dir) }
|
||||
|
||||
// Alias for "io/fs"
|
||||
func FormatFileInfo(info FileInfo) string { return iofs.FormatFileInfo(info) }
|
||||
|
||||
// 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)}
|
||||
|
||||
// Alias for "io/fs"
|
||||
func ValidPath(name string) bool { return iofs.ValidPath(name) }
|
||||
|
||||
// TODO: func WalkDir(fsys FS, root string, fn WalkDirFunc) error { return iofs.WalkDir(fsys, root, fn) }
|
||||
|
||||
// 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
|
||||
)
|
||||
@@ -1,82 +0,0 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
// Wraps the provided [iofs.FS] file system so it can be used as a file system for blogo.
|
||||
// [Metadata] from this [FS] will be empty, and by default, mutable.
|
||||
func FromIOFS(fsys iofs.FS, immutable ...bool) FS {
|
||||
if fsys == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
m := MetadataMap(map[string]any{})
|
||||
i := false
|
||||
if len(immutable) > 0 && immutable[0] {
|
||||
m = ImmutableMetadata(m)
|
||||
i = true
|
||||
}
|
||||
|
||||
return &wrapperFS{
|
||||
fsys: fsys,
|
||||
metadata: m,
|
||||
immutable: i,
|
||||
}
|
||||
}
|
||||
|
||||
type wrapperFS struct {
|
||||
fsys 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.fsys.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return FromIOFile(file, f.immutable), nil
|
||||
}
|
||||
|
||||
// Wraps the provided [iofs.File] so it can be used as a file system for blogo.
|
||||
// [Metadata] from this [File] will be empty, and by default, mutable.
|
||||
func FromIOFile(file iofs.File, immutable ...bool) File {
|
||||
m := MetadataMap(map[string]any{})
|
||||
if len(immutable) > 0 && immutable[0] {
|
||||
m = ImmutableMetadata(m)
|
||||
}
|
||||
|
||||
return &wrapperFile{
|
||||
File: file,
|
||||
metadata: m,
|
||||
}
|
||||
}
|
||||
|
||||
type wrapperFile struct {
|
||||
iofs.File
|
||||
metadata Metadata
|
||||
}
|
||||
|
||||
func (f *wrapperFile) Metadata() Metadata {
|
||||
return f.metadata
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
// 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 "errors"
|
||||
|
||||
var (
|
||||
ErrMetadataNotFound = errors.New("key in metadata was not found")
|
||||
ErrMetadataIncorrectType = errors.New("key in metadata is not of the provided type")
|
||||
ErrMetadataImmutable = errors.New("key in metadata cannot be set to another value")
|
||||
ErrMetadataNotEmpty = errors.New("key in metadata is not empty")
|
||||
)
|
||||
|
||||
type Metadata interface {
|
||||
Get(key string) (any, error)
|
||||
Set(key string, v any, strict ...bool) error
|
||||
Delete(key string, strict ...bool) error
|
||||
}
|
||||
|
||||
type metadataMap map[string]any
|
||||
|
||||
func MetadataMap(m map[string]any) Metadata {
|
||||
if m == nil {
|
||||
m = map[string]any{}
|
||||
}
|
||||
return metadataMap(m)
|
||||
}
|
||||
|
||||
func (m metadataMap) Get(key string) (any, error) {
|
||||
v, ok := m[key]
|
||||
if !ok {
|
||||
return nil, ErrMetadataNotFound
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (m metadataMap) Set(key string, v any, strict ...bool) error {
|
||||
if _, ok := m[key]; ok && len(strict) > 0 && strict[0] {
|
||||
return ErrMetadataNotEmpty
|
||||
}
|
||||
m[key] = v
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m metadataMap) Delete(key string, strict ...bool) error {
|
||||
if _, ok := m[key]; ok && len(strict) > 0 && strict[0] {
|
||||
return ErrMetadataNotEmpty
|
||||
}
|
||||
delete(m, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
type joinedMetadata struct {
|
||||
ms []Metadata
|
||||
m Metadata
|
||||
}
|
||||
|
||||
func JoinMetadata(ms ...Metadata) Metadata {
|
||||
jm := []Metadata{}
|
||||
for _, m := range ms {
|
||||
if ms, ok := m.(*joinedMetadata); ok {
|
||||
jm = append(jm, ms.m)
|
||||
jm = append(jm, ms.ms...)
|
||||
} else if m != nil {
|
||||
jm = append(jm, m)
|
||||
}
|
||||
}
|
||||
return &joinedMetadata{
|
||||
ms: jm,
|
||||
m: MetadataMap(map[string]any{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (jm *joinedMetadata) Get(key string) (any, error) {
|
||||
if v, err := jm.m.Get(key); err == nil {
|
||||
return v, nil
|
||||
}
|
||||
for _, m := range jm.ms {
|
||||
v, err := m.Get(key)
|
||||
if err == nil {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrMetadataNotEmpty
|
||||
}
|
||||
|
||||
func (jm *joinedMetadata) Set(key string, v any, strict ...bool) error {
|
||||
if _, err := jm.m.Get(key); err == nil {
|
||||
return jm.m.Set(key, v, strict...)
|
||||
}
|
||||
|
||||
for _, m := range jm.ms {
|
||||
_, err := m.Get(key)
|
||||
if err == nil {
|
||||
return m.Set(key, v, strict...)
|
||||
} else if errors.Is(err, ErrMetadataImmutable) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return jm.m.Set(key, v, strict...)
|
||||
}
|
||||
|
||||
func (jm *joinedMetadata) Delete(key string, strict ...bool) error {
|
||||
if _, err := jm.m.Get(key); err == nil {
|
||||
return jm.m.Delete(key, strict...)
|
||||
}
|
||||
|
||||
for _, m := range jm.ms {
|
||||
_, err := m.Get(key)
|
||||
if err == nil {
|
||||
return m.Delete(key, strict...)
|
||||
} else if errors.Is(err, ErrMetadataImmutable) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return jm.m.Delete(key, strict...)
|
||||
}
|
||||
|
||||
type immutableMetadata struct {
|
||||
Metadata
|
||||
}
|
||||
|
||||
func ImmutableMetadata(m Metadata) Metadata {
|
||||
return &immutableMetadata{m}
|
||||
}
|
||||
|
||||
func (m *immutableMetadata) Set(key string, v any, strict ...bool) error {
|
||||
return ErrMetadataImmutable
|
||||
}
|
||||
|
||||
func (m *immutableMetadata) Delete(key string, strict ...bool) error {
|
||||
return ErrMetadataImmutable
|
||||
}
|
||||
|
||||
type TypedMetadata struct {
|
||||
Metadata
|
||||
}
|
||||
|
||||
func NewTypedMetadata(m Metadata) *TypedMetadata {
|
||||
if tm, ok := m.(*TypedMetadata); ok {
|
||||
return tm
|
||||
}
|
||||
return &TypedMetadata{m}
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetBool(key string) (bool, error) {
|
||||
return GetTyped[bool](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetString(key string) (string, error) {
|
||||
return GetTyped[string](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetInt(key string) (int, error) {
|
||||
return GetTyped[int](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetInt8(key string) (int8, error) {
|
||||
return GetTyped[int8](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetInt16(key string) (int16, error) {
|
||||
return GetTyped[int16](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetInt32(key string) (int32, error) {
|
||||
return GetTyped[int32](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetInt64(key string) (int64, error) {
|
||||
return GetTyped[int64](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetUint(key string) (uint, error) {
|
||||
return GetTyped[uint](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetUint8(key string) (uint8, error) {
|
||||
return GetTyped[uint8](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetUint16(key string) (uint16, error) {
|
||||
return GetTyped[uint16](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetUint32(key string) (uint32, error) {
|
||||
return GetTyped[uint32](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetUint64(key string) (uint64, error) {
|
||||
return GetTyped[uint64](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetByte(key string) (byte, error) {
|
||||
return GetTyped[byte](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetRune(key string) (rune, error) {
|
||||
return GetTyped[rune](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetFloat32(key string) (float32, error) {
|
||||
return GetTyped[float32](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetFloat64(key string) (float64, error) {
|
||||
return GetTyped[float64](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetComplex64(key string) (complex64, error) {
|
||||
return GetTyped[complex64](m, key)
|
||||
}
|
||||
|
||||
func (m *TypedMetadata) GetComplex128(key string) (complex128, error) {
|
||||
return GetTyped[complex128](m, key)
|
||||
}
|
||||
|
||||
func GetTyped[T any](m Metadata, key string) (T, error) {
|
||||
var z T
|
||||
|
||||
v, err := m.Get(key)
|
||||
if err != nil {
|
||||
return z, err
|
||||
}
|
||||
|
||||
if v, ok := v.(T); ok {
|
||||
return v, nil
|
||||
} else {
|
||||
return z, ErrMetadataIncorrectType
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user