govaluate

package module
v0.0.0-...-5918ab1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: MIT Imports: 7 Imported by: 0

README

govaluate

Build Status Godoc

作用

此包是一个类似计算器功能的包,内部包含自定义语法解析

因为有些功能在原包体上不支持,因此进行了二次开发,并进行性能优化

example:

type CalculatorOutPut struct {
    IntValue    float64 `json:"int_value"` // 如果用默认的运算符,返回值都会是float64类型  如果需要指定类型,就需要编写元函数
    FloatValue  float64 `json:"float_value"`
    StringValue string  `json:"string_value"`
}

type CalculatorInPut struct {
    IntValue    float64 `json:"int_value"`
    FloatValue  float64 `json:"float_value"`
    StringValue string  `json:"string_value"`
}

func TestNewCalculator(t *testing.T) {
    cal := NewCalculator()
    cal.SetCalculatorRelations([]CalculatorRelation{
        {
            OutputField: "IntValue",
            Expression:  "IntValue+1",
        },
        {
            OutputField: "FloatValue",
            Expression:  "FloatValue+3",
        },
        {
            OutputField: "StringValue",
            Expression:  "'a'",
        },
    })
    res := CalculatorOutPut{}
    e := cal.Expression(CalculatorInPut{}, &res)
    t.Logf("%+v %+v", e, res)
}

func BenchmarkNewCalculator(b *testing.B) {
    cal := NewCalculator()
    cal.SetCalculatorRelations([]CalculatorRelation{
        {
            OutputField: "IntValue",
            Expression:  "IntValue+1",
        },
        {
            OutputField: "FloatValue",
            Expression:  "FloatValue+3",
        },
        {
            OutputField: "StringValue",
            Expression:  "'a'",
        },
    })
    res := CalculatorOutPut{}

    for i := 0; i < b.N; i++ {
        _ = cal.Expression(CalculatorInPut{}, &res)
    }
    b.ReportAllocs()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Calculator

type Calculator struct {
	Eval                defined.IExpression
	CalculatorRelations []CalculatorRelation
}

func NewCalculator

func NewCalculator() *Calculator

func (*Calculator) Expression

func (c *Calculator) Expression(in interface{}, out interface{}) error

func (*Calculator) SetCalculatorRelations

func (c *Calculator) SetCalculatorRelations(values []CalculatorRelation)

type CalculatorRelation

type CalculatorRelation struct {
	OutputField string `json:"output_field"`
	Expression  string `json:"expression"`

} // output_field = eval(expression)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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