@@ -44,6 +44,8 @@ type Action struct {
4444 NewAction func (ctx context.Context , method string , req model.Map ) * Action
4545
4646 HooksMap map [string ][]* Hook
47+
48+ ret model.Map
4749}
4850
4951func New (ctx context.Context , actionConfig * config.ActionConfig , method string , req model.Map ) * Action {
@@ -118,67 +120,68 @@ func (a *Action) parse() error {
118120 return nil
119121}
120122
121- func (a * Action ) Result () (model.Map , error ) {
122-
123- err := a .parse ()
124- if err != nil {
125- return nil , err
123+ func (a * Action ) hookExecute (i int , inTransaction bool ) error {
124+ k := a .tagRequest .ExecQueue [i ]
125+ node := a .children [k ]
126+ nodeHookReq := & HookReq {
127+ Node : node ,
128+ Method : a .method ,
129+ ctx : a .ctx ,
130+ nextIdx : - 1 ,
131+ isInTransaction : inTransaction ,
132+ hooks : getHooksByAccessName (a .HooksMap , k ),
126133 }
127134
128- ret := model.Map {}
129-
130- for _ , k := range a .tagRequest .ExecQueue {
131- node := a .children [k ]
135+ nodeHookReq .handler = func (ctx context.Context , n * Node , method string ) error {
132136
133- actionHookReq := & HookReq {
134- Node : node ,
135- Method : a .method ,
136- ctx : a .ctx ,
137- nextIdx : - 1 ,
138- isInTransaction : false ,
139- hooks : getHooksByAccessName (a .HooksMap , k ),
137+ if i + 1 < len (a .tagRequest .ExecQueue ) {
138+ return a .hookExecute (i + 1 , inTransaction )
140139 }
141140
142- actionHookReq .handler = func (ctx context.Context , n * Node , method string ) error {
141+ // 执行完了普通hook的before,开始执行事务内
142+ if ! inTransaction {
143+
143144 transactionHandler := noTransactionHandler
144145
145146 if a .tagRequest .Transaction != nil && * a .tagRequest .Transaction == true {
146147 h := GetTransactionHandler (a .ctx , a )
147148 if h == nil {
148- err = consts .NewSysErr ("transaction handler is nil" )
149+ err : = consts .NewSysErr ("transaction handler is nil" )
149150 return err
150151 }
151-
152152 transactionHandler = h
153-
154153 }
155154
156- err = transactionHandler (a .ctx , func (ctx context.Context ) error {
157- for _ , k := range a .tagRequest .ExecQueue {
158- node := a .children [k ]
159- ret [k ], err = node .execute (ctx , a .method )
160- if err != nil {
161- return err
162- }
163- }
164- return nil
155+ err := transactionHandler (a .ctx , func (ctx context.Context ) error {
156+ return a .hookExecute (0 , ! inTransaction )
165157 })
166158
167159 return err
168160 }
169161
170- err = node . reqUpdate ()
171- if err != nil {
172- return nil , err
173- }
162+ var err error
163+ a . ret [ k ], err = node . execute ( ctx , a . method )
164+ return err
165+ }
174166
175- err := actionHookReq .Next ()
176- if err != nil {
177- return nil , err
178- }
167+ err := nodeHookReq .Next ()
168+ return err
169+ }
170+
171+ func (a * Action ) Result () (model.Map , error ) {
172+
173+ err := a .parse ()
174+ if err != nil {
175+ return nil , err
179176 }
180177
181- return ret , nil
178+ a .ret = model.Map {}
179+
180+ err = a .hookExecute (0 , false )
181+ if err != nil {
182+ a .err = err
183+ }
184+ return a .ret , err
182185}
183186
184187func checkTag (req model.Map , method string , requestCfg * config.ActionConfig ) (* config.RequestConfig , error ) {
0 commit comments