This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
auto.go
213 lines (183 loc) · 4.91 KB
/
auto.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package render
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"path"
"reflect"
"strings"
"github.com/gobuffalo/flect/name"
)
var errNoID = fmt.Errorf("no ID on model")
// ErrRedirect indicates to Context#Render that this is a
// redirect and a template shouldn't be rendered.
type ErrRedirect struct {
Status int
URL string
}
func (ErrRedirect) Error() string {
return ""
}
// Auto figures out how to render the model based information
// about the request and the name of the model. Auto supports
// automatic rendering of HTML, JSON, and XML. Any status code
// give to Context#Render between 300 - 400 will be respected
// by Auto. Other status codes are not.
/*
# Rules for HTML template lookup:
GET /users - users/index.html
GET /users/id - users/show.html
GET /users/new - users/new.html
GET /users/id/edit - users/edit.html
POST /users - (redirect to /users/id or render user/new.html)
PUT /users/edit - (redirect to /users/id or render user/edit.html)
DELETE /users/id - redirect to /users
*/
func Auto(ctx context.Context, i interface{}) Renderer {
e := New(Options{})
return e.Auto(ctx, i)
}
// Auto figures out how to render the model based information
// about the request and the name of the model. Auto supports
// automatic rendering of HTML, JSON, and XML. Any status code
// give to Context#Render between 300 - 400 will be respected
// by Auto. Other status codes are not.
/*
# Rules for HTML template lookup:
GET /users - users/index.html
GET /users/id - users/show.html
GET /users/new - users/new.html
GET /users/id/edit - users/edit.html
POST /users - (redirect to /users/id or render user/new.html)
PUT /users/edit - (redirect to /users/id or render user/edit.html)
DELETE /users/id - redirect to /users
*/
func (e *Engine) Auto(ctx context.Context, i interface{}) Renderer {
ct, _ := ctx.Value("contentType").(string)
if ct == "" {
ct = e.DefaultContentType
}
ct = strings.TrimSpace(strings.ToLower(ct))
if strings.Contains(ct, "json") {
return e.JSON(i)
}
if strings.Contains(ct, "xml") {
return e.XML(i)
}
return htmlAutoRenderer{
Engine: e,
model: i,
}
}
type htmlAutoRenderer struct {
*Engine
model interface{}
}
func (htmlAutoRenderer) ContentType() string {
return "text/html"
}
func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error {
n := name.New(ir.typeName())
pname := name.New(n.Pluralize().String())
isPlural := ir.isPlural()
if isPlural {
data[pname.VarCasePlural().String()] = ir.model
} else {
data[n.VarCaseSingle().String()] = ir.model
}
templatePrefix := pname.File()
if pf, ok := data["template_prefix"].(string); ok {
templatePrefix = name.New(pf)
}
switch data["method"] {
case "PUT", "POST", "DELETE":
if err := ir.redirect(pname, w, data); err != nil {
var er ErrRedirect
if errors.As(err, &er) && er.Status >= http.StatusMultipleChoices && er.Status < http.StatusBadRequest {
return err
}
if data["method"] == "PUT" {
return ir.HTML(path.Join(templatePrefix.String(), "edit.html")).Render(w, data)
}
return ir.HTML(path.Join(templatePrefix.String(), "new.html")).Render(w, data)
}
return nil
}
cp, ok := data["current_path"].(string)
defCase := func() error {
return ir.HTML(path.Join(templatePrefix.String(), "index.html")).Render(w, data)
}
if !ok {
return defCase()
}
if strings.HasSuffix(cp, "/edit/") {
return ir.HTML(path.Join(templatePrefix.String(), "edit.html")).Render(w, data)
}
if strings.HasSuffix(cp, "/new/") {
return ir.HTML(path.Join(templatePrefix.String(), "new.html")).Render(w, data)
}
if !isPlural {
return ir.HTML(path.Join(templatePrefix.String(), "show.html")).Render(w, data)
}
return defCase()
}
func (ir htmlAutoRenderer) redirect(name name.Ident, w io.Writer, data Data) error {
rv := reflect.Indirect(reflect.ValueOf(ir.model))
f := rv.FieldByName("ID")
if !f.IsValid() {
return errNoID
}
fi := f.Interface()
rt := reflect.TypeOf(fi)
zero := reflect.Zero(rt)
if fi != zero.Interface() {
m, ok := data["method"].(string)
if !ok {
m = "GET"
}
url := fmt.Sprint(data["current_path"])
id := fmt.Sprint(f.Interface())
url = strings.TrimSuffix(url, "/")
switch m {
case "DELETE":
url = strings.TrimSuffix(url, id)
default:
if !strings.HasSuffix(url, id) {
url = path.Join(url, id)
}
}
code := http.StatusFound
if i, ok := data["status"].(int); ok {
if i >= http.StatusMultipleChoices {
code = i
}
}
return ErrRedirect{
Status: code,
URL: url,
}
}
return errNoID
}
func (ir htmlAutoRenderer) typeName() string {
rv := reflect.Indirect(reflect.ValueOf(ir.model))
rt := rv.Type()
switch rt.Kind() {
case reflect.Slice, reflect.Array:
el := rt.Elem()
return el.Name()
default:
return rt.Name()
}
}
func (ir htmlAutoRenderer) isPlural() bool {
rv := reflect.Indirect(reflect.ValueOf(ir.model))
rt := rv.Type()
switch rt.Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
return true
}
return false
}