libloader

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package libloader provides a flexible library loading system for Scriptling. It supports chaining multiple loaders together, allowing libraries to be loaded from various sources (filesystem, API, memory, etc.) in a prioritized order.

The package follows Python's module loading conventions, supporting both:

  • Folder structure: libs/knot/groups.py → import knot.groups (preferred)
  • Flat structure: libs/knot.groups.py → import knot.groups (legacy)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

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

Chain tries multiple loaders in sequence until one succeeds. Loaders are tried in the order they are added.

func NewChain

func NewChain(loaders ...LibraryLoader) *Chain

NewChain creates a new loader chain with the given loaders. Loaders are tried in the order provided.

func (*Chain) Add

func (c *Chain) Add(loader LibraryLoader)

Add appends a loader to the end of the chain.

func (*Chain) Description

func (c *Chain) Description() string

Description returns a description of all loaders in the chain.

func (*Chain) Load

func (c *Chain) Load(name string) (string, bool, error)

Load tries each loader in sequence until one finds the library. Returns the first successful result, or (nil, false, nil) if no loader found it. Returns an error immediately if any loader encounters an error.

func (*Chain) Loaders

func (c *Chain) Loaders() []LibraryLoader

Loaders returns the list of loaders in the chain.

type FilesystemLoader

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

FilesystemLoader loads libraries from the filesystem. It supports Python-style folder structure for nested modules:

  • libs/knot/groups.py → import knot.groups (preferred)
  • libs/knot.groups.py → import knot.groups (legacy fallback)

The loader checks the folder structure first, then falls back to flat files.

func NewFilesystem

func NewFilesystem(baseDir string, opts ...FilesystemOption) *FilesystemLoader

NewFilesystem creates a new filesystem loader. The baseDir is the root directory to search for libraries.

Example:

loader := NewFilesystem("/app/libs")
// Will load:
//   import json          -> /app/libs/json.py
//   import knot.groups   -> /app/libs/knot/groups.py (preferred)
//                          -> /app/libs/knot.groups.py (fallback)

func (*FilesystemLoader) BaseDir

func (l *FilesystemLoader) BaseDir() string

BaseDir returns the base directory this loader searches.

func (*FilesystemLoader) Description

func (l *FilesystemLoader) Description() string

Description returns a description of this loader.

func (*FilesystemLoader) Extension

func (l *FilesystemLoader) Extension() string

Extension returns the file extension being used.

func (*FilesystemLoader) Load

func (l *FilesystemLoader) Load(name string) (string, bool, error)

Load attempts to load a library from the filesystem. It tries the folder structure first, then falls back to flat files.

For a library name "knot.groups", it checks:

  1. baseDir/knot/groups.py (folder structure - Python style)
  2. baseDir/knot.groups.py (flat structure - legacy support)

Returns (source, true, nil) if found, ("", false, nil) if not found, or ("", false, error) on filesystem errors.

type FilesystemOption

type FilesystemOption func(*FilesystemLoader)

FilesystemOption configures a FilesystemLoader.

func WithDescription

func WithDescription(desc string) FilesystemOption

WithDescription sets a custom description for the loader.

func WithExtension

func WithExtension(ext string) FilesystemOption

WithExtension sets a custom file extension (default: ".py").

func WithFollowLinks(follow bool) FilesystemOption

WithFollowLinks enables or disables following symbolic links (default: true).

type FuncLoader

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

FuncLoader is a loader that uses a function to load libraries. Useful for simple custom loaders without implementing the full interface.

func NewFuncLoader

func NewFuncLoader(fn func(name string) (string, bool, error), description string) *FuncLoader

NewFuncLoader creates a loader from a function.

func (*FuncLoader) Description

func (f *FuncLoader) Description() string

Description returns the loader description.

func (*FuncLoader) Load

func (f *FuncLoader) Load(name string) (string, bool, error)

Load calls the wrapped function.

type LibraryLoader

type LibraryLoader interface {
	// Load attempts to load a library by name.
	// Returns the library source code, whether it was found, and any error.
	// If the library is not found, returns (nil, false, nil).
	// If there's an error (e.g., network failure), returns (nil, false, error).
	Load(name string) (source string, found bool, err error)

	// Description returns a human-readable description of this loader.
	// Used for debugging and logging.
	Description() string
}

LibraryLoader attempts to load a library by name. Implementations can load from various sources: filesystem, API, memory, etc.

type MemoryLoader

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

MemoryLoader loads libraries from an in-memory map. Useful for testing and for registering libraries programmatically.

func NewMemoryLoader

func NewMemoryLoader(libraries map[string]string) *MemoryLoader

NewMemoryLoader creates a new memory loader with the given libraries.

func NewMemoryLoaderWithDescription

func NewMemoryLoaderWithDescription(libraries map[string]string, description string) *MemoryLoader

NewMemoryLoaderWithDescription creates a memory loader with a custom description.

func (*MemoryLoader) Description

func (m *MemoryLoader) Description() string

Description returns the loader description.

func (*MemoryLoader) Load

func (m *MemoryLoader) Load(name string) (string, bool, error)

Load returns the library source if it exists in memory.

func (*MemoryLoader) Remove

func (m *MemoryLoader) Remove(name string)

Remove removes a library from memory.

func (*MemoryLoader) Set

func (m *MemoryLoader) Set(name, source string)

Set adds or updates a library in memory.

type MultiFilesystemLoader

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

MultiFilesystemLoader loads from multiple directories in order. Useful for having a user library directory that overrides system libraries.

func NewMultiFilesystem

func NewMultiFilesystem(dirs ...string) *MultiFilesystemLoader

NewMultiFilesystem creates a loader that searches multiple directories. Directories are searched in the order provided.

Example:

loader := NewMultiFilesystem("/app/user/libs", "/app/system/libs")
// Will check user libs first, then fall back to system libs

func (*MultiFilesystemLoader) AddDir

func (m *MultiFilesystemLoader) AddDir(dir string)

AddDir adds another directory to search (appended to the end).

func (*MultiFilesystemLoader) Description

func (m *MultiFilesystemLoader) Description() string

Description returns a description of this loader.

func (*MultiFilesystemLoader) Load

func (m *MultiFilesystemLoader) Load(name string) (string, bool, error)

Load tries each directory in order until the library is found.

Jump to

Keyboard shortcuts

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