-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_mem.go
66 lines (53 loc) · 1.28 KB
/
context_mem.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
package starch
import (
"bytes"
"fmt"
)
type MemoryContext struct {
Headers map[string]string
Params map[string]string
Sink bytes.Buffer
Source bytes.Buffer
Status int
Vars map[string]any
}
func NewMemoryContext() *MemoryContext {
return &MemoryContext{
Headers: map[string]string{},
Params: map[string]string{},
Vars: map[string]any{},
}
}
func (t *MemoryContext) Var(key string) any {
return t.Vars[key]
}
func (t *MemoryContext) SetVar(key string, value any) {
t.Vars[key] = value
}
func (t *MemoryContext) Read(buff []byte) (int, error) {
return t.Source.Read(buff)
}
func (t *MemoryContext) Write(buff []byte) (int, error) {
return t.Sink.Write(buff)
}
func (t *MemoryContext) WriteHeader(key string, value string) error {
t.Headers[key] = value
return nil
}
func (t *MemoryContext) WriteStatus(code int) error {
t.Status = code
return nil
}
func (t *MemoryContext) Render(c Component) error {
return c.Render(t)
}
func (t *MemoryContext) WriteString(s string, args ...any) error {
_, err := t.Write([]byte(fmt.Sprintf(s, args...)))
return err
}
func (t *MemoryContext) Redirect(string) error {
return nil
}
func (t *MemoryContext) Param(key string) string {
return t.Params[key]
}