Skip to content

Commit

Permalink
Change to variadic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
creydr committed Jun 17, 2024
1 parent 536d5bb commit 1683138
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
96 changes: 42 additions & 54 deletions test/rekt/resources/eventpolicy/eventpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,82 +52,70 @@ func Install(name string, opts ...manifest.CfgFn) feature.StepFn {
}
}

func WithTos(tos []eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn {
return func(cfg map[string]interface{}) {
for _, to := range tos {
WithTo(to)(cfg)
}
}
}

func WithTo(ref eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn {
func WithTo(tos ...eventingv1alpha1.EventPolicySpecTo) manifest.CfgFn {
return func(cfg map[string]interface{}) {
if _, set := cfg["to"]; !set {
cfg["to"] = []map[string]interface{}{}
}

to := map[string]interface{}{}
if ref.Ref != nil {
to = map[string]interface{}{
"ref": map[string]interface{}{
"apiVersion": ref.Ref.APIVersion,
"kind": ref.Ref.Kind,
"name": ref.Ref.Name,
}}
}

if ref.Selector != nil {
selector := labelSelectorToStringMap(ref.Selector.LabelSelector)
selector["apiVersion"] = ref.Selector.APIVersion
selector["kind"] = ref.Selector.Kind

to = map[string]interface{}{
"selector": selector,
res := cfg["to"].([]map[string]interface{})
for _, ref := range tos {
to := map[string]interface{}{}
if ref.Ref != nil {
to = map[string]interface{}{
"ref": map[string]interface{}{
"apiVersion": ref.Ref.APIVersion,
"kind": ref.Ref.Kind,
"name": ref.Ref.Name,
}}
}
}

tos := cfg["to"].([]map[string]interface{})
tos = append(tos, to)
if ref.Selector != nil {
selector := labelSelectorToStringMap(ref.Selector.LabelSelector)
selector["apiVersion"] = ref.Selector.APIVersion
selector["kind"] = ref.Selector.Kind

cfg["to"] = tos
}
}
to = map[string]interface{}{
"selector": selector,
}
}

func WithFroms(froms []eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn {
return func(cfg map[string]interface{}) {
for _, from := range froms {
WithFrom(from)(cfg)
res = append(res, to)
}

cfg["to"] = res
}
}

func WithFrom(ref eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn {
func WithFrom(froms ...eventingv1alpha1.EventPolicySpecFrom) manifest.CfgFn {
return func(cfg map[string]interface{}) {
if _, set := cfg["from"]; !set {
cfg["from"] = []map[string]interface{}{}
}

from := map[string]interface{}{}
if ref.Ref != nil {
from = map[string]interface{}{
"ref": map[string]interface{}{
"apiVersion": ref.Ref.APIVersion,
"kind": ref.Ref.Kind,
"name": ref.Ref.Name,
"namespace": ref.Ref.Namespace,
}}
}
res := cfg["from"].([]map[string]interface{})
for _, ref := range froms {
from := map[string]interface{}{}
if ref.Ref != nil {
from = map[string]interface{}{
"ref": map[string]interface{}{
"apiVersion": ref.Ref.APIVersion,
"kind": ref.Ref.Kind,
"name": ref.Ref.Name,
"namespace": ref.Ref.Namespace,
}}
}

if ref.Sub != nil && *ref.Sub != "" {
from = map[string]interface{}{
"sub": *ref.Sub,
if ref.Sub != nil && *ref.Sub != "" {
from = map[string]interface{}{
"sub": *ref.Sub,
}
}
}

froms := cfg["from"].([]map[string]interface{})
froms = append(froms, from)
res = append(res, from)
}

cfg["from"] = froms
cfg["from"] = res
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/rekt/resources/eventpolicy/eventpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Example_full() {
}

cfgFn := []manifest.CfgFn{
eventpolicy.WithTos([]v1alpha1.EventPolicySpecTo{
eventpolicy.WithTo([]v1alpha1.EventPolicySpecTo{
{
Ref: &v1alpha1.EventPolicyToReference{
Name: "my-broker",
Expand Down Expand Up @@ -111,8 +111,8 @@ func Example_full() {
},
},
},
}),
eventpolicy.WithFroms([]v1alpha1.EventPolicySpecFrom{
}...),
eventpolicy.WithFrom([]v1alpha1.EventPolicySpecFrom{
{
Ref: &v1alpha1.EventPolicyFromReference{
APIVersion: "eventing.knative.dev/v1",
Expand All @@ -124,7 +124,7 @@ func Example_full() {
{
Sub: ptr.String("my-sub"),
},
}),
}...),
}

for _, fn := range cfgFn {
Expand Down

0 comments on commit 1683138

Please sign in to comment.