-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
34 lines (28 loc) · 875 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package profitbricks
import (
"github.com/ionos-cloud/sdk-go/v5"
"reflect"
)
func fillInResponse(obj interface{}, response *ionoscloud.APIResponse) {
if response == nil || response.Response == nil {
return
}
objValue := reflect.ValueOf(obj)
if objValue.Type().Kind() != reflect.Ptr {
panic("fillInResponse() requires a pointer")
}
headersVal := reflect.ValueOf(obj).Elem().FieldByName("Headers")
if headersVal.IsValid() {
h := response.Header
headersVal.Set(reflect.ValueOf(&h))
}
responseVal := reflect.ValueOf(obj).Elem().FieldByName("Response")
if responseVal.IsValid() {
body := string(response.Payload)
responseVal.Set(reflect.ValueOf(body))
}
statusVal := reflect.ValueOf(obj).Elem().FieldByName("StatusCode")
if statusVal.IsValid() && statusVal.Type().Kind() == reflect.Int {
statusVal.Set(reflect.ValueOf(response.StatusCode))
}
}