expression

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: Apache-2.0 Imports: 18 Imported by: 3 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Expression

Go Report Card Go Reference

A Go package that provides powerful expression evaluation and templating capabilities, built on top of the expr language.

Installation

go get github.com/jahvon/expression

Basic Expression Evaluation

package main

import (
    "fmt"
    "github.com/jahvon/expression"
)

func main() {
    // Create data context
    data := map[string]interface{}{
        "name": "John",
        "age":  30,
    }

    // Evaluate expressions
    result, err := expression.Evaluate("name + ' is ' + string(age)", data)
    if err != nil {
        panic(err)
    }
    fmt.Println(result) // Output: John is 30

    // Check truthiness
    truthy, err := expression.IsTruthy("age > 18", data)
    if err != nil {
        panic(err)
    }
    fmt.Println(truthy) // Output: true
}

Template Processing

The template engine extends Go's text/template with Expr expression evaluation:

package main

import (
    "fmt"
    "github.com/jahvon/expression"
)

func main() {
    data := map[string]interface{}{
        "user":    "Alice",
        "enabled": true,
        "items":   []string{"apple", "banana", "orange"},
    }

    tmpl := expression.NewTemplate("example", data)
    
    templateText := `
Hello {{user}}!
It's {{$("time")}}

{{if enabled}}
Your account is active.
Items:
{{range items}}
- {{.}}
{{end}}
{{end}}
`

    err := tmpl.Parse(templateText)
    if err != nil {
        panic(err)
    }

    result, err := tmpl.ExecuteToString()
    if err != nil {
        panic(err)
    }
    
    fmt.Println(result)
}

Expression Language

See the Expr Language Definition for the full syntax and capabilities of the expression language.

Additionally, the following functions are provided:

File Helpers:

  • fileExists(path) - Check if file/directory exists
  • dirExists(path) - Check if path is a directory
  • isFile(path) - Check if path is a file
  • isDir(path) - Check if path is a directory
  • basename(path) - Get filename from path
  • dirname(path) - Get directory from path
  • readFile(path) - Read file contents as string
  • fileSize(path) - Get file size in bytes
  • fileModTime(path) - Get file modification time
  • fileAge(path) - Get duration since last modified
// Example usage
result, _ := expression.Evaluate(`fileExists("/path/file.txt") && fileSize("/path/file.txt") > 0`, nil)
filename, _ := expression.EvaluateString(`basename("/home/user/doc.txt")`, nil) // "doc.txt"

Contributing

Contributions are welcome! Please ensure all tests pass:

go test ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate

func Evaluate(ex string, data Data) (interface{}, error)

func EvaluateString

func EvaluateString(ex string, data Data) (string, error)

func IsTruthy

func IsTruthy(ex string, data Data) (bool, error)

Types

type Data

type Data interface{}

func BuildData

func BuildData(ctx context.Context, envMap map[string]string, kvPairs ...interface{}) (Data, error)

BuildData constructs a Data object from a context, environment map, and key-value pairs. It provides the following variables by default: - `os`: string for the operating system (e.g., "linux", "darwin") - `arch`: string for the architecture (e.g., "amd64", "arm64") - `env`: the environment variables passed in the envMap - `$`: a function that takes a shell command as input and returns its output as a string

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template wraps text/template but evaluates expressions using expr instead

func NewTemplate

func NewTemplate(name string, data Data) *Template

func (*Template) Execute

func (t *Template) Execute(wr io.Writer) error

func (*Template) ExecuteToString

func (t *Template) ExecuteToString() (string, error)

func (*Template) Parse

func (t *Template) Parse(text string) error

func (*Template) ParseFile added in v0.1.3

func (t *Template) ParseFile(file string) error

Jump to

Keyboard shortcuts

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