[maint] added NewOrEmpty for optional SHA value (#1218)

ritik/code-1773
Enver Bisevac 2024-04-15 14:39:06 +00:00 committed by Harness
parent 9414262618
commit 650b6ba7df
1 changed files with 11 additions and 0 deletions

View File

@ -45,6 +45,7 @@ type SHA struct {
str string
}
// New creates and validates SHA from the value.
func New(value string) (SHA, error) {
value = strings.TrimSpace(value)
value = strings.ToLower(value)
@ -56,6 +57,15 @@ func New(value string) (SHA, error) {
}, nil
}
// NewOrEmpty returns None if value is empty otherwise it will try to create
// and validate new SHA object.
func NewOrEmpty(value string) (SHA, error) {
if value == "" {
return None, nil
}
return New(value)
}
func (s SHA) GobEncode() ([]byte, error) {
buffer := &bytes.Buffer{}
err := gob.NewEncoder(buffer).Encode(s.str)
@ -113,6 +123,7 @@ func (s SHA) Equal(val SHA) bool {
return s.str == val.str
}
// Must returns sha if there is an error it will panic.
func Must(value string) SHA {
sha, err := New(value)
if err != nil {