ExtractInto*Ptr methods for Result
diff --git a/results.go b/results.go
index 8cca421..8d262ec 100644
--- a/results.go
+++ b/results.go
@@ -3,8 +3,10 @@
import (
"bytes"
"encoding/json"
+ "fmt"
"io"
"net/http"
+ "reflect"
"strconv"
"time"
)
@@ -60,6 +62,59 @@
return err
}
+func (r Result) extractIntoPtr(to interface{}, label string) error {
+ if r.Err != nil {
+ return r.Err
+ }
+
+ t := reflect.TypeOf(to)
+ if k := t.Kind(); k != reflect.Ptr {
+ return fmt.Errorf("Expected pointer, got %v", k)
+ }
+ t = t.Elem()
+ switch t.Kind() {
+ case reflect.Struct, reflect.Slice:
+ default:
+ return fmt.Errorf("Invalid type: %v", t)
+ }
+
+ var (
+ b []byte
+ err error
+ )
+
+ switch label {
+ case "":
+ var m interface{}
+ err = r.ExtractInto(&m)
+ if err != nil {
+ return err
+ }
+ b, err = json.Marshal(m)
+ default:
+ var m map[string]interface{}
+ err = r.ExtractInto(&m)
+ if err != nil {
+ return err
+ }
+ b, err = json.Marshal(m[label])
+ }
+ if err != nil {
+ return err
+ }
+
+ err = json.Unmarshal(b, &to)
+ return err
+}
+
+func (r Result) ExtractIntoStructPtr(to interface{}, label string) error {
+ return r.extractIntoPtr(to, label)
+}
+
+func (r Result) ExtractIntoSlicePtr(to interface{}, label string) error {
+ return r.extractIntoPtr(to, label)
+}
+
// PrettyPrintJSON creates a string containing the full response body as
// pretty-printed JSON. It's useful for capturing test fixtures and for
// debugging extraction bugs. If you include its output in an issue related to