mirror of
https://github.com/danog/liquid.git
synced 2025-01-22 14:11:25 +01:00
Improve some internal names
This commit is contained in:
parent
dd41a36de3
commit
1da9d4072e
@ -46,7 +46,7 @@ func render(b []byte, filename string) {
|
||||
tpl.SetSourcePath(filename)
|
||||
out, err := tpl.Render(map[string]interface{}{})
|
||||
exitIfErr(err)
|
||||
os.Stdout.Write(out) // nolint: gas
|
||||
os.Stdout.Write(out) // nolint: gas, errcheck
|
||||
}
|
||||
|
||||
func usage(error bool) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Package generics defines methods such as sorting, comparison, and type conversion, that apply to interface types.
|
||||
// Package evaluator defines methods such as sorting, comparison, and type conversion, that apply to interface types.
|
||||
//
|
||||
// It is similar to, and makes heavy use of, the reflect package.
|
||||
//
|
||||
@ -8,19 +8,9 @@
|
||||
package evaluator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// GenericError is an error regarding generic conversion.
|
||||
type GenericError string
|
||||
|
||||
func (e GenericError) Error() string { return string(e) }
|
||||
|
||||
func genericErrorf(format string, a ...interface{}) error {
|
||||
return GenericError(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
// Contains returns a boolean indicating whether array is a sequence that contains item.
|
||||
func Contains(array interface{}, item interface{}) bool {
|
||||
item = ToLiquid(item)
|
||||
@ -36,29 +26,6 @@ func Contains(array interface{}, item interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
|
||||
func IsEmpty(value interface{}) bool {
|
||||
value = ToLiquid(value)
|
||||
if value == nil {
|
||||
return false
|
||||
}
|
||||
r := reflect.ValueOf(value)
|
||||
switch r.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return r.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !r.Bool()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsTrue returns a bool indicating whether the value is true according to Liquid semantics.
|
||||
func IsTrue(value interface{}) bool {
|
||||
value = ToLiquid(value)
|
||||
return value != nil && value != false
|
||||
}
|
||||
|
||||
// Length returns the length of a string or array. In keeping with Liquid semantics,
|
||||
// and contra Go, it does not return the size of a map.
|
||||
func Length(value interface{}) int {
|
@ -15,7 +15,8 @@ type drop interface {
|
||||
type TypeError string
|
||||
|
||||
func (e TypeError) Error() string { return string(e) }
|
||||
func typeError(format string, a ...interface{}) TypeError {
|
||||
|
||||
func typeErrorf(format string, a ...interface{}) TypeError {
|
||||
return TypeError(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
@ -39,7 +40,7 @@ func conversionError(modifier string, value interface{}, typ reflect.Type) error
|
||||
case reflect.Value:
|
||||
value = ref.Interface()
|
||||
}
|
||||
return genericErrorf("can't convert %s%T(%v) to type %s", modifier, value, value, typ)
|
||||
return typeErrorf("can't convert %s%T(%v) to type %s", modifier, value, value, typ)
|
||||
}
|
||||
|
||||
// Convert value to the type. This is a more aggressive conversion, that will
|
||||
@ -140,7 +141,7 @@ func MustConvert(value interface{}, t reflect.Type) interface{} {
|
||||
func MustConvertItem(item interface{}, array []interface{}) interface{} {
|
||||
item, err := Convert(item, reflect.TypeOf(array).Elem())
|
||||
if err != nil {
|
||||
panic(typeError("can't convert %#v to %s: %s", item, reflect.TypeOf(array).Elem(), err))
|
||||
panic(typeErrorf("can't convert %#v to %s: %s", item, reflect.TypeOf(array).Elem(), err))
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
26
evaluator/predicates.go
Normal file
26
evaluator/predicates.go
Normal file
@ -0,0 +1,26 @@
|
||||
package evaluator
|
||||
|
||||
import "reflect"
|
||||
|
||||
// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
|
||||
func IsEmpty(value interface{}) bool {
|
||||
value = ToLiquid(value)
|
||||
if value == nil {
|
||||
return false
|
||||
}
|
||||
r := reflect.ValueOf(value)
|
||||
switch r.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return r.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !r.Bool()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsTrue returns a bool indicating whether the value is true according to Liquid semantics.
|
||||
func IsTrue(value interface{}) bool {
|
||||
value = ToLiquid(value)
|
||||
return value != nil && value != false
|
||||
}
|
@ -40,7 +40,7 @@ func (e expression) Evaluate(ctx Context) (out interface{}, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
switch e := r.(type) {
|
||||
case evaluator.GenericError:
|
||||
case evaluator.TypeError:
|
||||
err = e
|
||||
case InterpreterError:
|
||||
err = e
|
||||
|
@ -51,7 +51,7 @@ func (c renderContext) EvaluateString(source string) (out interface{}, err error
|
||||
err = e
|
||||
default:
|
||||
// fmt.Println(string(debug.Stack()))
|
||||
panic(Error("%s during evaluation of %s", e, source))
|
||||
panic(Errorf("%s during evaluation of %s", e, source))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
@ -11,7 +11,8 @@ import (
|
||||
type ParseError string
|
||||
|
||||
func (e ParseError) Error() string { return string(e) }
|
||||
func parseError(format string, a ...interface{}) ParseError {
|
||||
|
||||
func parseErrorf(format string, a ...interface{}) ParseError {
|
||||
return ParseError(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
@ -77,7 +78,7 @@ func (s Config) parseChunks(chunks []Chunk) (ASTNode, error) { // nolint: gocycl
|
||||
if sd != nil {
|
||||
suffix = "; immediate parent is " + sd.TagName()
|
||||
}
|
||||
return nil, parseError("%s not inside %s%s", c.Name, strings.Join(cs.ParentTags(), " or "), suffix)
|
||||
return nil, parseErrorf("%s not inside %s%s", c.Name, strings.Join(cs.ParentTags(), " or "), suffix)
|
||||
case cs.IsBlockStart():
|
||||
push := func() {
|
||||
stack = append(stack, frame{syntax: sd, node: bn, ap: ap})
|
||||
@ -107,12 +108,12 @@ func (s Config) parseChunks(chunks []Chunk) (ASTNode, error) { // nolint: gocycl
|
||||
}
|
||||
*ap = append(*ap, &ASTFunctional{c, f})
|
||||
} else {
|
||||
return nil, parseError("unknown tag: %s", c.Name)
|
||||
return nil, parseErrorf("unknown tag: %s", c.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
if bn != nil {
|
||||
return nil, parseError("unterminated %s tag at %s", bn.Name, bn.SourceInfo)
|
||||
return nil, parseErrorf("unterminated %s tag at %s", bn.Name, bn.SourceInfo)
|
||||
}
|
||||
if err := s.evaluateBuilders(root); err != nil {
|
||||
return nil, err
|
||||
|
@ -14,8 +14,8 @@ type renderError string
|
||||
|
||||
func (e renderError) Error() string { return string(e) }
|
||||
|
||||
// Error creates a render error.
|
||||
func Error(format string, a ...interface{}) renderError {
|
||||
// Errorf creates a render error.
|
||||
func Errorf(format string, a ...interface{}) renderError {
|
||||
return renderError(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
@ -57,21 +57,21 @@ func renderNode(node ASTNode, w io.Writer, ctx nodeContext) error { // nolint: g
|
||||
case *ASTBlock:
|
||||
cd, ok := ctx.config.findBlockDef(n.Name)
|
||||
if !ok || cd.parser == nil {
|
||||
return parseError("unknown tag: %s", n.Name)
|
||||
return parseErrorf("unknown tag: %s", n.Name)
|
||||
}
|
||||
renderer := n.renderer
|
||||
if renderer == nil {
|
||||
panic(parseError("unset renderer for %v", n))
|
||||
panic(parseErrorf("unset renderer for %v", n))
|
||||
}
|
||||
return renderer(w, renderContext{ctx, nil, n})
|
||||
case *ASTObject:
|
||||
value, err := ctx.Evaluate(n.expr)
|
||||
if err != nil {
|
||||
return parseError("%s in %s", err, n.Source)
|
||||
return parseErrorf("%s in %s", err, n.Source)
|
||||
}
|
||||
return writeObject(value, w)
|
||||
default:
|
||||
panic(parseError("unknown node type %T", node))
|
||||
panic(parseErrorf("unknown node type %T", node))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ func includeTag(source string) (func(io.Writer, render.Context) error, error) {
|
||||
}
|
||||
rel, ok := value.(string)
|
||||
if !ok {
|
||||
return render.Error("include requires a string argument; got %v", value)
|
||||
return render.Errorf("include requires a string argument; got %v", value)
|
||||
}
|
||||
filename := filepath.Join(filepath.Dir(ctx.SourceFile()), rel)
|
||||
s, err := ctx.RenderFile(filename, map[string]interface{}{})
|
||||
|
@ -8,8 +8,8 @@ import (
|
||||
"github.com/osteele/liquid/render"
|
||||
)
|
||||
|
||||
var errLoopContinueLoop = render.Error("continue outside a loop")
|
||||
var errLoopBreak = render.Error("break outside a loop")
|
||||
var errLoopContinueLoop = render.Errorf("continue outside a loop")
|
||||
var errLoopBreak = render.Errorf("break outside a loop")
|
||||
|
||||
func breakTag(parameters string) (func(io.Writer, render.Context) error, error) {
|
||||
return func(io.Writer, render.Context) error {
|
||||
|
Loading…
x
Reference in New Issue
Block a user