github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/goexprfuncs.go (about)

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/antonmedv/expr"
     8  	//	"reflect"
     9  	"github.com/mdaxf/iac/logger"
    10  )
    11  
    12  type GoExprFuncs struct {
    13  }
    14  
    15  func (cf *GoExprFuncs) Execute(f *Funcs) {
    16  	startTime := time.Now()
    17  	defer func() {
    18  		elapsed := time.Since(startTime)
    19  		f.iLog.PerformanceWithDuration("engine.funcs.GoExprFuncs.Execute", elapsed)
    20  	}()
    21  	defer func() {
    22  		if err := recover(); err != nil {
    23  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.GoExprFuncs.Execute with error: %s", err))
    24  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.GoExprFuncs.Execute with error: %s", err))
    25  			return
    26  		}
    27  	}()
    28  
    29  	namelist, _, inputs := f.SetInputs()
    30  
    31  	env := make(map[string]interface{}, 0)
    32  	for i := range namelist {
    33  		env[namelist[i]] = inputs[namelist[i]]
    34  	}
    35  
    36  	program, err := expr.Compile(f.Fobj.Content, expr.Env(env))
    37  	if err != nil {
    38  		f.iLog.Error(fmt.Sprintf("failed to compile expression: %v", err))
    39  		return
    40  	}
    41  
    42  	output, err := expr.Run(program, env)
    43  	if err != nil {
    44  		f.iLog.Error(fmt.Sprintf("failed to run expression: %v", err))
    45  		return
    46  	}
    47  
    48  	f.SetOutputs(output.(map[string]interface{}))
    49  }
    50  
    51  func (cf *GoExprFuncs) Validate(f *Funcs) (bool, error) {
    52  	startTime := time.Now()
    53  	defer func() {
    54  		elapsed := time.Since(startTime)
    55  		f.iLog.PerformanceWithDuration("engine.funcs.GoExprFuncs.Validate", elapsed)
    56  	}()
    57  	/*	defer func() {
    58  		if err := recover(); err != nil {
    59  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.GoExprFuncs.Validate with error: %s", err))
    60  			return
    61  		}
    62  	}() */
    63  
    64  	return true, nil
    65  }
    66  
    67  func (cf *GoExprFuncs) Testfunction(content string, inputs interface{}, outputs []string) (map[string]interface{}, error) {
    68  
    69  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "CSharp Function"}
    70  	startTime := time.Now()
    71  	defer func() {
    72  		elapsed := time.Since(startTime)
    73  		iLog.PerformanceWithDuration("engine.funcs.GoExprFuncs.Testfunction", elapsed)
    74  	}()
    75  	/*	defer func() {
    76  			if err := recover(); err != nil {
    77  				iLog.Error(fmt.Sprintf("There is error to engine.funcs.GoExprFuncs.Testfunction with error: %s", err))
    78  				return
    79  			}
    80  		}()
    81  	*/
    82  	iLog.Debug(fmt.Sprintf("Test Exec Function"))
    83  
    84  	iLog.Debug(fmt.Sprintf("Test Exec Function content: %s", content))
    85  	env := inputs.(map[string]interface{})
    86  
    87  	program, err := expr.Compile(content, expr.Env(env))
    88  	if err != nil {
    89  		iLog.Error(fmt.Sprintf("failed to compile expression: %v", err))
    90  		return nil, err
    91  	}
    92  
    93  	output, err := expr.Run(program, env)
    94  	if err != nil {
    95  		iLog.Error(fmt.Sprintf("failed to run expression: %v", err))
    96  		return nil, err
    97  	}
    98  
    99  	return output.(map[string]interface{}), nil
   100  }