exprml

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: BSD-2-Clause Imports: 9 Imported by: 1

README

exprml-go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(path *pb.Expr_Path, pos ...any) *pb.Expr_Path

func ArrValue

func ArrValue(arr []*pb.Value) *pb.Value

func BoolValue

func BoolValue(b bool) *pb.Value

func Find

func Find(defStack *pb.DefStack, ident string) *pb.DefStack

func Format added in v0.0.2

func Format(path *pb.Expr_Path) string

func Keys added in v0.0.2

func Keys(value *pb.Value) []string

func NewDefinition added in v0.0.2

func NewDefinition(path *pb.Expr_Path, ident string, value *pb.Value) *pb.Eval_Definition

func NumValue

func NumValue(num float64) *pb.Value

func ObjValue

func ObjValue(obj map[string]*pb.Value) *pb.Value

func Register

func Register(defStack *pb.DefStack, def *pb.Eval_Definition) *pb.DefStack

func StrValue

func StrValue(str string) *pb.Value

Types

type Decoder

type Decoder interface {
	Decode(input *pb.DecodeInput) *pb.DecodeOutput
}

func NewDecoder

func NewDecoder() Decoder

type Encoder

type Encoder interface {
	Encode(input *pb.EncodeInput) *pb.EncodeOutput
}

func NewEncoder

func NewEncoder() Encoder

type Evaluator

type Evaluator interface {
	EvaluateExpr(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateEval(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateScalar(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateRef(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateObj(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateArr(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateJson(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateIter(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateElem(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateCall(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateCases(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateOpUnary(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateOpBinary(input *pb.EvaluateInput) *pb.EvaluateOutput
	EvaluateOpVariadic(input *pb.EvaluateInput) *pb.EvaluateOutput
}
Example
source := "cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"
decoded := exprml.NewDecoder().Decode(&pb.DecodeInput{Yaml: source})
parsed := exprml.NewParser().Parse(&pb.ParseInput{Value: decoded.Value})
evaluated := exprml.NewEvaluator(nil).EvaluateExpr(&pb.EvaluateInput{Expr: parsed.Expr})
encoded := exprml.NewEncoder().Encode(&pb.EncodeInput{Value: evaluated.Value})
fmt.Println(encoded.Result)
Output:

Hello, ExprML!
Example (AfterEvaluate)
source := "cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"
decoded := exprml.NewDecoder().Decode(&pb.DecodeInput{Yaml: source})
parsed := exprml.NewParser().Parse(&pb.ParseInput{Value: decoded.Value})
evaluator := exprml.NewEvaluator(&exprml.EvaluatorConfig{
	AfterEvaluate: func(input *pb.EvaluateInput, output *pb.EvaluateOutput) error {
		fmt.Printf("After evaluation: %q: %v\n", exprml.Format(input.Expr.Path), output.Value.String())
		return nil
	},
})
_ = evaluator.EvaluateExpr(&pb.EvaluateInput{Expr: parsed.Expr})
Output:

After evaluation: "/cat/0": type:STR str:"Hello"
After evaluation: "/cat/1": type:STR str:", "
After evaluation: "/cat/2": type:STR str:"ExprML"
After evaluation: "/cat/3": type:STR str:"!"
After evaluation: "/": type:STR str:"Hello, ExprML!"
Example (BeforeEvaluate)
source := "cat: ['`Hello`', '`, `', '`ExprML`', '`!`']"
decoded := exprml.NewDecoder().Decode(&pb.DecodeInput{Yaml: source})
parsed := exprml.NewParser().Parse(&pb.ParseInput{Value: decoded.Value})
evaluator := exprml.NewEvaluator(&exprml.EvaluatorConfig{
	BeforeEvaluate: func(input *pb.EvaluateInput) error {
		fmt.Printf("Before evaluation: %q\n", exprml.Format(input.Expr.Path))
		return nil
	},
})
_ = evaluator.EvaluateExpr(&pb.EvaluateInput{Expr: parsed.Expr})
Output:

Before evaluation: "/"
Before evaluation: "/cat/0"
Before evaluation: "/cat/1"
Before evaluation: "/cat/2"
Before evaluation: "/cat/3"
Example (Extension)
source := "$hello: { $name: '`ExprML Extension`' }"
decoded := exprml.NewDecoder().Decode(&pb.DecodeInput{Yaml: source})
parsed := exprml.NewParser().Parse(&pb.ParseInput{Value: decoded.Value})
evaluator := exprml.NewEvaluator(&exprml.EvaluatorConfig{
	Extension: map[string]func(path *pb.Expr_Path, args map[string]*pb.Value) *pb.EvaluateOutput{
		"$hello": func(path *pb.Expr_Path, args map[string]*pb.Value) *pb.EvaluateOutput {
			name, ok := args["$name"]
			if !ok || name.Type != pb.Value_STR {
				return &pb.EvaluateOutput{
					Status:       pb.EvaluateOutput_UNKNOWN_ERROR,
					ErrorPath:    path,
					ErrorMessage: "invalid argument: $name",
				}
			}
			return &pb.EvaluateOutput{
				Value: &pb.Value{Type: pb.Value_STR, Str: "Hello, " + name.Str + "!"},
			}
		},
	},
})
evaluated := evaluator.EvaluateExpr(&pb.EvaluateInput{Expr: parsed.Expr})
encoded := exprml.NewEncoder().Encode(&pb.EncodeInput{Value: evaluated.Value})
fmt.Println(encoded.Result)
Output:

Hello, ExprML Extension!

func NewEvaluator

func NewEvaluator(config *EvaluatorConfig) Evaluator

type EvaluatorConfig added in v0.0.2

type EvaluatorConfig struct {
	Extension      map[string]func(path *pb.Expr_Path, args map[string]*pb.Value) *pb.EvaluateOutput
	BeforeEvaluate func(input *pb.EvaluateInput) error
	AfterEvaluate  func(input *pb.EvaluateInput, output *pb.EvaluateOutput) error
}

type Parser

type Parser interface {
	Parse(input *pb.ParseInput) *pb.ParseOutput
}

func NewParser

func NewParser() Parser

Directories

Path Synopsis
pb

Jump to

Keyboard shortcuts

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