mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Adds function GetResponseID() for reading "id"
This commit is contained in:
parent
e0746f112f
commit
5eed44f8f3
1 changed files with 29 additions and 0 deletions
|
@ -36,6 +36,35 @@ func ProvideErrorResponse(c *gin.Context, err error) bool {
|
|||
return false // No error
|
||||
}
|
||||
|
||||
func GetResponseID(resp *bytes.Buffer) (int, error) {
|
||||
|
||||
// Transform bytes buffer into byte slice
|
||||
respBytes := []byte(resp.String())
|
||||
|
||||
// Map JSON response to a map[string]map[string]interface{}
|
||||
var respRemapped map[string]map[string]interface{}
|
||||
err := json.Unmarshal(respBytes, &respRemapped)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("Unmarshal failed for respRemapped %v", err)
|
||||
}
|
||||
|
||||
// Get an arbitrary key from tha map. The only key (entry) of
|
||||
// course is the model's name. With that trick we do not have to
|
||||
// pass the higher level key as argument.
|
||||
for arbitrary_key := range respRemapped {
|
||||
|
||||
// The marshaler turns numerical values into float64 types so we
|
||||
// first have to make a type assertion to the interface and then
|
||||
// the conversion to integer before returning
|
||||
id, ok := respRemapped[arbitrary_key]["id"].(float64)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("Cannot type assert respRemapped")
|
||||
}
|
||||
return int(id), nil
|
||||
}
|
||||
return 0, fmt.Errorf("GetResponse reached exit")
|
||||
}
|
||||
|
||||
func LengthOfResponse(router *gin.Engine, token string, url string,
|
||||
method string, body []byte) (int, error) {
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue