GithubHelp home page GithubHelp logo

arcaflow-expressions's Introduction

Arcaflow Expressions Library

This library provides the ability to parse Arcaflow expressions.

Installation

This library can be installed as a Go module dependency:

go get go.flow.arcalot.io/expressions

Evaluating expressions

You can evaluate expressions against a data set consisting of primitive types (bool, int, float, string, map, slice) by parsing the expression and then calling the Evaluate() function:

package main

import (
    "fmt"
    "log"

    "go.flow.arcalot.io/expressions"
)

func main() {
    expr, err := expressions.New("$.foo.bar")
    if err != nil {
        log.Fatal(err)
    }
    result, err := expr.Evaluate(
        // Pass the data here:
        map[string]any{
            "foo": map[string]any{
                "bar": "hello world!",
            },
        },
        // Pass the workflow context (map of file names to content) here:
        nil,
    )
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(result)
}

Building a dependency tree

Similarly, you can also evaluate an expression against a scope and get a list of dependencies an expression has:

package main

import (
    "fmt"

    "go.flow.arcalot.io/expressions"
    "go.flow.arcalot.io/pluginsdk/schema"
)

var myScope = schema.NewScopeSchema(
    schema.NewObjectSchema(
        "root",
        map[string]*schema.PropertySchema{
            "foo": schema.NewPropertySchema(
                schema.NewStringSchema(nil, nil, nil),
                nil,
                true,
                nil,
                nil,
                nil,
                nil,
                nil,
            ),
        },
    ),
)

func main() {
    expr, err := expressions.New("$.foo")
    if err != nil {
        panic(err)
    }

    dependencyList, err := expr.Dependencies(
        myScope,
        nil,
    )
    if err != nil {
        panic(err)
    }

    fmt.Printf("%v", dependencyList)
    // Output: [$.foo]
}

Evaluating result types

You can also evaluate an expression and retrieve the result type. Note, that the result is not 100% guaranteed as the result may be optional and the value may not be available.

package main

import (
    "fmt"

    "go.flow.arcalot.io/expressions"
    "go.flow.arcalot.io/pluginsdk/schema"
)

var scopeForType = schema.NewScopeSchema(
    schema.NewObjectSchema(
        "root",
        map[string]*schema.PropertySchema{
            "foo": schema.NewPropertySchema(
                schema.NewStringSchema(nil, nil, nil),
                nil,
                true,
                nil,
                nil,
                nil,
                nil,
                nil,
            ),
        },
    ),
)

func main() {
    expr, err := expressions.New("$.foo")
    if err != nil {
        panic(err)
    }

    t, err := expr.Type(
        scopeForType,
        nil,
    )
    if err != nil {
        panic(err)
    }

    fmt.Printf("%v", t.TypeID())
    // Output: string
}

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.