github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/callback_mgr/callback_mgr.go (about)

     1  package callback_mgr
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"time"
     7  
     8  	"github.com/mdaxf/iac/logger"
     9  )
    10  
    11  var CallBackMap map[string]interface{}
    12  
    13  func init() {
    14  	CallBackMap = make(map[string]interface{})
    15  }
    16  
    17  func RegisterCallBack(key string, callBack interface{}) {
    18  	log := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "CallbackRegister"}
    19  	log.Debug(fmt.Sprintf("RegisterCallBack: %s with %v", key, callBack))
    20  	CallBackMap[key] = callBack
    21  
    22  	log.Debug(fmt.Sprintf("callBackMap: %s", CallBackMap))
    23  }
    24  
    25  func CallBackFunc(key string, args ...interface{}) ([]interface{}, error) {
    26  	log := logger.Log{ModuleName: logger.Framework, User: "System", ControllerName: "CallbackExecution"}
    27  	startTime := time.Now()
    28  	defer func() {
    29  		elapsed := time.Since(startTime)
    30  		log.PerformanceWithDuration("CallbackExecution", elapsed)
    31  	}()
    32  	defer func() {
    33  		if r := recover(); r != nil {
    34  			log.Error(fmt.Sprintf("CallbackExecution error: %v", r))
    35  			return
    36  
    37  		}
    38  	}()
    39  
    40  	log.Debug(fmt.Sprintf("CallBackFunc: %s with %v", key, args))
    41  	log.Debug(fmt.Sprintf("callBackMap: %s", CallBackMap))
    42  	if callBack, ok := CallBackMap[key]; ok {
    43  
    44  		log.Debug(fmt.Sprintf("callBack: %s", callBack))
    45  
    46  		in := make([]reflect.Value, len(args))
    47  		for i, arg := range args {
    48  			if arg == nil {
    49  				arg = reflect.ValueOf("")
    50  			}
    51  
    52  			in[i] = reflect.ValueOf(arg)
    53  		}
    54  		log.Debug(fmt.Sprintf("in: %s", in))
    55  
    56  		funcValue := reflect.ValueOf(callBack)
    57  		log.Debug(fmt.Sprintf("funcValue: %s", funcValue))
    58  
    59  		if funcValue.Kind() != reflect.Func {
    60  
    61  			log.Error(fmt.Sprintf("callBack(%s) is not a function", key))
    62  			return nil, fmt.Errorf(fmt.Sprintf("callBack(%s) is not a function", key))
    63  		}
    64  
    65  		outList := funcValue.Call(in)
    66  		result := make([]interface{}, len(outList))
    67  
    68  		log.Debug(fmt.Sprintf("outList: %s", logger.ConvertJson(outList)))
    69  
    70  		for i, out := range outList {
    71  			result[i] = out.Interface()
    72  		}
    73  		log.Debug(fmt.Sprintf("result: %s", logger.ConvertJson(result)))
    74  		return result, nil
    75  	} else {
    76  		log.Error(fmt.Sprintf("callBack(%s) not found", key))
    77  		return nil, fmt.Errorf(fmt.Sprintf("callBack(%s) not found", key))
    78  	}
    79  }
    80  
    81  // convertSliceToMap converts a slice of interfaces into a map[string]interface{}.
    82  // It iterates over the slice, treating every even-indexed element as the key and the following odd-indexed element as the value.
    83  // If the key is a string, it adds the key-value pair to the resulting map.
    84  // The function returns the resulting map.
    85  func ConvertSliceToMap(slice []interface{}) map[string]interface{} {
    86  	resultMap := make(map[string]interface{})
    87  
    88  	for i := 0; i < len(slice); i += 2 {
    89  		key, ok1 := slice[i].(string)
    90  		value := slice[i+1]
    91  
    92  		if ok1 {
    93  			resultMap[key] = value
    94  		}
    95  	}
    96  
    97  	return resultMap
    98  }