Skip to content

Commit

Permalink
PolicyFromSpec: return nil on error
Browse files Browse the repository at this point in the history
Returning nil wrapped in Policy interface along with error returns in an
interface with a nil value. This can't be used to perform a nil check.
To make it safe, turn nil value on error.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Sep 20, 2021
1 parent 687ece4 commit d03c7f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/policy/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ func PolicerFromSpec(choice imagev1.ImagePolicyChoice) (Policer, error) {
return nil, fmt.Errorf("given ImagePolicyChoice object is invalid")
}

return p, err
if err != nil {
return nil, err
}
return p, nil
}
9 changes: 9 additions & 0 deletions internal/policy/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ func TestFactory_PolicerFromSpec(t *testing.T) {
if err != nil {
t.Error("should not return error")
}

// A nil checkable Policer for invalid policy.
p, err := PolicerFromSpec(imagev1.ImagePolicyChoice{SemVer: &imagev1.SemVerPolicy{Range: "*-*"}})
if err == nil {
t.Error("should return error")
}
if p != nil {
t.Error("should be nil")
}
}

0 comments on commit d03c7f8

Please sign in to comment.