github.com/aacfactory/fns@v1.2.85/hooks/hook.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package hooks 19 20 import ( 21 "github.com/aacfactory/configures" 22 "github.com/aacfactory/errors" 23 "github.com/aacfactory/fns/context" 24 "github.com/aacfactory/json" 25 "github.com/aacfactory/logs" 26 ) 27 28 type Config map[string]json.RawMessage 29 30 func (config Config) Get(name string) (v configures.Config, err error) { 31 if len(config) == 0 { 32 v, _ = configures.NewJsonConfig([]byte{'{', '}'}) 33 return 34 } 35 p, has := config[name] 36 if !has { 37 v, _ = configures.NewJsonConfig([]byte{'{', '}'}) 38 return 39 } 40 v, err = configures.NewJsonConfig(p) 41 if err != nil { 42 err = errors.Warning("fns: get hook config failed").WithCause(err).WithMeta("hook", name) 43 return 44 } 45 return 46 } 47 48 type Options struct { 49 Log logs.Logger 50 Config configures.Config 51 } 52 53 type Hook interface { 54 Name() string 55 Construct(options Options) (err error) 56 Execute(ctx context.Context) 57 Shutdown(ctx context.Context) 58 }