Repository navigation
Expand file tree
/
Copy pathcontent.go
More file actions
155 lines (134 loc) · 5.53 KB
/
Copy pathcontent.go
File metadata and controls
155 lines (134 loc) · 5.53 KB
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
package apihandler
import (
"errors"
"net/http"
)
// ErrNilTransformer is returned by Resource.Content when the body was
// constructed without a Transformer.
var ErrNilTransformer = errors.New("apihandler: nil transformer")
// Result is the value a Handler returns: a status code, optional extra
// headers, and a lazy body. The zero value is a valid empty 200 OK
// response.
type Result struct {
// Status is the HTTP status code; 0 means http.StatusOK. Writer.Handler
// coerces a status outside the 200-599 range of valid final statuses
// to 500 before writing.
Status int
// Header holds optional extra headers copied onto the response. Header
// is ignored when the handler returns a non-nil error, when the body's
// Content fails, or when encoding fails before the response is
// committed — error responses never carry the success headers.
Header http.Header
// Body is the lazy response body; nil means no body. A body attached to
// a 204 or 304 Result is not evaluated and not written.
Body Content
}
// SetHeader sets a response header with http.Header.Set semantics,
// allocating Header on first use.
func (res *Result) SetHeader(key, value string) {
if res.Header == nil {
res.Header = http.Header{}
}
res.Header.Set(key, value)
}
// Content is the lazy body of a Result: the response payload is not
// produced until the edge calls Content, so middleware between handler and
// Writer can decorate or replace the body before any transformation runs.
//
// Implementations must be free of side effects and safe to evaluate more
// than once; the Writer and any decorating middleware may each evaluate
// the body. A Content whose evaluation returns a nil payload with a nil
// error produces an empty response — the Writer writes the status and no
// bytes. To encode a JSON null instead, return a typed nil, for example
// (*User)(nil).
//
// When decorating a Result's body by hand, capture the previous body in a
// local variable before reassigning the field; a ContentFunc whose closure
// reads the field it was assigned to recurses forever. MapBody performs the
// capture correctly and is the recommended path.
type Content interface {
// Content produces the response payload.
Content() (any, error)
}
// ContentFunc adapts an ordinary function to the Content interface, in the
// manner of http.HandlerFunc.
type ContentFunc func() (any, error)
// Content calls the underlying function.
func (content ContentFunc) Content() (any, error) {
return content()
}
// Resourcer exposes the untransformed domain object behind a Content body,
// so middleware (HAL link builders, policy filters) can inspect it without
// generic type assertions. Resource implements it, and bodies produced by
// MapBody forward it from the body they wrap.
//
// The returned value is an any and may be a typed nil (for example a nil
// *User boxed in the interface): test it with a type assertion, never with
// a bare == nil comparison. Resource returns nil, or a typed nil boxed in
// any, when no usable domain object is available; after MapBody the body
// always implements Resourcer, forwarding the inner view or nil.
type Resourcer interface {
// Resource returns the untransformed domain object.
Resource() any
}
// Resource binds a domain object, its request, and its transformer,
// deferring transformation until the edge asks for the content. It is the
// body type produced by the CRUD constructors. Transformer must be
// non-nil; Content returns an error otherwise. The bound request is only
// valid for the lifetime of the request; evaluate Content before the
// handler returns (Writer does this). Do not retain a Result for later
// evaluation.
type Resource[T, R any] struct {
Request *http.Request
Value T
Transformer Transformer[T, R]
}
// Content transforms the bound domain object into its response
// representation. It returns ErrNilTransformer when Transformer is nil.
func (res Resource[T, R]) Content() (any, error) {
if res.Transformer == nil {
return nil, ErrNilTransformer
}
return res.Transformer.Transform(res.Request, res.Value)
}
// Resource returns the untransformed domain object, satisfying Resourcer.
// A nil Value is boxed in any as a typed nil; assert to the domain type
// rather than comparing to nil.
func (res Resource[T, R]) Resource() any {
return res.Value
}
// MapBody returns a copy of res whose body content is passed through
// transform after the original body produces it. It is a no-op when res has
// no body. The returned body still implements Resourcer when the original
// body did, so stacked decorators preserve middleware access to the
// untransformed domain object.
func MapBody(res Result, transform func(content any) (any, error)) Result {
if res.Body == nil {
return res
}
res.Body = mappedContent{inner: res.Body, transform: transform}
return res
}
// mappedContent decorates a Content body with a transformation while
// forwarding the Resourcer view of the wrapped body.
type mappedContent struct {
inner Content
transform func(content any) (any, error)
}
// Content produces the wrapped body's payload and passes it through the
// mapping function.
func (mapped mappedContent) Content() (any, error) {
content, err := mapped.inner.Content()
if err != nil {
return nil, err
}
return mapped.transform(content)
}
// Resource forwards the untransformed domain object from the wrapped body,
// or returns nil when the wrapped body does not implement Resourcer.
func (mapped mappedContent) Resource() any {
if resourcer, ok := mapped.inner.(Resourcer); ok {
return resourcer.Resource()
}
return nil
}