load

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyLib = errors.New("directory contains no library files")

Functions

This section is empty.

Types

type BasicLoader

type BasicLoader struct {
	// MainReader reads and returns the main file located under the passed path.
	//
	// A return of (nil, nil) is valid and indicates the file wasn't found.
	MainReader func(path string) (*File, error)
	// TemplateReader reads and returns the template file located under the
	// passed module path.
	//
	// A return of (nil, nil) is valid and indicates the file wasn't found.
	TemplateReader func(extendingFile *file.File, path string) (*File, error)
	// IncludeReader reads and returns the file located under the passed path.
	//
	// It gets passed the file that includes it to help resolve path correctly.
	//
	// A return of (nil, nil) is valid and indicates the file wasn't found.
	IncludeReader func(includingFile *file.File, path string) (*File, error)
	// LibraryReader reads and returns the files of the library available under
	// the passed use path.
	//
	// It gets passed the file that uses it to help resolve version correctly.
	// If usingFile is nil, the library is loaded standalone and path should be
	// interpreted as an absolute path instead of a module path.
	//
	// A return of (nil, nil) is valid and indicates the library wasn't found.
	LibraryReader func(usingFile *file.File, path string) (*Library, error)
	// DirLibraryLoader loads the library in the directory of f, a
	// main, include, or template file, which is not yet linked or validated.
	//
	// A return of (nil, nil) is valid and indicates that there are no library
	// files in f's directory.
	DirLibraryLoader func(f *file.File) (*file.Library, error)

	// Parser is the function used to parse a file.
	Parser func(in []byte) (*file.File, error)
	// PreLinkValidator is the validator called after parsing and before
	// linking.
	PreLinkValidator func(*file.File) error
	// Linker links the given file recursively.
	//
	// Commonly, it utilizes this Loader to link the used libraries of f, f's
	// includes, and the template of f, if it has one.
	Linker func(f *file.File) error

	// LibraryLinker is the linker used to link a library.
	LibraryLinker func(lib *file.Library) error

	// FileValidator is the validator function called if a main file is loaded.
	//
	// It is expected that the function recursively validates all files the
	// passed file depends on.
	MainValidator func(*file.File) error
	// LibraryValidator is the validator called if a library is loaded with
	// link set to false.
	//
	// It is expected that the function recursively validates all files the
	// passed library depends on.
	LibraryValidator func(*file.Library) error
}

BasicLoader is a Loader implementation that allows configuration of every step of the loading process.

func (BasicLoader) LoadInclude

func (b BasicLoader) LoadInclude(includingFile *file.File, name string) (file.IncludeFile, error)

func (BasicLoader) LoadLibrary

func (b BasicLoader) LoadLibrary(usingFile *file.File, usePath string) (*file.Library, error)

func (BasicLoader) LoadMain

func (b BasicLoader) LoadMain(path string) (*file.File, error)

func (BasicLoader) LoadTemplate

func (b BasicLoader) LoadTemplate(extendingFile *file.File, extendPath string) (*file.File, error)

type CachingLoader

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

func Cache

func Cache(l Loader) *CachingLoader

Cache wraps the passed Loader and returns a load that caches results to the Loader's methods.

It also ensures that loads for the same path are only made once.

func (*CachingLoader) LoadDirLibrary

func (l *CachingLoader) LoadDirLibrary(f *file.File, get func() (*file.Library, error)) (*file.Library, error)

LoadDirLibrary is a special helper that allows caching of dir libraries.

To use it call with the file to load the dir library for and a getter function.

If the dir library for f is cached, it is returned. LoadDirLibrary considers a library from the same module with path.Base(f.PathInModule) as f's dir library.

If the dir library for f is not cached, get is called and expected to read parse, link, and validate the library. A return of (nil, nil) is valid, and indicates that there exists no dir library for f. get must not call any of the loaders other methods as to not cause a deadlock.

func (*CachingLoader) LoadInclude

func (l *CachingLoader) LoadInclude(includingFile *file.File, p string) (file.IncludeFile, error)

func (*CachingLoader) LoadLibrary

func (l *CachingLoader) LoadLibrary(usingFile *file.File, usePath string) (*file.Library, error)

func (*CachingLoader) LoadMain

func (l *CachingLoader) LoadMain(path string) (*file.File, error)

func (*CachingLoader) LoadTemplate

func (l *CachingLoader) LoadTemplate(extendingFile *file.File, extendPath string) (*file.File, error)

type File

type File struct {
	Name         string
	Module       string
	PathInModule string

	AbsolutePath string

	// IsCorgi indicates whether this is a corgi file, or not.
	//
	// Only relevant for include files.
	IsCorgi bool
	Raw     []byte
}

type Library

type Library struct {
	Module       string
	PathInModule string

	AbsolutePath string

	Files       []File
	Precompiled *file.Library // if set, no other fields need to be set
}

type Loader

type Loader interface {
	// LoadLibrary loads the library provided under the passed module path.
	//
	// It gets passed the file that uses it.
	//
	// If usingFile is nil, the library is loaded standalone and path should be
	// interpreted as an absolute path instead of a module path.
	// This happens, for example, when pre-compiling a library.
	//
	// A return of (nil, nil) is valid and indicates that the loader was unable
	// to find a library with the given path.
	LoadLibrary(usingFile *file.File, usePath string) (*file.Library, error)
	// LoadInclude loads the file available under the slash-separated path,
	// relative to the passed includingFile.
	//
	// A return of (nil, nil) is valid and indicates that the load was unable
	// to find a file that matches.
	LoadInclude(includingFile *file.File, path string) (file.IncludeFile, error)
	// LoadTemplate loads the template file associated with the given
	// module path.
	//
	// A return of (nil, nil) is valid and indicates that the load was unable
	// to find a file that matches.
	LoadTemplate(extendingFile *file.File, extendPath string) (*file.File, error)
	// LoadMain loads a main file available under the passed system path.
	//
	// A return of (nil, nil) is valid and indicates that the load was unable
	// to find a file that matches.
	LoadMain(path string) (*file.File, error)
}

Loader is an abstraction of where the Linker loads its files from.

It must be concurrently safe.

Jump to

Keyboard shortcuts

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