mirror of https://github.com/harness/drone.git
[maint] added NewOrEmpty for optional SHA value (#1218)
parent
9414262618
commit
650b6ba7df
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue