示例#1
0
func parseId(expr *chain.Cell) (Condition, error) {
	if expr == nil {
		return nil, fmt.Errorf("missing ID value in (id) expression")
	}

	id := new(Id)

	if id_str, ok := expr.Car().(atomiser.Symbol); !ok {
		return nil, fmt.Errorf("expected ID, got '%#v' instead", expr.Car())
	} else {
		parsed_id, err := strconv.ParseInt(string(id_str), 10, 64)
		if err != nil {
			return nil, fmt.Errorf("couldn't parse numeric ID in id expression")
		}
		*id = Id(parsed_id)
	}

	return id, nil
}
示例#2
0
func parseExpressionToCondition(expr *chain.Cell) (Condition, error) {
	sym, ok := expr.Car().(atomiser.Symbol)
	if !ok {
		return nil, fmt.Errorf("expected symbol, got '%v' instead", expr.Car())
	}

	sym = atomiser.Symbol(strings.ToLower(string(sym)))

	switch sym {
	case "and":
		return parseAnd(expr.Cdr())
	case "or":
		return parseOr(expr.Cdr())
	case "eq":
		return parseEqual(expr.Cdr())
	case "id":
		return parseId(expr.Cdr())
	}
	return nil, fmt.Errorf("unknown symbol '%s'", sym)
}
示例#3
0
func parseEqual(expr *chain.Cell) (Condition, error) {
	cond := &Equals{}

	if expr == nil {
		return nil, errors.New("missing arguments in eq")
	}

	if field, ok := expr.Car().(atomiser.Symbol); !ok {
		return nil, fmt.Errorf("expected field name, got '%#v' instead", expr.Car())
	} else {
		cond.Field = string(field)
	}

	expr = expr.Cdr()
	if expr == nil {
		return nil, fmt.Errorf("missing value in (eq %s) expression", cond.Field)
	}

	cond.Value = fmt.Sprintf("%v", expr.Car())

	return cond, nil
}