Skip to content

Commit

Permalink
Switch functional options with no args to vars
Browse files Browse the repository at this point in the history
This switches functional arguments with no variables to not taking
arguments, meaning that they can be called like

```go
r.Update(ctx, obj, client.UpdateDryRunAll)
```

instead of

```go
r.Update(ctx, obj, client.UpdateDryRunAll())
```

Options with arguments still get called as functions.
  • Loading branch information
DirectXMan12 committed May 17, 2019
1 parent 0a67a3c commit 9c43b2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
14 changes: 7 additions & 7 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ var _ = Describe("Client", func() {
Expect(cl).NotTo(BeNil())

By("creating the object (with DryRun)")
err = cl.Create(context.TODO(), dep, client.CreateDryRunAll())
err = cl.Create(context.TODO(), dep, client.CreateDryRunAll)
Expect(err).NotTo(HaveOccurred())

actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
Expand Down Expand Up @@ -415,7 +415,7 @@ var _ = Describe("Client", func() {
})

By("creating the object")
err = cl.Create(context.TODO(), u, client.CreateDryRunAll())
err = cl.Create(context.TODO(), u, client.CreateDryRunAll)
Expect(err).NotTo(HaveOccurred())

actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
Expand Down Expand Up @@ -1074,7 +1074,7 @@ var _ = Describe("Client", func() {
Expect(err).NotTo(HaveOccurred())

By("patching the Deployment with dry-run")
err = cl.Patch(context.TODO(), dep, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll())
err = cl.Patch(context.TODO(), dep, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll)
Expect(err).NotTo(HaveOccurred())

By("validating patched Deployment doesn't have the new annotation")
Expand Down Expand Up @@ -1183,7 +1183,7 @@ var _ = Describe("Client", func() {
Kind: "Deployment",
Version: "v1",
})
err = cl.Patch(context.TODO(), u, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll())
err = cl.Patch(context.TODO(), u, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll)
Expect(err).NotTo(HaveOccurred())

By("validating patched Deployment does not have the new annotation")
Expand Down Expand Up @@ -2000,7 +2000,7 @@ var _ = Describe("Client", func() {
Describe("CreateOptions", func() {
It("should allow setting DryRun to 'all'", func() {
co := &client.CreateOptions{}
client.CreateDryRunAll()(co)
client.CreateDryRunAll(co)
all := []string{metav1.DryRunAll}
Expect(co.AsCreateOptions().DryRun).To(Equal(all))
})
Expand Down Expand Up @@ -2141,7 +2141,7 @@ var _ = Describe("Client", func() {
Describe("UpdateOptions", func() {
It("should allow setting DryRun to 'all'", func() {
uo := &client.UpdateOptions{}
client.UpdateDryRunAll()(uo)
client.UpdateDryRunAll(uo)
all := []string{metav1.DryRunAll}
Expect(uo.AsUpdateOptions().DryRun).To(Equal(all))
})
Expand All @@ -2157,7 +2157,7 @@ var _ = Describe("Client", func() {
Describe("PatchOptions", func() {
It("should allow setting DryRun to 'all'", func() {
po := &client.PatchOptions{}
client.PatchDryRunAll()(po)
client.PatchDryRunAll(po)
all := []string{metav1.DryRunAll}
Expect(po.AsPatchOptions().DryRun).To(Equal(all))
})
Expand Down
35 changes: 15 additions & 20 deletions pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ type CreateOptionFunc func(*CreateOptions)

// CreateDryRunAll is a functional option that sets the DryRun
// field of a CreateOptions struct to metav1.DryRunAll.
func CreateDryRunAll() CreateOptionFunc {
return func(opts *CreateOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}
var CreateDryRunAll CreateOptionFunc = func(opts *CreateOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}

// DeleteOptions contains options for delete requests. It's generally a subset
Expand Down Expand Up @@ -339,10 +337,8 @@ type UpdateOptionFunc func(*UpdateOptions)

// UpdateDryRunAll is a functional option that sets the DryRun
// field of a UpdateOptions struct to metav1.DryRunAll.
func UpdateDryRunAll() UpdateOptionFunc {
return func(opts *UpdateOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}
var UpdateDryRunAll UpdateOptionFunc = func(opts *UpdateOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}

// PatchOptions contains options for patch requests.
Expand Down Expand Up @@ -398,22 +394,21 @@ func (o *PatchOptions) AsPatchOptions() *metav1.PatchOptions {
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
type PatchOptionFunc func(*PatchOptions)

// ForceOwnership sets the Force option, indicating that
// in case of conflicts with server-side apply, the client should
// acquire ownership of the conflicting field. Most controllers
// should use this.
var ForceOwnership PatchOptionFunc = func(opts *PatchOptions) {
definitelyTrue := true
opts.Force = &definitelyTrue
}

// PatchDryRunAll is a functional option that sets the DryRun
// field of a PatchOptions struct to metav1.DryRunAll.
func PatchDryRunAll() PatchOptionFunc {
return func(opts *PatchOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}
var PatchDryRunAll PatchOptionFunc = func(opts *PatchOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}

// ForceOwnership is a functional option that sets the Force
// field of a PatchOptions struct to true.
func ForceOwnership() PatchOptionFunc {
force := true
return func(opts *PatchOptions) {
opts.Force = &force
}
}

// FieldOwner set the field manager name for the given server-side apply patch.
func FieldOwner(name string) PatchOptionFunc {
Expand Down

0 comments on commit 9c43b2a

Please sign in to comment.