github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/databases/orm/filter.go (about) 1 // The original package is migrated from beego and modified, you can find orignal from following link: 2 // "github.com/beego/beego/" 3 // 4 // Copyright 2023 IAC. All Rights Reserved. 5 // 6 // Licensed under the Apache License, Version 2.0 (the "License"); 7 // you may not use this file except in compliance with the License. 8 // You may obtain a copy of the License at 9 // 10 // http://www.apache.org/licenses/LICENSE-2.0 11 // 12 // Unless required by applicable law or agreed to in writing, software 13 // distributed under the License is distributed on an "AS IS" BASIS, 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 // See the License for the specific language governing permissions and 16 // limitations under the License. 17 18 package orm 19 20 import ( 21 "context" 22 ) 23 24 // FilterChain is used to build a Filter 25 // don't forget to call next(...) inside your Filter 26 type FilterChain func(next Filter) Filter 27 28 // Filter's behavior is a little big strange. 29 // it's only be called when users call methods of Ormer 30 // return value is an array. it's a little bit hard to understand, 31 // for example, the Ormer's Read method only return error 32 // so the filter processing this method should return an array whose first element is error 33 // and, Ormer's ReadOrCreateWithCtx return three values, so the Filter's result should contains three values 34 type Filter func(ctx context.Context, inv *Invocation) []interface{} 35 36 var globalFilterChains = make([]FilterChain, 0, 4) 37 38 // AddGlobalFilterChain adds a new FilterChain 39 // All orm instances built after this invocation will use this filterChain, 40 // but instances built before this invocation will not be affected 41 func AddGlobalFilterChain(filterChain ...FilterChain) { 42 globalFilterChains = append(globalFilterChains, filterChain...) 43 }