Skip to content

Commit b9ace5b

Browse files
committed
Merge pull request #173 from manyminds/default_response
api2go.Response as default for api2go.Responder
2 parents a60bf6a + fd4edb3 commit b9ace5b

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

api_public.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,28 @@ func (m JSONContentMarshaler) Marshal(i interface{}) ([]byte, error) {
121121
func (m JSONContentMarshaler) Unmarshal(data []byte, i interface{}) error {
122122
return json.Unmarshal(data, i)
123123
}
124+
125+
//The Response struct implements api2go.Responder and can be used as a default
126+
//implementation for your responses
127+
//you can fill the field `Meta` with all the metadata your application needs
128+
//like license, tokens, etc
129+
type Response struct {
130+
Res interface{}
131+
Code int
132+
Meta map[string]interface{}
133+
}
134+
135+
// Metadata returns additional meta data
136+
func (r Response) Metadata() map[string]interface{} {
137+
return r.Meta
138+
}
139+
140+
// Result returns the actual payload
141+
func (r Response) Result() interface{} {
142+
return r.Res
143+
}
144+
145+
// StatusCode sets the return status code
146+
func (r Response) StatusCode() int {
147+
return r.Code
148+
}

api_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,6 @@ func (i invalid) GetID() string {
2222
return "invalid"
2323
}
2424

25-
type Response struct {
26-
Meta map[string]interface{}
27-
Res interface{}
28-
Code int
29-
}
30-
31-
func (r Response) Metadata() map[string]interface{} {
32-
return r.Meta
33-
}
34-
35-
func (r Response) Result() interface{} {
36-
return r.Res
37-
}
38-
39-
func (r Response) StatusCode() int {
40-
return r.Code
41-
}
42-
4325
type Post struct {
4426
ID string `jsonapi:"-"`
4527
Title string

0 commit comments

Comments
 (0)