2022-04-12 12:12:14 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import "encoding/json"
|
|
|
|
|
2022-09-30 16:04:22 +00:00
|
|
|
type jsonResponseError struct {
|
|
|
|
Error *string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonResponseSuccess struct {
|
2022-04-12 12:12:14 +00:00
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareJSONResponse(result interface{}, err error) string {
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
2022-09-30 16:04:22 +00:00
|
|
|
errResponse := jsonResponseError{
|
2022-04-12 12:12:14 +00:00
|
|
|
Error: &errStr,
|
|
|
|
}
|
|
|
|
response, _ := json.Marshal(&errResponse)
|
|
|
|
return string(response)
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:04:22 +00:00
|
|
|
data, err := json.Marshal(jsonResponseSuccess{Result: result})
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponse(nil, err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeJSONResponse(err error) string {
|
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
2022-09-30 16:04:22 +00:00
|
|
|
outBytes, _ := json.Marshal(jsonResponseError{Error: &errStr})
|
|
|
|
return string(outBytes)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 16:04:22 +00:00
|
|
|
out := jsonResponseSuccess{
|
|
|
|
Result: true,
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
|
|
|
|
return string(outBytes)
|
|
|
|
}
|