Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions proto/v1/resource_selector.go
Original file line number Diff line number Diff line change
@@ -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)
}