github.com/coveo/gotemplate@v2.7.7+incompatible/template/add_funcs.go (about) 1 package template 2 3 import ( 4 "sort" 5 "sync" 6 7 "github.com/coveo/gotemplate/utils" 8 ) 9 10 const ( 11 goTemplateBase = "Base go template functions" 12 ) 13 14 var addFuncMutex sync.Mutex 15 16 // Add additional functions to the go template context 17 func (t *Template) addFuncs() { 18 // We cannot create functions table in multiple threads concurrently 19 addFuncMutex.Lock() 20 defer addFuncMutex.Unlock() 21 22 if baseGoTemplateFuncs == nil { 23 baseGoTemplateFuncs = make(funcTableMap, len(baseGoTemplate)) 24 for key, val := range baseGoTemplate { 25 baseGoTemplateFuncs[key] = FuncInfo{ 26 group: goTemplateBase, 27 description: val.description, 28 in: val.args, 29 out: val.out, 30 } 31 } 32 } 33 t.addFunctions(baseGoTemplateFuncs) 34 35 add := func(o Options, f func()) { 36 if t.options[o] { 37 f() 38 } 39 } 40 41 add(Sprig, t.addSprigFuncs) 42 add(Math, t.addMathFuncs) 43 add(Data, t.addDataFuncs) 44 add(Logging, t.addLoggingFuncs) 45 add(Runtime, t.addRuntimeFuncs) 46 add(Utils, t.addUtilsFuncs) 47 add(Net, t.addNetFuncs) 48 add(OS, t.addOSFuncs) 49 } 50 51 // Apply all regular expressions replacements to the supplied string 52 func (t Template) substitute(content string) string { 53 return utils.Substitute(content, t.substitutes...) 54 } 55 56 // List the available template names 57 func (t Template) getTemplateNames() []string { 58 templates := t.Templates() 59 result := make([]string, len(templates)) 60 for i := range templates { 61 result[i] = templates[i].Name() 62 } 63 sort.Strings(result) 64 return result 65 } 66 67 var baseGoTemplateFuncs funcTableMap 68 69 var baseGoTemplate = map[string]struct { 70 description, args, out string 71 }{ 72 "and": { 73 `Returns the boolean AND of its arguments by returning the first empty argument or the last argument, that is, "and x y" behaves as "if x then y else x". All the arguments are evaluated.`, 74 `arg0 reflect.Value, args ...reflect.Value`, `reflect.Value`, 75 }, 76 "call": { 77 `Returns the result of calling the first argument, which must be a function, with the remaining arguments as parameters. Thus "call .X.Y 1 2" is, in Go notation, dot.X.Y(1, 2) where Y is a func-valued field, map entry, or the like. The first argument must be the result of an evaluation that yields a value of function type (as distinct from a predefined function such as print). The function must return either one or two result values, the second of which is of type error. If the arguments don't match the function or the returned error value is non-nil, execution stops.`, 78 `fn reflect.Value, args ...reflect.Value`, `reflect.Value, error)`, 79 }, 80 "html": { 81 `Returns the escaped HTML equivalent of the textual representation of its arguments. This function is unavailable in html/template, with a few exceptions.`, 82 `args ...interface{}`, `string`, 83 }, 84 "index": { 85 `Returns the result of indexing its first argument by the following arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each indexed item must be a map, slice, or array.`, 86 `item reflect.Value, indices ...reflect.Value`, `(reflect.Value, error)`, 87 }, 88 "js": { 89 `Returns the escaped JavaScript equivalent of the textual representation of its arguments.`, 90 `args ...interface{}`, `string`, 91 }, 92 "len": { 93 `Returns the integer length of its argument.`, 94 `item interface{}`, `(int, error)`, 95 }, 96 "not": { 97 `Returns the boolean negation of its single argument.`, 98 `not(arg reflect.Value`, `bool`, 99 }, 100 "or": { 101 `Returns the boolean OR of its arguments by returning the first non-empty argument or the last argument, that is, "or x y" behaves as "if x then x else y". All the arguments are evaluated.`, 102 `or(arg0 reflect.Value, args ...reflect.Value`, `reflect.Value`, 103 }, 104 "print": { 105 `An alias for fmt.Sprint`, 106 `args ...interface{}`, `string`, 107 }, 108 "printf": { 109 `An alias for fmt.Sprintf`, 110 `format string, args ...interface{}`, `string`, 111 }, 112 "println": { 113 `An alias for fmt.Sprintln`, 114 `args ...interface{}`, `string`, 115 }, 116 "urlquery": { 117 `Returns the escaped value of the textual representation of its arguments in a form suitable for embedding in a URL query. This function is unavailable in html/template, with a few exceptions.`, 118 `args ...interface{}`, `string`, 119 }, 120 121 "eq": { 122 `Returns the boolean truth of arg1 == arg2`, 123 `arg1 reflect.Value, arg2 ...reflect.Value`, `(bool, error)`, 124 }, 125 "ge": { 126 `Returns the boolean truth of arg1 >= arg2`, 127 `arg1 reflect.Value, arg2 ...reflect.Value`, `(bool, error)`, 128 }, 129 "gt": { 130 `Returns the boolean truth of arg1 > arg2`, 131 `arg1 reflect.Value, arg2 ...reflect.Value`, `(bool, error)`, 132 }, 133 "le": { 134 `Returns the boolean truth of arg1 <= arg2`, 135 `arg1 reflect.Value, arg2 ...reflect.Value`, `(bool, error)`, 136 }, 137 "lt": { 138 `Returns the boolean truth of arg1 < arg2`, 139 `arg1 reflect.Value, arg2 ...reflect.Value`, `(bool, error)`, 140 }, 141 "ne": { 142 `Returns the boolean truth of arg1 != arg2`, 143 `arg1 reflect.Value, arg2 ...reflect.Value`, `(bool, error)`, 144 }, 145 }