boolq

package module
v0.0.0-...-f869a72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2016 License: MIT Imports: 2 Imported by: 9

README

boolq

build simple bool expressions. Supports !, AND, OR, and () grouping. Individual items start with a letter and continue until whitespace. How you treat those items is up to you and is based on the Ask method.

Example:

package main

import (
	"fmt"
	"log"

	"github.com/kylebrandt/boolq"
)

func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
}

func main() {
	f := foo{}
	ask := "(true AND true) AND !false"
	q, err := boolq.AskExpr(ask, f)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(q)
}

type foo struct{}

func (f foo) Ask(ask string) (bool, error) {
	switch ask {
	case "true":
		return true, nil
	case "false":
		return false, nil
	}
	return false, fmt.Errorf("couldn't parse ask arg")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AskExpr

func AskExpr(expr string, asker Asker) (bool, error)

AskExpr takes an expression and an Asker. It then parses the expression calling the Asker's Ask on expressions AskNodes and returns if the expression is true or not for the given asker.

func AskParsedExpr

func AskParsedExpr(q *Tree, asker Asker) (bool, error)

AskParsedExpr is like AskExpr but takes an expression that has already been parsed by parse.Parse on the expression. This is useful if you are calling the same expression multiple times.

Types

type Asker

type Asker interface {
	Ask(string) (bool, error)
}

An Asker is something that can be queried using boolq. The string passed to Ask will be the component in an expression. For example with the expression `(foo:bar AND baz:biz)` Ask will be called twice, once with the argument "foo:bar" and another time with the argument "baz:biz"

type Tree

type Tree struct {
	*parse.Tree
}

func Parse

func Parse(text string) (*Tree, error)

Parse parses an expression and returns the parsed expression. It can be used wtih AskParsedExpr

Directories

Path Synopsis
Parse nodes.
Parse nodes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL