예제 #1
0
파일: operations.go 프로젝트: xet7/gerb
func (v *AdditiveValue) Resolve(context *Context) interface{} {
	a := v.a.Resolve(context)
	b := v.b.Resolve(context)
	if na, ok := r.ToInt(a); ok {
		if nb, ok := r.ToInt(b); ok {
			if v.negate {
				nb = -nb
			}
			return na + nb
		}

	} else if fa, ok := r.ToFloat(a); ok {
		if fb, ok := r.ToFloat(b); ok {
			if v.negate {
				fb = -fb
			}
			return fa + fb
		}
	} else if ta, ok := a.(time.Duration); ok {
		if tb, ok := b.(time.Duration); ok {
			if v.negate {
				return ta - tb
			}
			return ta + tb
		}
	}
	if v.negate {
		return loggedOperationNil(a, b, "-", 0)
	}
	return loggedOperationNil(a, b, "+", 0)
}
예제 #2
0
파일: operations.go 프로젝트: xet7/gerb
func (v *MultiplicativeValue) Resolve(context *Context) interface{} {
	a := v.a.Resolve(context)
	b := v.b.Resolve(context)
	if na, ok := r.ToInt(a); ok {
		if nb, ok := r.ToInt(b); ok {
			if v.divide {
				return na / nb
			}
			return na * nb
		}
	} else if fa, ok := r.ToFloat(a); ok {
		if fb, ok := r.ToFloat(b); ok {
			if v.divide {
				return fa / fb
			}
			return fa * fb
		}
	} else if ta, ok := a.(time.Duration); ok {
		if tb, ok := b.(time.Duration); ok {
			if v.divide {
				return ta / tb
			}
			return ta * tb
		}
	}
	if v.divide {
		return loggedOperationNil(a, b, "/", 0)
	}
	return loggedOperationNil(a, b, "*", 0)
}
예제 #3
0
파일: operations.go 프로젝트: xet7/gerb
func (v *ModulatedValue) Resolve(context *Context) interface{} {
	a := v.a.Resolve(context)
	b := v.b.Resolve(context)
	if na, ok := r.ToInt(a); ok {
		if nb, ok := r.ToInt(b); ok {
			return na % nb
		}
	}
	return loggedOperationNil(a, b, "%", 0)
}
예제 #4
0
파일: value.go 프로젝트: xet7/gerb
func unindex(container interface{}, params []Value, context *Context) interface{} {
	valueLength := len(params)
	if valueLength == 0 {
		return nil
	}

	value := reflect.ValueOf(container)
	kind := value.Kind()
	if kind == reflect.Array || kind == reflect.Slice || kind == reflect.String {
		length := value.Len()
		first, ok := r.ToInt(params[0].Resolve(context))
		if ok == false {
			return nil
		}
		if first < 0 {
			first = 0
		} else if first > length-1 {
			first = length
		}
		if valueLength == 2 {
			second, ok := r.ToInt(params[1].Resolve(context))
			if ok == false {
				second = length
			} else if second > length {
				second = length
			}
			return value.Slice(first, second).Interface()
		} else {
			return value.Index(first).Interface()
		}

	} else if kind == reflect.Map {
		indexValue := reflect.ValueOf(params[0].Resolve(context))
		return value.MapIndex(indexValue).Interface()
	}
	return nil
}
예제 #5
0
파일: operations.go 프로젝트: xet7/gerb
func (v *PlusEqualValue) Resolve(context *Context) interface{} {
	id := v.a.Id()
	if len(id) == 0 {
		Log.Error(fmt.Sprintf("Invalid operation %s on a non-dynamic value ", v.operation))
	}
	counter, ok := context.Counters[id]
	if ok == false {
		counter, ok = r.ToInt(v.a.Resolve(context))
		if ok == false {
			Log.Error(fmt.Sprintf("Called %s on a non-integer %s", v.operation, id))
		}
	}
	b, ok := r.ToInt(v.b.Resolve(context))
	if ok == false {
		Log.Error(fmt.Sprintf("Trying to call %s on %s but the right value was not an integer", v.operation, id))
	}
	if v.negate {
		counter -= b
	} else {
		counter += b
	}
	context.Counters[id] = counter
	return counter
}