Exemplo n.º 1
0
// For single values, Add will add (using the + operator) the addend to the existing addend (if found).
// Supports numeric values and strings.
//
// If the first add for a key is an array or slice, then the next value(s) will be appended.
func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
	var newVal interface{}
	existingAddend, found := c.values[key]
	if found {
		var err error

		addendV := reflect.ValueOf(existingAddend)

		if addendV.Kind() == reflect.Slice || addendV.Kind() == reflect.Array {
			nav := reflect.ValueOf(newAddend)
			if nav.Kind() == reflect.Slice || nav.Kind() == reflect.Array {
				newVal = reflect.AppendSlice(addendV, nav).Interface()
			} else {
				newVal = reflect.Append(addendV, nav).Interface()
			}
		} else {
			newVal, err = helpers.DoArithmetic(existingAddend, newAddend, '+')
			if err != nil {
				return "", err
			}
		}
	} else {
		newVal = newAddend
	}
	c.values[key] = newVal
	return "", nil // have to return something to make it work with the Go templates
}
Exemplo n.º 2
0
// Add will add (using the + operator) the addend to the existing addend (if found).
// Supports numeric values and strings.
func (c *Scratch) Add(key string, newAddend interface{}) (string, error) {
	var newVal interface{}
	existingAddend, found := c.values[key]
	if found {
		var err error
		newVal, err = helpers.DoArithmetic(existingAddend, newAddend, '+')
		if err != nil {
			return "", err
		}
	} else {
		newVal = newAddend
	}
	c.values[key] = newVal
	return "", nil // have to return something to make it work with the Go templates
}
Exemplo n.º 3
0
func init() {
	funcMap = template.FuncMap{
		"absURL":       func(a string) template.HTML { return template.HTML(helpers.AbsURL(a)) },
		"add":          func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '+') },
		"after":        after,
		"apply":        apply,
		"base64Decode": base64Decode,
		"base64Encode": base64Encode,
		"chomp":        chomp,
		"countrunes":   countRunes,
		"countwords":   countWords,
		"default":      dfault,
		"dateFormat":   dateFormat,
		"delimit":      delimit,
		"dict":         dictionary,
		"div":          func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '/') },
		"echoParam":    returnWhenSet,
		"emojify":      emojify,
		"eq":           eq,
		"findRE":       findRE,
		"first":        first,
		"ge":           ge,
		"getCSV":       getCSV,
		"getJSON":      getJSON,
		"getenv":       func(varName string) string { return os.Getenv(varName) },
		"gt":           gt,
		"hasPrefix":    func(a, b string) bool { return strings.HasPrefix(a, b) },
		"highlight":    highlight,
		"humanize":     humanize,
		"in":           in,
		"index":        index,
		"int":          func(v interface{}) int { return cast.ToInt(v) },
		"intersect":    intersect,
		"isSet":        isSet,
		"isset":        isSet,
		"jsonify":      jsonify,
		"last":         last,
		"le":           le,
		"lower":        func(a string) string { return strings.ToLower(a) },
		"lt":           lt,
		"markdownify":  markdownify,
		"md5":          md5,
		"mod":          mod,
		"modBool":      modBool,
		"mul":          func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '*') },
		"ne":           ne,
		"partial":      partial,
		"plainify":     plainify,
		"pluralize":    pluralize,
		"querify":      querify,
		"readDir":      readDirFromWorkingDir,
		"readFile":     readFileFromWorkingDir,
		"ref":          ref,
		"relURL":       func(a string) template.HTML { return template.HTML(helpers.RelURL(a)) },
		"relref":       relRef,
		"replace":      replace,
		"replaceRE":    replaceRE,
		"safeCSS":      safeCSS,
		"safeHTML":     safeHTML,
		"safeHTMLAttr": safeHTMLAttr,
		"safeJS":       safeJS,
		"safeURL":      safeURL,
		"sanitizeURL":  helpers.SanitizeURL,
		"sanitizeurl":  helpers.SanitizeURL,
		"seq":          helpers.Seq,
		"sha1":         sha1,
		"shuffle":      shuffle,
		"singularize":  singularize,
		"slice":        slice,
		"slicestr":     slicestr,
		"sort":         sortSeq,
		"split":        split,
		"string":       func(v interface{}) string { return cast.ToString(v) },
		"sub":          func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '-') },
		"substr":       substr,
		"title":        func(a string) string { return strings.Title(a) },
		"trim":         trim,
		"upper":        func(a string) string { return strings.ToUpper(a) },
		"urlize":       helpers.URLize,
		"where":        where,
	}
}