예제 #1
0
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))
}
예제 #2
0
파일: for.go 프로젝트: get3w/get3w
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
}
예제 #3
0
파일: date.go 프로젝트: get3w/get3w
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
}
예제 #4
0
파일: modulo.go 프로젝트: get3w/get3w
// 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
	}
}
예제 #5
0
파일: divideby.go 프로젝트: get3w/get3w
func DivideByFactory(parameters []core.Value) core.Filter {
	if len(parameters) == 0 {
		return Noop
	}
	switch typed := parameters[0].(type) {
	case *core.StaticIntValue:
		return (&FloatTimesFilter{1 / float64(typed.Value)}).Times
	case *core.StaticFloatValue:
		return (&FloatTimesFilter{1 / typed.Value}).Times
	case *core.DynamicValue:
		return (&DynamicDivideByFilter{typed}).DivideBy
	default:
		if n, ok := core.ToInt(parameters[0].Underlying()); ok {
			return (&FloatTimesFilter{1 / float64(n)}).Times
		}
		return Noop
	}
}
예제 #6
0
파일: minus.go 프로젝트: get3w/get3w
// Creates a minus filter
func MinusFactory(parameters []core.Value) core.Filter {
	if len(parameters) == 0 {
		return defaultMinus
	}
	switch typed := parameters[0].(type) {
	case *core.StaticIntValue:
		return (&IntPlusFilter{-typed.Value}).Plus
	case *core.StaticFloatValue:
		return (&FloatPlusFilter{-typed.Value}).Plus
	case *core.DynamicValue:
		return (&DynamicMinusFilter{typed}).Minus
	default:
		if n, ok := core.ToInt(parameters[0].Underlying()); ok {
			return (&IntPlusFilter{-n}).Plus
		}
		return Noop
	}
}
예제 #7
0
파일: truncate.go 프로젝트: get3w/get3w
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
}
예제 #8
0
파일: modulo.go 프로젝트: get3w/get3w
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
}