pip-services3-expressions-gox

module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT

README

Pip.Services Logo
Tokenizers, parsers and expression calculators Golang

This module is a part of the Pip.Services polyglot microservices toolkit. It provides syntax and lexical analyzers and expression calculator optimized for repeated calculations.

The module contains the following packages:

  • Calculator - Expression calculator
  • CSV - CSV tokenizer
  • IO - input/output utility classes to support lexical analysis
  • Mustache - Mustache templating engine
  • Tokenizers - lexical analyzers to break incoming character streams into tokens
  • Variants - dynamic objects that can hold any values and operators for them

Quick links:

Use

Get the package from the Github repository:

go get -u github.com/pip-services3-gox/pip-services3-expressions-gox@latest

The example below shows how to use expression calculator to dynamically calculate user-defined expressions.

import (
    "fmt"

    calc "github.com/pip-services3-gox/pip-services3-expressions-gox/calculator"
    vars "github.com/pip-services3-gox/pip-services3-expressions-gox/calculator/variables"
    variants "github.com/pip-services3-gox/pip-services3-expressions-gox/variants"
)

...
calculator := calc.NewExpressionCalculator()

calculator.SetExpression("A + b / (3 - Max(-123, 1)*2)")

variables := vars.NewVariableCollection()
variables.Add(vars.NewVariable("A", variants.NewVariantFromInteger(1)))
variables.Add(vars.NewVariable("B", variants.NewVariantFromString("3")))

result, err := calculator.EvaluateWithVariables(variables)
if err != nil {
    fmt.Println("Failed to calculate the expression")
} else {
    fmt.Println("The result of the expression is " + result.AsString())
}
...

This is an example to process mustache templates.

import (
    "fmt"

    mustache "github.com/pip-services3-gox/pip-services3-expressions-gox/mustache"
)

template := mustache.NewMustacheTemplate()
template.SetTemplate("Hello, {{{NAME}}}{{#ESCLAMATION}}!{{/ESCLAMATION}}{{#unless ESCLAMATION}}.{{/unless}}")
result, err := template.EvaluateWithVariables(map[string]string{ "NAME": "Mike", "ESCLAMATION": "true" })
if err != nil {
    fmt.Println("Failed to evaluate mustache template")
} else {
    fmt.Println("The result of template evaluation is '" + result + "'")
}

Develop

For development you shall install the following prerequisites:

  • Golang v1.18+
  • Visual Studio Code or another IDE of your choice
  • Docker
  • Git

Run automated tests:

go test -v ./test/...

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized test as:

./test.ps1
./clear.ps1

Contacts

The Golang version of Pip.Services is created and maintained by:

  • Sergey Seroukhov

Jump to

Keyboard shortcuts

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