Skip to content

Commit

Permalink
clean up dump() switching on number of args
Browse files Browse the repository at this point in the history
and make it panic when used wrong (it's a dev function...)
  • Loading branch information
sauerbraten committed Mar 5, 2021
1 parent f5d696f commit fc3b0ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 51 deletions.
45 changes: 19 additions & 26 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})),
}
}
Expand Down
37 changes: 18 additions & 19 deletions docs/builtins.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions testData/devdump.jet
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fc3b0ef

Please sign in to comment.