From 606d727cf0c38e63bfde11c843f30de4fb6ac4ec Mon Sep 17 00:00:00 2001 From: Tim <32556895+Avarei@users.noreply.github.com> Date: Sat, 29 Nov 2025 21:49:04 +0100 Subject: [PATCH] add special marshal and unmarshal function for ResourceSelector Signed-off-by: Tim <32556895+Avarei@users.noreply.github.com> --- proto/v1/resource_selector.go | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 proto/v1/resource_selector.go diff --git a/proto/v1/resource_selector.go b/proto/v1/resource_selector.go new file mode 100644 index 0000000..7a9bc09 --- /dev/null +++ b/proto/v1/resource_selector.go @@ -0,0 +1,58 @@ +package v1 + +import "encoding/json" + +type jsonResourceSelector struct { + ApiVersion string `json:"apiVersion"` + Kind string `json:"kind"` + MatchName *string `json:"matchName,omitempty"` + MatchLabels *MatchLabels `json:"matchLabels,omitempty"` + Namespace *string `json:"namespace,omitempty"` +} + +func (r *ResourceSelector) UnmarshalJSON(data []byte) error { + var tmp jsonResourceSelector + + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + + r.ApiVersion = tmp.ApiVersion + r.Kind = tmp.Kind + r.Namespace = tmp.Namespace + + switch { + case tmp.MatchName != nil: + r.Match = &ResourceSelector_MatchName{ + MatchName: *tmp.MatchName, + } + + case tmp.MatchLabels != nil: + r.Match = &ResourceSelector_MatchLabels{ + MatchLabels: tmp.MatchLabels, + } + + default: + r.Match = nil + } + + return nil +} + +func (r *ResourceSelector) MarshalJSON() ([]byte, error) { + var tmp jsonResourceSelector + + tmp.ApiVersion = r.ApiVersion + tmp.Kind = r.Kind + tmp.Namespace = r.Namespace + + switch m := r.Match.(type) { + case *ResourceSelector_MatchName: + tmp.MatchName = &m.MatchName + case *ResourceSelector_MatchLabels: + tmp.MatchLabels = m.MatchLabels + } + + return json.Marshal(tmp) +} +