gitlab.com/evatix-go/core@v1.3.55/coredata/coredynamic/SimpleResult.go (about) 1 package coredynamic 2 3 import ( 4 "errors" 5 "reflect" 6 7 "gitlab.com/evatix-go/core/constants" 8 "gitlab.com/evatix-go/core/errcore" 9 "gitlab.com/evatix-go/core/internal/strutilinternal" 10 ) 11 12 type SimpleResult struct { 13 Dynamic 14 Result interface{} 15 Message string 16 err error 17 } 18 19 func InvalidSimpleResultNoMessage() *SimpleResult { 20 return &SimpleResult{ 21 Result: nil, 22 Dynamic: NewDynamic(nil, false), 23 Message: constants.EmptyString, 24 } 25 } 26 27 func InvalidSimpleResult( 28 invalidMessage string, 29 ) *SimpleResult { 30 return &SimpleResult{ 31 Result: nil, 32 Dynamic: NewDynamic(nil, false), 33 Message: invalidMessage, 34 } 35 } 36 37 func NewSimpleResultValid( 38 result interface{}, 39 ) *SimpleResult { 40 return &SimpleResult{ 41 Result: result, 42 Dynamic: NewDynamic(result, true), 43 Message: constants.EmptyString, 44 } 45 } 46 47 func NewSimpleResult( 48 result interface{}, 49 isValid bool, 50 invalidMessage string, 51 ) *SimpleResult { 52 return &SimpleResult{ 53 Result: result, 54 Dynamic: NewDynamic(result, isValid), 55 Message: invalidMessage, 56 } 57 } 58 59 func (it *SimpleResult) GetErrorOnTypeMismatch( 60 typeMatch reflect.Type, 61 isIncludeInvalidMessage bool, 62 ) error { 63 if it.IsReflectTypeOf(typeMatch) { 64 return nil 65 } 66 67 typeMismatchMessage := errcore.CombineWithMsgType( 68 errcore.TypeMismatchType, 69 "Current type - ["+it.ReflectTypeName()+"], expected type", 70 typeMatch) + constants.NewLineUnix 71 72 if !isIncludeInvalidMessage { 73 return errors.New(typeMismatchMessage) 74 } 75 76 return errors.New(typeMismatchMessage + it.Message) 77 } 78 79 func (it *SimpleResult) InvalidError() error { 80 if it.err != nil { 81 return it.err 82 } 83 84 if strutilinternal.IsEmptyOrWhitespace(it.Message) { 85 return nil 86 } 87 88 if it.err == nil { 89 it.err = errors.New(it.Message) 90 } 91 92 return it.err 93 } 94 95 func (it *SimpleResult) Clone() SimpleResult { 96 return SimpleResult{ 97 Dynamic: it.Dynamic, 98 Result: it.Result, 99 Message: it.Message, 100 } 101 } 102 103 func (it *SimpleResult) ClonePtr() *SimpleResult { 104 if it == nil { 105 return nil 106 } 107 108 return &SimpleResult{ 109 Dynamic: it.Dynamic, 110 Result: it.Result, 111 Message: it.Message, 112 } 113 }