github.com/seeker-insurance/kit@v0.0.13/jsonapi/models_test.go (about) 1 package jsonapi 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 type BadModel struct { 9 ID int `jsonapi:"primary"` 10 } 11 12 type ModelBadTypes struct { 13 ID string `jsonapi:"primary,badtypes"` 14 StringField string `jsonapi:"attr,string_field"` 15 FloatField float64 `jsonapi:"attr,float_field"` 16 TimeField time.Time `jsonapi:"attr,time_field"` 17 TimePtrField *time.Time `jsonapi:"attr,time_ptr_field"` 18 } 19 20 type WithPointer struct { 21 ID *uint64 `jsonapi:"primary,with-pointers"` 22 Name *string `jsonapi:"attr,name"` 23 IsActive *bool `jsonapi:"attr,is-active"` 24 IntVal *int `jsonapi:"attr,int-val"` 25 FloatVal *float32 `jsonapi:"attr,float-val"` 26 } 27 28 type Timestamp struct { 29 ID int `jsonapi:"primary,timestamps"` 30 Time time.Time `jsonapi:"attr,timestamp,iso8601"` 31 Next *time.Time `jsonapi:"attr,next,iso8601"` 32 } 33 34 type Car struct { 35 ID *string `jsonapi:"primary,cars"` 36 Make *string `jsonapi:"attr,make,omitempty"` 37 Model *string `jsonapi:"attr,model,omitempty"` 38 Year *uint `jsonapi:"attr,year,omitempty"` 39 } 40 41 type Post struct { 42 Blog 43 ID uint64 `jsonapi:"primary,posts"` 44 BlogID int `jsonapi:"attr,blog_id"` 45 ClientID string `jsonapi:"client-id"` 46 Title string `jsonapi:"attr,title"` 47 Body string `jsonapi:"attr,body"` 48 Comments []*Comment `jsonapi:"relation,comments"` 49 LatestComment *Comment `jsonapi:"relation,latest_comment"` 50 } 51 52 type Comment struct { 53 ID int `jsonapi:"primary,comments"` 54 ClientID string `jsonapi:"client-id"` 55 PostID int `jsonapi:"attr,post_id"` 56 Body string `jsonapi:"attr,body"` 57 } 58 59 type Book struct { 60 ID uint64 `jsonapi:"primary,books"` 61 Author string `jsonapi:"attr,author"` 62 ISBN string `jsonapi:"attr,isbn"` 63 Title string `jsonapi:"attr,title,omitempty"` 64 Description *string `jsonapi:"attr,description"` 65 Pages *uint `jsonapi:"attr,pages,omitempty"` 66 PublishedAt time.Time 67 Tags []string `jsonapi:"attr,tags"` 68 } 69 70 type Blog struct { 71 ID int `jsonapi:"primary,blogs"` 72 ClientID string `jsonapi:"client-id"` 73 Title string `jsonapi:"attr,title"` 74 Posts []*Post `jsonapi:"relation,posts"` 75 CurrentPost *Post `jsonapi:"relation,current_post"` 76 CurrentPostID int `jsonapi:"attr,current_post_id"` 77 CreatedAt time.Time `jsonapi:"attr,created_at"` 78 ViewCount int `jsonapi:"attr,view_count"` 79 R *blogR `jsonapi:"embed"` 80 } 81 82 type blogR struct { 83 Comment *Comment `jsonapi:"relation,comment"` 84 } 85 86 func (b *Blog) JSONAPILinks() *Links { 87 return &Links{ 88 "self": fmt.Sprintf("https://example.com/api/blogs/%d", b.ID), 89 "comments": Link{ 90 Href: fmt.Sprintf("https://example.com/api/blogs/%d/comments", b.ID), 91 Meta: Meta{ 92 "counts": map[string]uint{ 93 "likes": 4, 94 "comments": 20, 95 }, 96 }, 97 }, 98 } 99 } 100 101 func (b *Blog) JSONAPIRelationshipLinks(relation string) *Links { 102 if relation == "posts" { 103 return &Links{ 104 "related": Link{ 105 Href: fmt.Sprintf("https://example.com/api/blogs/%d/posts", b.ID), 106 Meta: Meta{ 107 "count": len(b.Posts), 108 }, 109 }, 110 } 111 } 112 if relation == "current_post" { 113 return &Links{ 114 "self": fmt.Sprintf("https://example.com/api/posts/%s", "3"), 115 "related": Link{ 116 Href: fmt.Sprintf("https://example.com/api/blogs/%d/current_post", b.ID), 117 }, 118 } 119 } 120 return nil 121 } 122 123 func (b *Blog) JSONAPIMeta() *Meta { 124 return &Meta{ 125 "detail": "extra details regarding the blog", 126 } 127 } 128 129 func (b *Blog) JSONAPIRelationshipMeta(relation string) *Meta { 130 if relation == "posts" { 131 return &Meta{ 132 "this": map[string]interface{}{ 133 "can": map[string]interface{}{ 134 "go": []interface{}{ 135 "as", 136 "deep", 137 map[string]interface{}{ 138 "as": "required", 139 }, 140 }, 141 }, 142 }, 143 } 144 } 145 if relation == "current_post" { 146 return &Meta{ 147 "detail": "extra current_post detail", 148 } 149 } 150 return nil 151 } 152 153 type BadComment struct { 154 ID uint64 `jsonapi:"primary,bad-comment"` 155 Body string `jsonapi:"attr,body"` 156 } 157 158 func (bc *BadComment) JSONAPILinks() *Links { 159 return &Links{ 160 "self": []string{"invalid", "should error"}, 161 } 162 } 163 164 // Embedded Struct Models 165 type Engine struct { 166 NumberOfCylinders uint `jsonapi:"attr,cylinders"` 167 HorsePower uint `jsonapi:"attr,hp"` 168 } 169 170 type BlockHeater struct { 171 Watts uint `jsonapi:"attr,watts"` 172 } 173 174 type Vehicle struct { 175 ID uint `json:"id" jsonapi:"primary,car"` 176 Make string `jsonapi:"attr,make"` 177 Model string `jsonapi:"attr,model"` 178 Year uint `jsonapi:"attr,year"` 179 Engine // every car must have an engine 180 *BlockHeater // not every car will have a block heater 181 }