-
Notifications
You must be signed in to change notification settings - Fork 9
/
context.go
123 lines (108 loc) · 2.86 KB
/
context.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
/*
* Copyright 2018. bigpigeon. All rights reserved.
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*/
package toyorm
import (
"math"
)
const abortIndex int = math.MaxInt8 / 2
type HandlerFunc func(c *Context) error
type HandlersChain []HandlerFunc
type Context struct {
handlers HandlersChain
index int8
Brick *ToyBrick
Result *Result
value map[interface{}]interface{}
//err error
}
func NewContext(handlers HandlersChain, brick *ToyBrick, columns ModelRecords) *Context {
return &Context{
handlers: handlers,
index: -1,
Brick: brick,
Result: &Result{
Records: columns,
Preload: map[string]*Result{},
RecordsActions: map[int][]int{},
MiddleModelPreload: map[string]*Result{},
SimpleRelation: map[string]map[int]int{},
MultipleRelation: map[string]map[int]Pair{},
},
}
}
func (c *Context) Value(v interface{}) interface{} {
return c.value[v]
}
func (c *Context) Next() error {
c.index++
//handlerNames := make([]string, len(c.handlers))
//for i, h := range c.handlers {
// handlerNames[i] = runtime.FuncForPC(reflect.ValueOf(h).Pointer()).column()
//}
var err error
for s := int8(len(c.handlers)); c.index < s; c.index++ {
err = c.handlers[c.index](c)
if err != nil {
c.Abort()
}
}
return err
}
func (c *Context) IsAborted() bool {
return len(c.handlers) >= abortIndex
}
func (c *Context) Abort() {
c.index = int8(abortIndex)
}
type CollectionHandlerFunc func(c *CollectionContext) error
type CollectionHandlersChain []CollectionHandlerFunc
type CollectionContext struct {
handlers CollectionHandlersChain
index int8
Brick *CollectionBrick
Result *Result
value map[interface{}]interface{}
//err error
}
func NewCollectionContext(handlers CollectionHandlersChain, brick *CollectionBrick, columns ModelRecords) *CollectionContext {
return &CollectionContext{
handlers: handlers,
index: -1,
Brick: brick,
Result: &Result{
Records: columns,
Preload: map[string]*Result{},
RecordsActions: map[int][]int{},
MiddleModelPreload: map[string]*Result{},
SimpleRelation: map[string]map[int]int{},
MultipleRelation: map[string]map[int]Pair{},
},
}
}
func (c *CollectionContext) Value(v interface{}) interface{} {
return c.value[v]
}
func (c *CollectionContext) Next() error {
c.index++
//handlerNames := make([]string, len(c.handlers))
//for i, h := range c.handlers {
// handlerNames[i] = runtime.FuncForPC(reflect.ValueOf(h).Pointer()).column()
//}
var err error
for s := int8(len(c.handlers)); c.index < s; c.index++ {
err = c.handlers[c.index](c)
if err != nil {
c.Abort()
}
}
return err
}
func (c *CollectionContext) IsAborted() bool {
return len(c.handlers) >= abortIndex
}
func (c *CollectionContext) Abort() {
c.index = int8(abortIndex)
}