-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Function to check nil Identifier (#97)
* Validation function for resource Identifier * Renamed: Valid -> Nil in order to avoid confusions
- Loading branch information
1 parent
4ff6030
commit 923006b
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package resource | ||
|
||
// String method calls proto.CompactTextString which returns "<nil>" string | ||
// in case if passed proto.Message is nil. | ||
// The "<nil>" string is common representation of nil value in Go code. See fmt package. | ||
const pbnil = "<nil>" | ||
|
||
// Nil reports whether id is empty identifier or not. | ||
// The id is empty if it is either nil or could be converted to the empty string by its String method. | ||
func Nil(id *Identifier) (ok bool) { | ||
// comparison with pbnil is mostly paranoid check | ||
// should never happen | ||
if id == nil || id.String() == "" || id.String() == pbnil { | ||
return true | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package resource | ||
|
||
import "testing" | ||
|
||
func TestNil(t *testing.T) { | ||
tcases := []struct { | ||
Identifier *Identifier | ||
Expected bool | ||
}{ | ||
{ | ||
Identifier: nil, | ||
Expected: true, | ||
}, | ||
{ | ||
Identifier: &Identifier{}, | ||
Expected: true, | ||
}, | ||
{ | ||
Identifier: &Identifier{ResourceId: "uuid"}, | ||
Expected: false, | ||
}, | ||
} | ||
|
||
for n, tc := range tcases { | ||
if v := Nil(tc.Identifier); v != tc.Expected { | ||
t.Errorf("%d: invalid result %t, expected %t", n, v, tc.Expected) | ||
} | ||
} | ||
} |