github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/net/paginated_resources.go (about)

     1  package net
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  )
     7  
     8  func NewPaginatedResources(exampleResource interface{}) PaginatedResources {
     9  	return PaginatedResources{
    10  		resourceType: reflect.TypeOf(exampleResource),
    11  	}
    12  }
    13  
    14  type PaginatedResources struct {
    15  	NextURL        string          `json:"next_url"`
    16  	ResourcesBytes json.RawMessage `json:"resources"`
    17  	resourceType   reflect.Type
    18  }
    19  
    20  func (pr PaginatedResources) Resources() ([]interface{}, error) {
    21  	slicePtr := reflect.New(reflect.SliceOf(pr.resourceType))
    22  	err := json.Unmarshal([]byte(pr.ResourcesBytes), slicePtr.Interface())
    23  	slice := reflect.Indirect(slicePtr)
    24  
    25  	contents := make([]interface{}, 0, slice.Len())
    26  	for i := 0; i < slice.Len(); i++ {
    27  		contents = append(contents, slice.Index(i).Interface())
    28  	}
    29  	return contents, err
    30  }