GithubHelp home page GithubHelp logo

conditional's Introduction

Buildkite Conditional Evaluator

A small c-like language for evaluating boolean conditions, used in Buildkite's pipeline.yml format and for filtering whether webhooks are accepted.

What's supported?

  • Comparators: == != =~ !~
  • Logical operators: || &&
  • Integers 12345
  • Strings 'foobar' or "foobar"
  • Booleans true false
  • Parenthesis to control order of evaluation ( )
  • Object dereferencing foo.bar
  • Regular expressions /^v1\.0/
  • Function calls foo("bar")
  • Prefixes: !
  • Arrays: ["foo","bar"] @> "foo"

Syntax Examples

// individual terms
true
false

// compare values
build.branch == "master"
build.tag != "v1.0.0"
"blah" == 'blah'

// function calls
env('FOO') == "BAR"
env('FOO') == obj.bar
env(env('BAR')) == "FOO"

// regular expression matches
build.tag =~ /^v/

// complex expressions
((build.tag =~ ^v) || (meta-data("foo") == "bar"))

// array operations
["master","staging"] @> build.branch

Usage

package main

import (
	"log"

	"github.com/buildkite/conditional/evaluator"
	"github.com/buildkite/conditional/lexer"
	"github.com/buildkite/conditional/object"
	"github.com/buildkite/conditional/parser"
)

func main() {
	l := lexer.New(`build.message =~ /^llamas rock/`)
	p := parser.New(l)
	expr := p.Parse()

	if errs := p.Errors(); len(errs) > 0 {
		log.Fatal(errs)
	}

	obj := evaluator.Eval(expr, object.Struct{
		"build": object.Struct{
			"message": &object.String{"llamas rock, and so do alpacas"},
		},
	})

	log.Printf("Result: %#v", obj)
}

Design

Largely derived from Writing an Interpreter in Go:

  • lexer.Lexer takes a string of input and turns it into a stream of token.Token
  • parser.Parser takes a Lexer and parses tokens into an ast.Expression
  • evaluator.Evaluator which takes a ast.Expression and evaluates it, with a object.Map for variables in scope. An *object.Object is returned.

conditional's People

Contributors

lox avatar pda avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.