-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
63 lines (56 loc) · 1.62 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"encoding/json"
"errors"
"time"
"github.com/globalsign/mgo/bson"
)
var (
collectionDummy = "dummy"
)
type requestModel struct {
Content string `json:"content"` // body to response
Charset string `json:"charset"` // charset
ContentType string `json:"content_type"` // http 'Content-Type'
Status int `json:"status"` // http status
Headers map[string]string `json:"headers"`
}
// validate requestModel. do not trust any input
func (m *requestModel) validate() error {
var err error
if m.Status == 0 {
err = errors.New("status is not set")
} else if m.ContentType == "" {
err = errors.New("content type is empty")
} else if m.Charset == "" {
err = errors.New("charset is empty")
}
return err
}
// dummyModel is a model for manipulating databases' data
type dummyModel struct {
ID bson.ObjectId `bson:"_id"`
Version string // API version. v1, v2 ... vn
Content string // body to response
Charset string // charset
ContentType string // http 'Content-Type'
Headers string // stringify JSON
Status int // http status
CreatedAt time.Time // Time to created this record
}
func (d *dummyModel) updateWithRequestData(m *requestModel) error {
d.Content = m.Content
d.Charset = m.Charset
d.ContentType = m.ContentType
d.Status = m.Status
d.CreatedAt = time.Now()
d.Version = apiVersion
// convert map to JSON
jsonBytes, err := json.Marshal(m.Headers)
if err != nil {
return err
}
// stringify it
d.Headers = string(jsonBytes)
return nil
}