github.com/gophercloud/gophercloud@v1.11.0/docs/contributor-tutorial/.template/results.go (about) 1 package RESOURCE 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // RESOURCE represents... 9 type Resource struct { 10 } 11 12 type commonResult struct { 13 gophercloud.Result 14 } 15 16 // GetResult is the response from a Get operation. Call its Extract method 17 // to interpret it as a RESOURCE. 18 type GetResult struct { 19 commonResult 20 } 21 22 // CreateResult is the response from a Create operation. Call its Extract method 23 // to interpret it as a RESOURCE. 24 type CreateResult struct { 25 commonResult 26 } 27 28 // DeleteResult is the response from a Delete operation. Call its ExtractErr to 29 // determine if the request succeeded or failed. 30 type DeleteResult struct { 31 gophercloud.ErrResult 32 } 33 34 // UpdateResult is the result of an Update request. Call its Extract method to 35 // interpret it as a RESOURCE. 36 type UpdateResult struct { 37 commonResult 38 } 39 40 // ResourcePage is a single page of RESOURCE results. 41 type ResourcePage struct { 42 pagination.LinkedPageBase 43 } 44 45 // IsEmpty determines whether or not a page of RESOURCES contains any results. 46 func (r ResourcePage) IsEmpty() (bool, error) { 47 if r.StatusCode == 204 { 48 return true, nil 49 } 50 51 resources, err := ExtractResources(r) 52 return len(resources) == 0, err 53 } 54 55 // NextPageURL extracts the "next" link from the links section of the result. 56 func (r ResourcePage) NextPageURL() (string, error) { 57 var s struct { 58 Links struct { 59 Next string `json:"next"` 60 Previous string `json:"previous"` 61 } `json:"links"` 62 } 63 err := r.ExtractInto(&s) 64 if err != nil { 65 return "", err 66 } 67 return s.Links.Next, err 68 } 69 70 // ExtractResources returns a slice of Resources contained in a single page of 71 // results. 72 func ExtractResources(r pagination.Page) ([]Resource, error) { 73 var s struct { 74 Resources []Resource `json:"resources"` 75 } 76 err := (r.(ResourcePage)).ExtractInto(&s) 77 return s.Resources, err 78 } 79 80 // Extract interprets any commonResult as a Resource. 81 func (r commonResult) Extract() (*Resource, error) { 82 var s struct { 83 Resource *Resource `json:"resource"` 84 } 85 err := r.ExtractInto(&s) 86 return s.Resource, err 87 }