From 5eed44f8f394a467a382acb82199993fb38c6bb8 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 25 Aug 2019 10:58:02 +0200 Subject: [PATCH] Adds function GetResponseID() for reading "id" --- common/utilities.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/common/utilities.go b/common/utilities.go index 6e741e4..e89fa23 100644 --- a/common/utilities.go +++ b/common/utilities.go @@ -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) {