core

package
v0.0.0-...-4f7e1e8 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2015 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

shared interfaces and utility functions

Index

Constants

View Source
const (
	OR LogicalOperator = iota
	AND
	UnknownLogical

	Equals ComparisonOperator = iota
	NotEquals
	LessThan
	GreaterThan
	LessThanOrEqual
	GreaterThanOrEqual
	Contains
	NotContains
	Unary
	NotUnary
	UnknownComparator

	String Type = iota
	Nil
	Int
	Int64
	Uint
	Float64
	Complex128
	Bool
	Time
	Today
	Array
	Unknown
)

Variables

View Source
var (
	EmptyBytes = []byte{}
	Whitespace = []byte(" ")
)
View Source
var BytePool = bytepool.New(128, 4096)
View Source
var FilterLookup = make(map[string]FilterFactory)

A map of filter names to filter factories

View Source
var (
	Now = func() time.Time { return time.Now() }
)
View Source
var TypeOperations = map[Type]map[ComparisonOperator]ConditionResolver{
	String: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(string) == right.(string) },
		LessThan: func(left, right interface{}) bool { return left.(string) < right.(string) },
	},
	Nil: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left == nil && right == nil },
		LessThan: func(left, right interface{}) bool { return false },
	},
	Int: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(int) == right.(int) },
		LessThan: func(left, right interface{}) bool { return left.(int) < right.(int) },
	},
	Int64: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(int64) == right.(int64) },
		LessThan: func(left, right interface{}) bool { return left.(int64) < right.(int64) },
	},
	Uint: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(uint) == right.(uint) },
		LessThan: func(left, right interface{}) bool { return left.(uint) < right.(uint) },
	},
	Float64: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(float64) == right.(float64) },
		LessThan: func(left, right interface{}) bool { return left.(float64) < right.(float64) },
	},
	Complex128: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(complex128) == right.(complex128) },
		LessThan: func(left, right interface{}) bool { return false },
	},
	Bool: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(bool) == right.(bool) },
		LessThan: func(left, right interface{}) bool { return false },
	},
	Time: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return left.(time.Time).Unix() == right.(time.Time).Unix() },
		LessThan: func(left, right interface{}) bool { return left.(time.Time).Unix() < right.(time.Time).Unix() },
	},
	Today: map[ComparisonOperator]ConditionResolver{
		Equals: func(left, right interface{}) bool {
			l, r := left.(time.Time), right.(time.Time)
			return l.YearDay() == r.YearDay() && l.Year() == r.Year()
		},
		LessThan: func(left, right interface{}) bool {
			l, r := left.(time.Time), right.(time.Time)
			if l.Year() > r.Year() {
				return false
			}
			if l.Year() < r.Year() {
				return true
			}
			return l.YearDay() < r.YearDay()
		},
	},
	Array: map[ComparisonOperator]ConditionResolver{
		Equals:   func(left, right interface{}) bool { return reflect.DeepEqual(left, right) },
		LessThan: func(left, right interface{}) bool { return reflect.ValueOf(left).Len() < reflect.ValueOf(right).Len() },
	},
}

Functions

func ContainsComparison

func ContainsComparison(left, right interface{}) bool

I think most of this sucks

func EqualsComparison

func EqualsComparison(left, right interface{}) bool

func GreaterThanComparison

func GreaterThanComparison(left, right interface{}) bool

func GreaterThanOrEqualComparison

func GreaterThanOrEqualComparison(left, right interface{}) bool

func IsTokenEnd

func IsTokenEnd(b byte) bool

func LessThanComparison

func LessThanComparison(left, right interface{}) bool

func LessThanOrEqualComparison

func LessThanOrEqualComparison(left, right interface{}) bool

func NotContainsComparison

func NotContainsComparison(left, right interface{}) bool

func NotEqualsComparison

func NotEqualsComparison(left, right interface{}) bool

func NotUnaryComparison

func NotUnaryComparison(left, right interface{}) bool

func RegisterFilter

func RegisterFilter(name string, factory FilterFactory)

Register's a filter for the given name (not thread-safe)

func Resolve

func Resolve(data interface{}, field string) interface{}

Resolves the value of field within data

func ResolveFinal

func ResolveFinal(value interface{}) interface{}

This is necessary in the case where the value references a struct directly:

template := "{{ user }}""
data := map[string]interface{}{"user": &User{"Leto"}},

Without this step, the above would result in a value which points to the User we need to resolve this a step further and get the value of "User" (which will either me the output of its String() method, or %v)

Of course, we only want this final resolution once we need the value. If we call this too early, say in Resolve above, we won't be able to build nested paths

func SkipSpaces

func SkipSpaces(data []byte) int

return the position of the first none space, or -1 if no white space exists

func ToBytes

func ToBytes(data interface{}) []byte

Convert arbitrary data to []byte

func ToInt

func ToInt(data interface{}) (int, bool)

Convert arbitrary data to string

func ToLength

func ToLength(input interface{}) (int, bool)

gets the length of string, map or array

func ToString

func ToString(data interface{}) string

Convert arbitrary data to string

func TrimStrings

func TrimStrings(values []string) []string

Since these templates are possibly long-lived, let's free up any space which was accumulated while we grew these arrays

func UnaryComparison

func UnaryComparison(left, right interface{}) bool

Types

type Cache

type Cache interface {
	Get(key string) Code
	Set(key string, template Code)
	Clear()
}

cache interface

type Code

type Code interface {
	Execute(writer io.Writer, data map[string]interface{}) ExecuteState
}

interface for something that can render itself

type ComparisonOperator

type ComparisonOperator int

type Completable

type Completable interface {
	Complete(value Value, operator ComparisonOperator)
	Verifiable
}

type Condition

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

represents a conditions (such as x == y)

func (*Condition) IsTrue

func (c *Condition) IsTrue(data map[string]interface{}) bool

type ConditionGroup

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

represents a group of conditions

func (ConditionGroup) Complete

func (g ConditionGroup) Complete(value Value, operator ComparisonOperator)

func (*ConditionGroup) Inverse

func (g *ConditionGroup) Inverse()

func (*ConditionGroup) IsTrue

func (g *ConditionGroup) IsTrue(data map[string]interface{}) bool

type ConditionResolver

type ConditionResolver func(left, right interface{}) bool

Resolves a condition

type Configuration

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

Configuration used for generating a template

func (*Configuration) Cache

func (c *Configuration) Cache(cache Cache) *Configuration

Set the caching engine, or nil for no caching

func (*Configuration) GetCache

func (c *Configuration) GetCache() Cache

Gets the configured cache

func (*Configuration) GetIncludeHandler

func (c *Configuration) GetIncludeHandler() IncludeHandler

Gets the configured include handler

func (*Configuration) GetPreserveWhitespace

func (c *Configuration) GetPreserveWhitespace() bool

Gets the preserves whitespace value

func (*Configuration) IncludeHandler

func (c *Configuration) IncludeHandler(handler IncludeHandler) *Configuration

Set the include handler

func (*Configuration) PreserveWhitespace

func (c *Configuration) PreserveWhitespace() *Configuration

Preserves whitespace

type DynamicValue

type DynamicValue struct {
	Fields []string
	// contains filtered or unexported fields
}

func NewDynamicValue

func NewDynamicValue(fields []string) *DynamicValue

func (*DynamicValue) Resolve

func (v *DynamicValue) Resolve(data map[string]interface{}) interface{}

func (*DynamicValue) ResolveWithNil

func (v *DynamicValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*DynamicValue) Underlying

func (v *DynamicValue) Underlying() interface{}

type ExecuteState

type ExecuteState int
const (
	Normal ExecuteState = iota
	Break
	Continue
)

type Filter

type Filter func(input interface{}, data map[string]interface{}) interface{}

An interface function

type FilterFactory

type FilterFactory func(parameters []Value) Filter

A filter factory creates a filter based on the supplied parameters

type IncludeHandler

type IncludeHandler func(name string, writer io.Writer, data map[string]interface{})

The callback to execute to resolve include tags If you're going to use name to read from the filesystem, beware of directory traversal.

type LogicalOperator

type LogicalOperator int

type MarkupType

type MarkupType int
const (
	OutputMarkup MarkupType = iota
	TagMarkup
	NoMarkup
)

type Parser

type Parser struct {
	Position int
	Data     []byte
	Len      int
	End      int
	Line     int
}

func NewParser

func NewParser(data []byte) *Parser

func (*Parser) Current

func (p *Parser) Current() byte

func (*Parser) Error

func (p *Parser) Error(s string) error

func (*Parser) Forward

func (p *Parser) Forward()

func (*Parser) ForwardBy

func (p *Parser) ForwardBy(count int)

func (*Parser) HasMore

func (p *Parser) HasMore() bool

func (*Parser) Left

func (p *Parser) Left() int

func (*Parser) Out

func (p *Parser) Out()

func (*Parser) Peek

func (p *Parser) Peek() byte

func (*Parser) ReadComparisonOperator

func (p *Parser) ReadComparisonOperator() ComparisonOperator

func (*Parser) ReadConditionGroup

func (p *Parser) ReadConditionGroup() (Verifiable, error)

func (*Parser) ReadDynamicValues

func (p *Parser) ReadDynamicValues() (Value, error)

func (*Parser) ReadFilters

func (p *Parser) ReadFilters() ([]Filter, error)

func (*Parser) ReadLogicalOperator

func (p *Parser) ReadLogicalOperator() LogicalOperator

func (*Parser) ReadName

func (p *Parser) ReadName() string

func (*Parser) ReadParameters

func (p *Parser) ReadParameters() ([]Value, error)

func (*Parser) ReadPartialCondition

func (p *Parser) ReadPartialCondition() (Completable, error)

func (*Parser) ReadRangeValue

func (p *Parser) ReadRangeValue() (Value, error)

func (*Parser) ReadStaticBoolValue

func (p *Parser) ReadStaticBoolValue() (Value, bool)

func (*Parser) ReadStaticEmptyValue

func (p *Parser) ReadStaticEmptyValue() (Value, bool)

func (*Parser) ReadStaticNumericValue

func (p *Parser) ReadStaticNumericValue() (Value, error)

func (*Parser) ReadStaticStringValue

func (p *Parser) ReadStaticStringValue(delimiter byte) (Value, error)

func (*Parser) ReadValue

func (p *Parser) ReadValue() (Value, error)

func (*Parser) SkipPastOutput

func (p *Parser) SkipPastOutput()

func (*Parser) SkipPastTag

func (p *Parser) SkipPastTag()

func (*Parser) SkipSpaces

func (p *Parser) SkipSpaces() (current byte)

func (*Parser) SkipUntil

func (p *Parser) SkipUntil(b byte) (current byte)

func (*Parser) Snapshot

func (p *Parser) Snapshot(start int) []byte

func (*Parser) ToMarkup

func (p *Parser) ToMarkup(preserveWhitespace bool) ([]byte, MarkupType)

type RangeValue

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

func (*RangeValue) Resolve

func (v *RangeValue) Resolve(data map[string]interface{}) interface{}

func (*RangeValue) ResolveWithNil

func (v *RangeValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*RangeValue) Underlying

func (v *RangeValue) Underlying() interface{}

type StaticBoolValue

type StaticBoolValue struct {
	Value bool
}

func (*StaticBoolValue) Resolve

func (v *StaticBoolValue) Resolve(data map[string]interface{}) interface{}

func (*StaticBoolValue) ResolveWithNil

func (v *StaticBoolValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*StaticBoolValue) Underlying

func (v *StaticBoolValue) Underlying() interface{}

type StaticEmptyValue

type StaticEmptyValue struct{}

func (*StaticEmptyValue) Resolve

func (v *StaticEmptyValue) Resolve(data map[string]interface{}) interface{}

func (*StaticEmptyValue) ResolveWithNil

func (v *StaticEmptyValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*StaticEmptyValue) Underlying

func (v *StaticEmptyValue) Underlying() interface{}

type StaticFloatValue

type StaticFloatValue struct {
	Value float64
}

func (*StaticFloatValue) Resolve

func (v *StaticFloatValue) Resolve(data map[string]interface{}) interface{}

func (*StaticFloatValue) ResolveWithNil

func (v *StaticFloatValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*StaticFloatValue) Underlying

func (v *StaticFloatValue) Underlying() interface{}

type StaticIntValue

type StaticIntValue struct {
	Value int
}

func (*StaticIntValue) Resolve

func (v *StaticIntValue) Resolve(data map[string]interface{}) interface{}

func (*StaticIntValue) ResolveWithNil

func (v *StaticIntValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*StaticIntValue) Underlying

func (v *StaticIntValue) Underlying() interface{}

type StaticStringValue

type StaticStringValue struct {
	Value string
}

func (*StaticStringValue) Resolve

func (v *StaticStringValue) Resolve(data map[string]interface{}) interface{}

func (*StaticStringValue) ResolveWithNil

func (v *StaticStringValue) ResolveWithNil(data map[string]interface{}) interface{}

func (*StaticStringValue) Underlying

func (v *StaticStringValue) Underlying() interface{}

type Tag

type Tag interface {
	AddCode(code Code)
	AddSibling(tag Tag) error
	Name() string
	Type() TagType
	Code
}

interface for an tag markup

type TagType

type TagType int

The type of tag

const (
	ContainerTag TagType = iota
	LoopTag
	EndTag
	SiblingTag
	StandaloneTag
	NoopTag
)

type TrueCondition

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

func (*TrueCondition) Inverse

func (t *TrueCondition) Inverse()

func (*TrueCondition) IsTrue

func (t *TrueCondition) IsTrue(data map[string]interface{}) bool

type Type

type Type int

type Value

type Value interface {
	Resolve(data map[string]interface{}) interface{}
	ResolveWithNil(data map[string]interface{}) interface{}
	Underlying() interface{}
}

Represents a value, which can either be static or dynamic

func TrimValues

func TrimValues(values []Value) []Value

Since these templates are possibly long-lived, let's free up any space which was accumulated while we grew these arrays

type Verifiable

type Verifiable interface {
	IsTrue(data map[string]interface{}) bool
	Inverse()
}

Jump to

Keyboard shortcuts

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