Created
April 28, 2023 20:19
-
-
Save pedrovasconcellos/4de37edcf693e0a27b095535822a3796 to your computer and use it in GitHub Desktop.
Result class in Go Language
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
type ResultT struct { | |
Response interface{} | |
Errors map[string]string | |
} | |
func NewResultT() ResultT { | |
return ResultT{ | |
Response: nil, | |
Errors: make(map[string]string), | |
} | |
} | |
func WithResponse(response interface{}) ResultT { | |
return ResultT{ | |
Response: response, | |
Errors: make(map[string]string), | |
} | |
} | |
func WithErrors(errors map[string]string) ResultT { | |
return ResultT{ | |
Response: nil, | |
Errors: errors, | |
} | |
} | |
func WithResponseAndErrors(response interface{}, errors map[string]string) ResultT { | |
return ResultT{ | |
Response: response, | |
Errors: errors, | |
} | |
} | |
func WithResponseAndError(response interface{}, parameterError string, error string) ResultT { | |
errors := make(map[string]string) | |
errors[parameterError] = error | |
return ResultT{ | |
Response: response, | |
Errors: errors, | |
} | |
} | |
func (r *ResultT) AddError(parameter string, error string) { | |
r.Errors[parameter] = error | |
} | |
func (r *ResultT) ContainsErrorDescription(errorDescription string) bool { | |
for _, v := range r.Errors { | |
if v == errorDescription { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment