diff --git a/default.go b/default.go index e710fdb..5aa6755 100644 --- a/default.go +++ b/default.go @@ -152,35 +152,28 @@ func init() { return reflect.ValueOf(&intsRanger{from: from, to: to}) })), "dump": reflect.ValueOf(Func(func(a Arguments) (result reflect.Value) { - a.RequireNumOfArguments("dump", 0, -1) - noOfArgs := a.NumOfArguments() - // no arguments were provided, dump all; do not recurse over parents - if noOfArgs == 0 { + switch numArgs := a.NumOfArguments(); numArgs { + case 0: + // no arguments were provided, dump all; do not recurse over parents return dumpAll(a, 0) - } - //// some arguments were provided, grab them - args := make([]reflect.Value, noOfArgs) - ids := make([]string, noOfArgs) - for i := 0; i < a.NumOfArguments(); i++ { - args[i] = a.Get(i) - } - - // did we use a dump(int) in our template? - if len(args) == 1 && args[0].Kind() == reflect.Float64 { - return dumpAll(a, int(args[0].Float())) - } - - // at this moment, all arguments should be strings, otherwise we have a problem - for _, arg := range args { - if arg.Kind() != reflect.String { - // TODO: maybe this should panic? - return reflect.ValueOf("dump: err: you sent more then one argument, and some of them aren't strings, do not know what to do.") + case 1: + if arg := a.Get(0); arg.Kind() == reflect.Float64 { + // dump all, maybe walk into parents + return dumpAll(a, int(arg.Float())) + } + fallthrough + default: + // one or more arguments were provided, grab them and check they are all strings + ids := make([]string, numArgs) + for i := range ids { + arg := a.Get(i) + if arg.Kind() != reflect.String { + panic(fmt.Errorf("dump: expected argument %d to be a string, but got a %T", i, arg.Interface())) + } + ids = append(ids, arg.String()) } - ids = append(ids, arg.String()) + return dumpIdentified(a.runtime, ids) } - - return dumpIdentified(a.runtime, ids) - })), } } diff --git a/docs/builtins.md b/docs/builtins.md index 87fc61d..683d878 100644 --- a/docs/builtins.md +++ b/docs/builtins.md @@ -1,19 +1,18 @@ # Built-ins -- [Built-ins](#built-ins) - - [Functions](#functions) - - [From Go](#from-go) - - [len](#len) - - [isset](#isset) - - [exec](#exec) - - [ints](#ints) - - [dump](#dump) - - [SafeWriter](#safewriter) - - [safeHtml](#safehtml) - - [safeJs](#safejs) - - [raw/unsafe](#rawunsafe) - - [Renderer](#renderer) - - [writeJson](#writejson) +- [Functions](#functions) + - [From Go](#from-go) + - [len](#len) + - [isset](#isset) + - [exec](#exec) + - [ints](#ints) + - [dump](#dump) +- [SafeWriter](#safewriter) + - [safeHtml](#safehtml) + - [safeJs](#safejs) + - [raw/unsafe](#rawunsafe) +- [Renderer](#renderer) + - [writeJson](#writejson) ## Functions @@ -55,15 +54,15 @@ It panics if you pass a value of any type other than string, array, slice, map, ### dump -`dump` is meant to support template development, and can be used to print out variables, blocks, context, and globals that are available to the template. +`dump` is meant to aid in template development, and can be used to print out variables, blocks, context, and globals that are available to the template. The function can be used in three forms: -`dump()` used without parameters will printout context, variables, globals, and blocks (in this order) in current scope, without accessing any parent. +`dump()` used without parameters will print out context, variables, globals, and blocks (in this order) in the current scope, without accessing any parent. -`dump(levels)` - where `levels` is an **integer** - is the same as `dump()`, and additionally will recurse over context parents to the maximum of `levels`. -For example, `dump(1)` will additionaly print out all variables accessible to the parent of current context. +`dump(levels)` - where `levels` is an **integer** - is the same as `dump()`, but will additionally recurse over context parents to the maximum depth of `levels`. +For example, `dump(1)` will additionaly print out all variables accessible in the direct parent of the current context. -`dump("name1","name2", ....)` will try to find the variable or block with the given name(s) in the current runtime. +`dump("name1", "name2", ...)` will search for the variable and/or block with the given name(s) in any scope (current and all parents) of the current runtime. ## SafeWriter diff --git a/func.go b/func.go index f704ee1..dae185f 100644 --- a/func.go +++ b/func.go @@ -74,10 +74,7 @@ func (a *Arguments) RequireNumOfArguments(funcname string, min, max int) { // NumOfArguments returns the number of arguments func (a *Arguments) NumOfArguments() int { num := len(a.args.Exprs) - if a.args.HasPipeSlot { - return num - } - if a.pipedVal != nil { + if a.pipedVal != nil && !a.args.HasPipeSlot { return num + 1 } return num diff --git a/testData/devdump.jet b/testData/devdump.jet index a177e7c..74baac3 100644 --- a/testData/devdump.jet +++ b/testData/devdump.jet @@ -11,7 +11,7 @@ ------------------------------------- dump with depth of 2 {{ dump(2) }} ------------------------------------- dump with erroneous use -{{ dump(1,"m") }} +{{ try }} {{ dump(1,"m") }} {{ catch err }} {{- err.Error() -}} {{ end }} ------------------------------------- dump named {{ dump("mainMenu", "m") }} done @@ -56,7 +56,7 @@ Blocks: block mainMenu(type="text",label="main"), from /devdump.jet ------------------------------------- dump with erroneous use -dump: err: you sent more then one argument, and some of them aren't strings, do not know what to do. +dump: expected argument 0 to be a string, but got a float64 ------------------------------------- dump named mainMenu:="a variable, not a block!" // string block mainMenu(type="text",label="main"), from /devdump.jet