github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/2-design-patterns/ch04-solid/02_maybe/src/maybe/string_either.go (about) 1 package maybe 2 3 import ( 4 "fmt" 5 ) 6 7 type StringEither interface { 8 SuccessOrFailure 9 Succeeded() StringOption 10 Failed() ErrorOption 11 } 12 13 type eitherString struct { 14 val string 15 err error 16 } 17 18 func EitherSuccess(i string) StringEither { 19 return eitherString{val: i} 20 } 21 22 func EitherFailure(err error) StringEither { 23 return eitherString{err: err} 24 } 25 26 func (e eitherString) Success() bool { 27 return e.err == nil 28 } 29 30 func (e eitherString) Failure() bool { 31 return e.err != nil 32 } 33 34 func (e eitherString) Succeeded() StringOption { 35 if e.err == nil { 36 return SomeString(e.val) 37 } 38 return EmptyString() 39 } 40 41 func (e eitherString) Failed() ErrorOption { 42 if e.err != nil { 43 return SomeError(e.err) 44 } 45 return EmptyError() 46 } 47 48 func (e eitherString) String() string { 49 if e.Success() { 50 return fmt.Sprintf("Succeeded(%s)", e.val) 51 } 52 return fmt.Sprintf("Failed(%s)", e.err) 53 }