Esempio n. 1
0
func (r *ReplaceFilter) Replace(input interface{}, data map[string]interface{}) interface{} {
	target := core.ToBytes(r.target.Resolve(data))
	with := core.ToBytes(r.with.Resolve(data))

	switch typed := input.(type) {
	case string:
		return strings.Replace(typed, string(target), string(with), r.count)
	case []byte:
		return bytes.Replace(typed, target, with, r.count)
	default:
		return input
	}
}
Esempio n. 2
0
func (o *Output) Render(writer io.Writer, data map[string]interface{}) {
	value := o.Value.Resolve(data)
	if o.Filters != nil {
		for _, filter := range o.Filters {
			value = filter(value, data)
		}
	}
	writer.Write(core.ToBytes(value))
}
Esempio n. 3
0
// join elements of the array with certain character between them
func (f *JoinFilter) Join(input interface{}, data map[string]interface{}) interface{} {
	value := reflect.ValueOf(input)
	kind := value.Kind()
	if kind != reflect.Array && kind != reflect.Slice {
		return input
	}
	l := value.Len()
	if l == 0 {
		return input
	}

	array := make([][]byte, l)
	for i := 0; i < l; i++ {
		array[i] = core.ToBytes(value.Index(i).Interface())
	}

	glue := core.ToBytes(f.glue.Resolve(data))
	return bytes.Join(array, glue)
}
Esempio n. 4
0
func (o *Output) Execute(writer io.Writer, data map[string]interface{}) core.ExecuteState {
	value := o.Value.Resolve(data)
	if o.Filters != nil {
		for _, filter := range o.Filters {
			value = filter(value, data)
		}
	}
	writer.Write(core.ToBytes(value))
	return core.Normal
}
Esempio n. 5
0
func (f *DebugFilter) Debug(input interface{}, data map[string]interface{}) interface{} {
	l := len(f.parameters)
	if l == 0 {
		return []byte("debug(" + input.(string) + ")")
	}
	values := make([]string, l)
	for i := 0; i < l; i++ {
		values[i] = string(core.ToBytes(f.parameters[i].Resolve(data)))
	}
	return []byte("debug(" + input.(string) + ", " + strings.Join(values, ", ") + ")")
}
Esempio n. 6
0
func (a *PrependFilter) Prepend(input interface{}, data map[string]interface{}) interface{} {
	var value string
	switch typed := a.value.Resolve(data).(type) {
	case string:
		value = typed
	default:
		value = string(core.ToBytes(typed))
	}
	switch typed := input.(type) {
	case string:
		return value + typed
	default:
		return value + core.ToString(typed)
	}
}