loader

package
v0.0.0-...-c4f0eec Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2014 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package loader abstracts the top-level xslate package from the job of loading the bytecode from a key value.

Index

Constants

View Source
const (
	MaskDumpByteCode = 1 << iota
	MaskDumpAST
)

Mask... set of constants are used as flags to denote Debug modes. If you're using these you should be reading the source code

Variables

View Source
var ErrAbsolutePathNotAllowed = errors.New("error: Absolute paths are not allowed")

ErrAbsolutePathNotAllowed is returned when the given path is not a relative path. As of this writing, Xslate does not allow you to load templates by absolute path, but this probably should be configurable

View Source
var ErrCacheMiss = errors.New("cache miss")

ErrCacheMiss is returned when the bytecode could not be found in the cache

View Source
var ErrTemplateNotFound = errors.New("error: Specified template was not found")

ErrTemplateNotFound is returned whenever one of the loaders failed to find a suitable template

Functions

This section is empty.

Types

type ByteCodeLoader

type ByteCodeLoader interface {
	DebugDumper
	LoadString(string, string) (*vm.ByteCode, error)
	Load(string) (*vm.ByteCode, error)
}

ByteCodeLoader defines the interface for objects that can load ByteCode specified by a key

type Cache

type Cache interface {
	Get(string) (*CacheEntity, error)
	Set(string, *CacheEntity) error
	Delete(string) error
}

Cache defines the interface for things that can cache generated ByteCode

type CacheEntity

type CacheEntity struct {
	ByteCode *vm.ByteCode
	Source   TemplateSource
}

CacheEntity contains all the othings required to perform calculations necessary to validate a template

type CacheStrategy

type CacheStrategy int

CacheStrategy specifies how the cache should be checked

const (
	// CacheNone flag specifies that cache checking and setting hould be skipped
	CacheNone CacheStrategy = iota
	// CacheVerify flag specifies that cached ByteCode generation time should be
	// verified against the source's last modified time. If new, the source is
	// re-parsed and re-compiled even on a cache hit.
	CacheVerify
	// CacheNoVerify flag specifies that if we have a cache hit, the ByteCode
	// is not verified against the source. If there's a cache hit, it is
	// used regardless of updates to the original template on file system
	CacheNoVerify
)

type CachedByteCodeLoader

type CachedByteCodeLoader struct {
	*StringByteCodeLoader // gives us LoadString
	*ReaderByteCodeLoader // gives us LoadReader
	Fetcher               TemplateFetcher
	Caches                []Cache
	CacheLevel            CacheStrategy
}

CachedByteCodeLoader is the default ByteCodeLoader that loads templates from the file system and caches in the file system, too

func NewCachedByteCodeLoader

func NewCachedByteCodeLoader(
	cache Cache,
	cacheLevel CacheStrategy,
	fetcher TemplateFetcher,
	parser parser.Parser,
	compiler compiler.Compiler,
) *CachedByteCodeLoader

NewCachedByteCodeLoader creates a new CachedByteCodeLoader

func (*CachedByteCodeLoader) DumpAST

func (l *CachedByteCodeLoader) DumpAST(v bool)

func (*CachedByteCodeLoader) DumpByteCode

func (l *CachedByteCodeLoader) DumpByteCode(v bool)

func (*CachedByteCodeLoader) Load

func (l *CachedByteCodeLoader) Load(key string) (bc *vm.ByteCode, err error)

Load loads the ByteCode for template specified by `key`, which, for this ByteCodeLoader, is the path to the template we want. If cached vm.ByteCode struct is found, it is loaded and its last modified time is compared against that of the template file. If the template is newer, it's compiled. Otherwise the cached version is used, saving us the time to parse and compile the template.

func (*CachedByteCodeLoader) ShouldDumpAST

func (l *CachedByteCodeLoader) ShouldDumpAST() bool

func (*CachedByteCodeLoader) ShouldDumpByteCode

func (l *CachedByteCodeLoader) ShouldDumpByteCode() bool

type DebugDumper

type DebugDumper interface {
	DumpAST(bool)
	DumpByteCode(bool)
	ShouldDumpAST() bool
	ShouldDumpByteCode() bool
}

DebugDumper defines interface that an object able to dump debug informatin during load time must fulfill

type FileCache

type FileCache struct {
	Dir string
}

FileCache is Cache implementation that stores caches in the file system

func NewFileCache

func NewFileCache(dir string) (*FileCache, error)

NewFileCache creates a new FileCache which stores caches underneath the directory specified by `dir`

func (*FileCache) Delete

func (c *FileCache) Delete(key string) error

Delete deletes the cache

func (*FileCache) Get

func (c *FileCache) Get(key string) (*CacheEntity, error)

Get returns the cached vm.ByteCode, if available

func (*FileCache) GetCachePath

func (c *FileCache) GetCachePath(key string) string

GetCachePath creates a string describing where a given template key would be cached in the file system

func (*FileCache) Set

func (c *FileCache) Set(key string, entity *CacheEntity) error

Set creates a new cache file to store the ByteCode.

type FileSource

type FileSource struct {
	Path           string
	LastStat       time.Time
	LastStatResult os.FileInfo
}

FileSource is a TemplateSource variant that holds template information in a file.

func NewFileSource

func NewFileSource(path string) *FileSource

NewFileSource creates a new FileSource

func (*FileSource) Bytes

func (s *FileSource) Bytes() ([]byte, error)

Bytes returns the bytes in teh template file

func (*FileSource) LastModified

func (s *FileSource) LastModified() (time.Time, error)

LastModified returns time when the target template file was last modified

func (*FileSource) Reader

func (s *FileSource) Reader() (io.Reader, error)

Reader returns the io.Reader instance for the file source

type FileTemplateFetcher

type FileTemplateFetcher struct {
	Paths []string
}

FileTemplateFetcher is a TemplateFetcher that loads template strings in the file system.

func NewFileTemplateFetcher

func NewFileTemplateFetcher(paths []string) (*FileTemplateFetcher, error)

NewFileTemplateFetcher creates a new struct. `paths` must give us the directories for us to look the templates in

func (*FileTemplateFetcher) FetchTemplate

func (l *FileTemplateFetcher) FetchTemplate(path string) (TemplateSource, error)

FetchTemplate returns a TemplateSource representing the template at path `path`. Paths are searched relative to the paths given to NewFileTemplateFetcher()

type Flags

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

Flags holds the flags to indicate if certain debug operations should be performed during load time

func NewFlags

func NewFlags() *Flags

NewFlags creates a new Flags struct initialized to 0

func (*Flags) DumpAST

func (f *Flags) DumpAST(b bool)

DumpAST sets the bitmask for DumpAST debug flag

func (*Flags) DumpByteCode

func (f *Flags) DumpByteCode(b bool)

DumpByteCode sets the bitmask for DumpByteCode debug flag

func (*Flags) ShouldDumpAST

func (f *Flags) ShouldDumpAST() bool

ShouldDumpAST returns true if the DumpAST debug flag is set

func (Flags) ShouldDumpByteCode

func (f Flags) ShouldDumpByteCode() bool

ShouldDumpByteCode returns true if the DumpByteCode debug flag is set

type HTTPSource

type HTTPSource struct {
	Buffer           *bytes.Buffer
	LastModifiedTime time.Time
}

HTTPSource represents a template source fetched via HTTP

func NewHTTPSource

func NewHTTPSource(r *http.Response) (*HTTPSource, error)

NewHTTPSource creates a new HTTPSource instance

func (*HTTPSource) Bytes

func (s *HTTPSource) Bytes() ([]byte, error)

Bytes returns the bytes in the template file

func (*HTTPSource) LastModified

func (s *HTTPSource) LastModified() (time.Time, error)

LastModified returns the last modified date of this template

func (*HTTPSource) Reader

func (s *HTTPSource) Reader() (io.Reader, error)

Reader returns the io.Reader for the template

type HTTPTemplateFetcher

type HTTPTemplateFetcher struct {
	URLs []string
}

HTTPTemplateFetcher is a proof of concept loader that fetches templates from external http servers. Probably not a good thing to use in your production environment

func NewHTTPTemplateFetcher

func NewHTTPTemplateFetcher(urls []string) (*HTTPTemplateFetcher, error)

NewHTTPTemplateFetcher creates a new struct. `urls` must give us the base HTTP urls for us to look the templates in (note: do not use trailing slashes)

func (*HTTPTemplateFetcher) FetchTemplate

func (l *HTTPTemplateFetcher) FetchTemplate(path string) (TemplateSource, error)

FetchTemplate returns a TemplateSource representing the template at path `path`. Paths are searched relative to the urls given to NewHTTPTemplateFetcher()

type MemoryCache

type MemoryCache map[string]*CacheEntity

MemoryCache is what's used store cached ByteCode in memory for maximum speed. As of this writing this cache never freed. We may need to introduce LRU in the future

func (MemoryCache) Delete

func (c MemoryCache) Delete(key string) error

Delete deletes the ByteCode

func (MemoryCache) Get

func (c MemoryCache) Get(key string) (*CacheEntity, error)

Get returns the cached ByteCode

func (MemoryCache) Set

func (c MemoryCache) Set(key string, bc *CacheEntity) error

Set stores the ByteCode

type ReaderByteCodeLoader

type ReaderByteCodeLoader struct {
	*Flags
	Parser   parser.Parser
	Compiler compiler.Compiler
}

ReaderByteCodeLoader is a fancy name for objects that can "given a template string, parse and compile it". This is one of the most common operations that users want to do, but it needs to be separate from other loaders because there's no sane way to cache intermediate results, and therefore has significant performance penalty

func NewReaderByteCodeLoader

func NewReaderByteCodeLoader(p parser.Parser, c compiler.Compiler) *ReaderByteCodeLoader

NewReaderByteCodeLoader creates a new object

func (*ReaderByteCodeLoader) LoadReader

func (l *ReaderByteCodeLoader) LoadReader(name string, rdr io.Reader) (*vm.ByteCode, error)

LoadReader takes a io.Reader and compiles it into vm.ByteCode

type StringByteCodeLoader

type StringByteCodeLoader struct {
	*Flags
	Parser   parser.Parser
	Compiler compiler.Compiler
}

StringByteCodeLoader is a fancy name for objects that can "given a template string, parse and compile it". This is one of the most common operations that users want to do, but it needs to be separate from other loaders because there's no sane way to cache intermediate results, and therefore has significant performance penalty

func NewStringByteCodeLoader

func NewStringByteCodeLoader(p parser.Parser, c compiler.Compiler) *StringByteCodeLoader

NewStringByteCodeLoader creates a new object

func (*StringByteCodeLoader) LoadString

func (l *StringByteCodeLoader) LoadString(name string, template string) (*vm.ByteCode, error)

LoadString takes a template string and compiles it into vm.ByteCode

type TemplateFetcher

type TemplateFetcher interface {
	FetchTemplate(string) (TemplateSource, error)
}

TemplateFetcher defines the interface for objects that can load TemplateSource specified by a key

type TemplateSource

type TemplateSource interface {
	LastModified() (time.Time, error)
	Bytes() ([]byte, error)
	Reader() (io.Reader, error)
}

TemplateSource is an abstraction over the actual template, which may live on a file system, cache, database, whatever. It needs to be able to give us the actual template string AND its last modified time

Jump to

Keyboard shortcuts

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