-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hold compiled assertion in api
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
f349c31
commit 0e18bed
Showing
7 changed files
with
114 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/kyverno/kyverno-json/pkg/engine/assert" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
// +k8s:deepcopy-gen=false | ||
// +kubebuilder:validation:XPreserveUnknownFields | ||
// +kubebuilder:validation:Type:="" | ||
// AssertionTree represents an assertion tree. | ||
type AssertionTree struct { | ||
_tree any | ||
_assertion func() (assert.Assertion, error) | ||
} | ||
|
||
func NewAssertionTree(value any) AssertionTree { | ||
return AssertionTree{ | ||
_tree: value, | ||
_assertion: sync.OnceValues(func() (assert.Assertion, error) { | ||
return assert.Parse(context.Background(), nil, value) | ||
}), | ||
} | ||
} | ||
|
||
func (t *AssertionTree) Assertion(ctx context.Context, path *field.Path) (assert.Assertion, error) { | ||
if t._tree == nil { | ||
return nil, nil | ||
} | ||
return t._assertion() | ||
} | ||
|
||
func (a *AssertionTree) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(a._tree) | ||
} | ||
|
||
func (a *AssertionTree) UnmarshalJSON(data []byte) error { | ||
var v any | ||
err := json.Unmarshal(data, &v) | ||
if err != nil { | ||
return err | ||
} | ||
a._tree = v | ||
return nil | ||
} | ||
|
||
func (in *AssertionTree) DeepCopyInto(out *AssertionTree) { | ||
out._tree = deepCopy(in._tree) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func deepCopy(in any) any { | ||
if in == nil { | ||
return nil | ||
} | ||
switch in := in.(type) { | ||
case string: | ||
return in | ||
case int: | ||
return in | ||
case int32: | ||
return in | ||
case int64: | ||
return in | ||
case float32: | ||
return in | ||
case float64: | ||
return in | ||
case bool: | ||
return in | ||
case []any: | ||
var out []any | ||
for _, in := range in { | ||
out = append(out, deepCopy(in)) | ||
} | ||
return out | ||
case map[string]any: | ||
out := map[string]any{} | ||
for k, in := range in { | ||
out[k] = deepCopy(in) | ||
} | ||
return out | ||
} | ||
panic(fmt.Sprintf("deep copy failed - unrecognized type %T", in)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters