From 9c17b30c1585a21a90e99fdd7f2f2061f4020c54 Mon Sep 17 00:00:00 2001 From: Sagleft Date: Mon, 14 Feb 2022 16:30:36 +0200 Subject: [PATCH] update strings result array parser --- request.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/request.go b/request.go index f4235d2..a23e6dc 100644 --- a/request.go +++ b/request.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/ioutil" + "log" "net/http" "reflect" "strconv" @@ -94,11 +95,27 @@ func (c *UtopiaClient) queryResultToStringsArray(methodName string, params map[s } response, err := c.apiQuery(methodName, params) if result, ok := response["result"]; ok { + log.Println(result) //check type assertion IResult, isConvertable := result.([]string) if !isConvertable { - return nil, errors.New("failed to get result array. []string expected, " + - reflect.TypeOf(result).String() + "given") + resultType := reflect.TypeOf(result).String() + if resultType == "[]interface {}" { + IResult, isConvertable := result.([]interface{}) + if !isConvertable { + return nil, errors.New("failed to get result array. can't convert to strings array") + } + resultArray := []string{} + for _, elementRaw := range IResult { + element, isConvertable := elementRaw.(string) + if !isConvertable { + return nil, errors.New("failed to convert result array element to string") + } + resultArray = append(resultArray, element) + } + return resultArray, nil + } + return nil, errors.New("failed to get result array. []string expected, " + resultType + "given") } return IResult, err }