Example #1
0
func (b *Builder) conditionOrProperty(data Property) interface{} {
	parse := func(expr string, required bool) walk.Condition {
		var negated bool
		if strings.HasPrefix(expr, "!") {
			negated = true
			expr = strings.TrimSpace(expr[1:])
		}

		var condition walk.Condition

		if p := b.property(expr); p != nil {
			condition = p.(walk.Condition)
		} else if c, ok := conditionsByName[expr]; ok {
			condition = c
		} else if required {
			panic("unknown condition or property name: " + expr)
		}

		if negated {
			condition = walk.NewNegatedCondition(condition)
		}

		return condition
	}

	switch val := data.(type) {
	case bindData:
		if c, ok := b.knownCompositeConditions[val.expression]; ok {
			return c
		} else if conds := strings.Split(val.expression, "&&"); len(conds) > 1 {
			// This looks like a composite condition.
			for i, s := range conds {
				conds[i] = strings.TrimSpace(s)
			}

			var conditions []walk.Condition

			for _, cond := range conds {
				conditions = append(conditions, parse(cond, true))
			}

			var condition walk.Condition
			if len(conditions) > 1 {
				condition = walk.NewAllCondition(conditions...)
				b.knownCompositeConditions[val.expression] = condition
			} else {
				condition = conditions[0]
			}

			return condition
		}

		return parse(val.expression, false)

	case walk.Condition:
		return val
	}

	return nil
}
Example #2
0
func (b *Builder) conditionOrProperty(data Property) interface{} {
	switch val := data.(type) {
	case bindData:
		if c, ok := b.knownCompositeConditions[val.expression]; ok {
			return c
		} else if conds := strings.Split(val.expression, "&&"); len(conds) > 1 {
			// This looks like a composite condition.
			for i, s := range conds {
				conds[i] = strings.TrimSpace(s)
			}

			var conditions []walk.Condition

			for _, cond := range conds {
				if p := b.property(cond); p != nil {
					conditions = append(conditions, p.(walk.Condition))
				} else if c, ok := conditionsByName[cond]; ok {
					conditions = append(conditions, c)
				} else {
					panic("unknown condition or property name: " + cond)
				}
			}

			var condition walk.Condition
			if len(conditions) > 1 {
				condition = walk.NewAllCondition(conditions...)
				b.knownCompositeConditions[val.expression] = condition
			} else {
				condition = conditions[0]
			}

			return condition
		}

		if p := b.property(val.expression); p != nil {
			return p
		}

		return conditionsByName[val.expression]

	case walk.Condition:
		return val
	}

	return nil
}