Skip to content

Commit 6be8f0e

Browse files
committed
Document motivation and an example use case
1 parent a7e4934 commit 6be8f0e

File tree

1 file changed

+11
-43
lines changed

1 file changed

+11
-43
lines changed

patchers/value/value.go

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,4 @@
11
// Package value provides a Patcher that uses interfaces to allow custom types that can be represented as standard go values to be used more easily in expressions.
2-
//
3-
// # Example Usage
4-
//
5-
// import (
6-
// "fmt"
7-
// "github.com/expr-lang/expr/patchers/value"
8-
// "github.com/expr-lang/expr"
9-
// )
10-
//
11-
// type customInt struct {
12-
// Int int
13-
// }
14-
//
15-
// // Provides type checking at compile time
16-
// func (v *customInt) AsInt() int {
17-
// return v.Int
18-
// }
19-
//
20-
// // Lets us return nil if we need to
21-
// func (v *customInt) AsAny() any {
22-
// return v.Int
23-
// }
24-
//
25-
// func main() {
26-
// env := make(map[string]any)
27-
// env["ValueOne"] = &customInt{1}
28-
// env["ValueTwo"] = &customInt{2}
29-
//
30-
// program, err := expr.Compile("ValueOne + ValueTwo", expr.Env(env), value.Patcher)
31-
//
32-
// if err != nil {
33-
// panic(err)
34-
// }
35-
//
36-
// out, err := vm.Run(program, env)
37-
//
38-
// if err != nil {
39-
// panic(err)
40-
// }
41-
//
42-
// fmt.Printf("Got %v", out)
43-
// }
442
package value
453

464
import (
@@ -52,7 +10,17 @@ import (
5210
"github.com/expr-lang/expr/conf"
5311
)
5412

55-
// Patcher is an expr.Option that both patches the program and adds the `$patcher_value_getter` function.
13+
// ValueGetter is a Patcher that allows custom types to be represented as standard go values for use with expr.
14+
// It also adds the `$patcher_value_getter` function to the program for efficiently calling matching interfaces.
15+
//
16+
// The purpose of this Patcher is to make it seemless to use custom types in expressions without the need to
17+
// first convert them to standard go values. It may also facilitate using already existing structs or maps as
18+
// environments when they contain compatabile types.
19+
//
20+
// An example usage may be modeling a database record with columns that have varying data types and constraints.
21+
// In such an example you may have custom types that, beyond storing a simple value, such as an integer, may
22+
// contain metadata such as column type and if a value is specifically a NULL value.
23+
//
5624
// Use it directly as an Option to expr.Compile()
5725
var ValueGetter = func() expr.Option {
5826
vPatcher := patcher{}

0 commit comments

Comments
 (0)