GithubHelp home page GithubHelp logo

adamancini / analyze Goto Github PK

View Code? Open in Web Editor NEW

This project forked from murphybytes/analyze

0.0 0.0 0.0 54 KB

A predicate expression evaluator designed to allow developers to embed logical expressions in YAML, or JSON files.

License: MIT License

Go 98.88% Makefile 1.12%

analyze's Introduction

Analyze

Documentation

A predicate expression evaluator designed to embed logical expressions in YAML or JSON. Think of being able to apply a SQL where clause on a set of data that you supply. For example, let say we have an array of objects.

[
  {
    "firstName": "John", 
    "lastName": "Smith"
  },
  {
    "firstName": "Mary",
    "lastName": "Jones"
  }
]

You could then apply the following expression to see if there were someone with the first name John in your collection of objects.

@len( @select(data, "$elt.firstName == 'John'")) > 0

This becomes useful when you want to build tools that perform analysis defined in a configuration file, so you might collect data on resources in kubernetes and check to see if a particular value is defined in a ConfigMap. The Go code to perform this looks like this. In this case we'd put the results of our collection in a map[string]interface{} rooted at 'resources' and 'config_maps' would refer to an array of config map resources.

ctx, err := context.New(data)
if err != nil {
	log.Fatal(err)
}
result, err := expression.EvaluateContext(ctx, `@len( @select( $resources.config_maps, "$elt.firstName == 'John'" ) ) > 0`)
if result {
	fmt.Println("found John!")
}

Expressions are composed of operators needed to compose a predicate expression and a way to declare variables that will reference a supplied data set as well as a set of builtin functions. If the existing functionality doesn't support your use case it's easy to define your own functions.

func MyFunction(args  []interface{})(interface{}, error) {
	// your implementation 
}

ctx, _ := context.New(data, context.Func("@myfunc", MyFunction))
result, _ := expression.EvaluateContext(ctx, `@myfunc($foo) == "hello"`)

Examples

Programs illustrating the usage of Analyze can be found in the examples directory. Also see the unit tests in the analyzer/expression package for more examples of expressions and how they are used.

analyze's People

Contributors

murphybytes 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.