-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_options_test.go
More file actions
89 lines (78 loc) · 2.05 KB
/
example_options_test.go
File metadata and controls
89 lines (78 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package conditional_test
import (
"errors"
"fmt"
"strings"
"github.com/buildkite/conditional"
)
func ExampleWithFunction() {
branch := "release/2026-06-07"
startsWith := conditional.WithFunction("starts_with", conditional.Function{
Args: []conditional.ValueType{conditional.StringType, conditional.StringType},
Return: conditional.BoolType,
Eval: func(args []conditional.Value) (conditional.Value, error) {
value, ok := args[0].AsString()
if !ok {
return conditional.NullValue(), errors.New("value must be a string")
}
prefix, ok := args[1].AsString()
if !ok {
return conditional.NullValue(), errors.New("prefix must be a string")
}
return conditional.BoolValue(strings.HasPrefix(value, prefix)), nil
},
})
ok, err := conditional.Evaluate(
`starts_with(build.branch, "release/")`,
conditional.Context{
EntryPoint: conditional.EntryPointBuildCondition,
Build: conditional.Build{
Branch: &branch,
},
},
startsWith,
)
if err != nil {
panic(err)
}
fmt.Println(ok)
// Output:
// true
}
func ExampleNewEvaluator() {
branch := "release/2026-06-07"
startsWith := conditional.WithFunction("starts_with", conditional.Function{
Args: []conditional.ValueType{conditional.StringType, conditional.StringType},
Return: conditional.BoolType,
Eval: func(args []conditional.Value) (conditional.Value, error) {
value, ok := args[0].AsString()
if !ok {
return conditional.NullValue(), errors.New("value must be a string")
}
prefix, ok := args[1].AsString()
if !ok {
return conditional.NullValue(), errors.New("prefix must be a string")
}
return conditional.BoolValue(strings.HasPrefix(value, prefix)), nil
},
})
evaluator, err := conditional.NewEvaluator(startsWith)
if err != nil {
panic(err)
}
ok, err := evaluator.Evaluate(
`starts_with(build.branch, "release/")`,
conditional.Context{
EntryPoint: conditional.EntryPointBuildCondition,
Build: conditional.Build{
Branch: &branch,
},
},
)
if err != nil {
panic(err)
}
fmt.Println(ok)
// Output:
// true
}