github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/subtc.go (about) 1 package funcs 2 3 import ( 4 "context" 5 "database/sql" 6 "fmt" 7 "time" 8 9 // "github.com/mdaxf/iac/engine/callback" 10 "github.com/mdaxf/iac/framework/callback_mgr" 11 ) 12 13 type TranFlow interface { 14 Execute(string, map[string]interface{}, context.Context, context.CancelFunc, *sql.Tx) (map[string]interface{}, error) 15 } 16 17 type SubTranCodeFuncs struct { 18 TranFlowstr TranFlow 19 } 20 21 // NewSubTran creates a new instance of SubTranCodeFuncs with the provided TranFlow. 22 // It returns a pointer to the newly created SubTranCodeFuncs. 23 func NewSubTran(tci TranFlow) *SubTranCodeFuncs { 24 return &SubTranCodeFuncs{ 25 TranFlowstr: tci, 26 } 27 28 } 29 30 type SubTranCode struct { 31 } 32 33 // Execute executes the subtran function. 34 // It sets the inputs, retrieves the transaction code, and calls the callback function to execute the transaction code. 35 // The outputs are then converted and set as the function outputs. 36 // If there is an error during execution, it logs the error and sets the error message. 37 // It also logs the performance of the function. 38 func (cf *SubTranCodeFuncs) Execute(f *Funcs) { 39 startTime := time.Now() 40 defer func() { 41 elapsed := time.Since(startTime) 42 f.iLog.PerformanceWithDuration("engine.funcs.SubTransCode.Execute", elapsed) 43 }() 44 defer func() { 45 if err := recover(); err != nil { 46 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.SubTransCode.Execute with error: %s", err)) 47 f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.SubTransCode.Execute with error: %s", err)) 48 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.SubTransCode.Execute with error: %s", err) 49 return 50 } 51 }() 52 53 tcode := "" 54 f.iLog.Debug(fmt.Sprintf("Executing subtran function")) 55 namelist, valuelist, mappedinputs := f.SetInputs() 56 57 for i, name := range namelist { 58 if name == "TranCode" { 59 tcode = valuelist[i] 60 } 61 } 62 if tcode == "" { 63 f.iLog.Error(fmt.Sprintf("Error executing transaction code: %s", "No trancode input")) 64 return 65 } 66 67 f.iLog.Debug(fmt.Sprintf("Executing subtran function to call transaction code: %v with inputs %s", tcode, mappedinputs)) 68 69 outputs, err := callback_mgr.CallBackFunc("TranCode_Execute", tcode, mappedinputs, f.SignalRClient, f.DocDBCon, f.Ctx, f.CtxCancel, f.DBTx) 70 //outputs := callback.ExecuteTranCode("TranFlowstr_Execute", tcode, mappedinputs, nil, nil, f.DBTx, f.DocDBCon, f.SignalRClient) 71 //outputs, err := cf.TranFlowstr.Execute(tcode, mappedinputs, f.Ctx, f.CtxCancel, f.DBTx) 72 if err != nil { 73 f.iLog.Error(fmt.Sprintf("Error executing transaction code: %v", err)) 74 return 75 } 76 f.SetOutputs(convertSliceToMap(outputs)) 77 } 78 79 // Validate is a method of the SubTranCodeFuncs struct that validates the function. 80 // It measures the performance of the function and logs the duration. 81 // It returns a boolean value indicating the success of the validation and an error if any. 82 // It also logs the performance of the function. 83 func (cf *SubTranCodeFuncs) Validate(f *Funcs) (bool, error) { 84 startTime := time.Now() 85 defer func() { 86 elapsed := time.Since(startTime) 87 f.iLog.PerformanceWithDuration("engine.funcs.SubTransCode.Validate", elapsed) 88 }() 89 /* defer func() { 90 if err := recover(); err != nil { 91 f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.SubTransCode.Validate with error: %s", err)) 92 f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.SubTransCode.Validate with error: %s", err) 93 return 94 } 95 }() */ 96 97 return true, nil 98 } 99 100 // convertSliceToMap converts a slice of interfaces into a map[string]interface{}. 101 // It iterates over the slice, treating every even-indexed element as the key and the following odd-indexed element as the value. 102 // If the key is a string, it adds the key-value pair to the resulting map. 103 // The function returns the resulting map. 104 func convertSliceToMap(slice []interface{}) map[string]interface{} { 105 resultMap := make(map[string]interface{}) 106 107 for i := 0; i < len(slice); i += 2 { 108 key, ok1 := slice[i].(string) 109 value := slice[i+1] 110 111 if ok1 { 112 resultMap[key] = value 113 } 114 } 115 116 return resultMap 117 }