File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -2,10 +2,12 @@ package v1alpha1
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"regexp"
7
8
8
9
"github.com/jmespath-community/go-jmespath/pkg/binding"
10
+ "github.com/jmespath-community/go-jmespath/pkg/parsing"
9
11
"github.com/kyverno/chainsaw/pkg/expressions"
10
12
"github.com/kyverno/kyverno-json/pkg/apis/policy/v1alpha1"
11
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -66,6 +68,34 @@ type Expectation struct {
66
68
// Expression defines an expression to be used in string fields.
67
69
type Expression string
68
70
71
+ func (e * Expression ) MarshalJSON () ([]byte , error ) {
72
+ if e == nil {
73
+ return nil , nil
74
+ }
75
+ return json .Marshal (string (* e ))
76
+ }
77
+
78
+ func (e * Expression ) UnmarshalJSON (data []byte ) error {
79
+ var statement string
80
+ err := json .Unmarshal (data , & statement )
81
+ if err != nil {
82
+ return err
83
+ }
84
+ * e = Expression (statement )
85
+ expression := expressions .Parse (context .TODO (), statement )
86
+ if expression == nil {
87
+ return nil
88
+ }
89
+ if expression .Engine == "" {
90
+ return nil
91
+ }
92
+ parser := parsing .NewParser ()
93
+ if _ , err := parser .Parse (statement ); err != nil {
94
+ return err
95
+ }
96
+ return nil
97
+ }
98
+
69
99
func (e Expression ) Value (ctx context.Context , bindings binding.Bindings ) (string , error ) {
70
100
return expressions .String (ctx , string (e ), bindings )
71
101
}
You can’t perform that action at this time.
0 commit comments