func (t *TruncateWordsFilter) Truncate(input interface{}, data map[string]interface{}) interface{} { length, ok := core.ToInt(t.limit.Resolve(data)) if ok == false { return input } var value string switch typed := input.(type) { case string: value = typed default: value = core.ToString(typed) } i := 2 found := 0 l := len(value) for ; i < l; i++ { if value[i] == ' ' && value[i-1] != ' ' && value[i-2] != ' ' { found++ if found == length { break } } } if i == l { return input } return value[:i] + core.ToString(t.append.Resolve(data)) }
func (f *For) Execute(writer io.Writer, data map[string]interface{}) core.ExecuteState { value := reflect.ValueOf(f.value.Resolve(data)) kind := value.Kind() if kind == reflect.Array || kind == reflect.Slice || kind == reflect.String || kind == reflect.Map { length := value.Len() if length == 0 { return core.Normal } state := &LoopState{ data: data, writer: writer, originalForLoop: data["forloop"], Length: length, } data["forloop"] = state if f.limit != nil { if limit, ok := core.ToInt(f.limit.Resolve(data)); ok && limit < length { state.Length = limit } } if f.offset != nil { if offset, ok := core.ToInt(f.offset.Resolve(data)); ok { state.offset = offset if n := state.Length + state.offset; n < state.Length { state.Length = n } else if length < state.offset+state.Length { state.Length = length - offset } } } defer f.loopTeardown(state) if kind == reflect.Map { state.isMap = true state.key = data[f.keyName] state.value = data[f.valueName] f.iterateMap(state, value) } else { state.value = data[f.name] f.iterateArray(state, value, kind == reflect.String) } } return core.Normal }
func inputToTime(input interface{}) (time.Time, bool) { switch typed := input.(type) { case time.Time: return typed, true case string: return timeFromString(typed) case []byte: return timeFromString(string(typed)) } if n, ok := core.ToInt(input); ok { return core.Now().Add(time.Minute * time.Duration(n)), true } return zeroTime, false }
// Creates a plus filter func ModuloFactory(parameters []core.Value) core.Filter { if len(parameters) == 0 { return Noop } switch typed := parameters[0].(type) { case *core.StaticIntValue: return (&IntModuloFilter{typed.Value}).Mod case *core.DynamicValue: return (&DynamicModuloFilter{typed}).Mod default: if n, ok := core.ToInt(parameters[0].Underlying()); ok { return (&IntModuloFilter{int(n)}).Mod } return Noop } }
// Creates a time filter func TimesFactory(parameters []core.Value) core.Filter { if len(parameters) == 0 { return Noop } switch typed := parameters[0].(type) { case *core.StaticIntValue: return (&IntTimesFilter{typed.Value}).Times case *core.StaticFloatValue: return (&FloatTimesFilter{typed.Value}).Times case *core.DynamicValue: return (&DynamicTimesFilter{typed}).Times default: if n, ok := core.ToInt(parameters[0].Underlying()); ok { return (&IntTimesFilter{n}).Times } return Noop } }
func (t *TruncateFilter) Truncate(input interface{}, data map[string]interface{}) interface{} { length, ok := core.ToInt(t.limit.Resolve(data)) if ok == false { return input } var value string switch typed := input.(type) { case string: value = typed default: value = core.ToString(typed) } append := core.ToString(t.append.Resolve(data)) length -= len(append) if length >= len(value) { return input } if length < 0 { return append } return value[:length] + append }
func (m *DynamicModuloFilter) Mod(input interface{}, data map[string]interface{}) interface{} { if n, ok := core.ToInt(m.value.Resolve(data)); ok { return modInt(n, input) } return input }