Skip to content

Commit

Permalink
update strings result array parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft committed Feb 14, 2022
1 parent 5199963 commit 9c17b30
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"strconv"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9c17b30

Please sign in to comment.