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

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/mdaxf/iac/engine/types"
     8  )
     9  
    10  type InputMapFuncs struct{}
    11  
    12  // Execute executes the input mapping functions.
    13  // It retrieves the input values from the Funcs object, maps them to the corresponding keys in the map data,
    14  // and sets the output values in the Funcs object.
    15  // If there is an error during execution, it logs the error, cancels the execution, and sets the error message.
    16  // It also logs the performance of the function.
    17  
    18  func (cf *InputMapFuncs) Execute(f *Funcs) {
    19  	startTime := time.Now()
    20  	defer func() {
    21  		elapsed := time.Since(startTime)
    22  		f.iLog.PerformanceWithDuration("engine.funcs.InputMapFuncs.Execute", elapsed)
    23  	}()
    24  	defer func() {
    25  		if err := recover(); err != nil {
    26  			errormsg := fmt.Sprintf("There is error to engine.funcs.InputMapFuncs.Execute with error: %s", err)
    27  			f.iLog.Error(errormsg)
    28  			f.CancelExecution(errormsg)
    29  			f.ErrorMessage = errormsg
    30  			return
    31  		}
    32  	}()
    33  	namelist, valuelist, _ := f.SetInputs()
    34  
    35  	data := f.Fobj.Mapdata
    36  
    37  	//var data map[string]interface{}
    38  
    39  	// Convert the string to JSON format
    40  	/*err := json.Unmarshal([]byte(mapstr), &data)
    41  	if err != nil {
    42  		f.iLog.Error(fmt.Sprintf("Error: %v", err))
    43  	} */
    44  	outputs := make(map[string]interface{})
    45  
    46  	for key := range data {
    47  		mapedinput := data[key]
    48  		for i := range namelist {
    49  			if mapedinput == namelist[i] {
    50  				outputs[key] = valuelist[i]
    51  			}
    52  		}
    53  	}
    54  
    55  	f.SetOutputs(outputs)
    56  }
    57  
    58  // Validate validates the input map functions.
    59  // It checks if the inputs and outputs in the map are valid and compatible.
    60  // If any error is encountered, it logs the error and returns false.
    61  // Otherwise, it returns true.
    62  // It also logs the performance of the function.
    63  
    64  func (cf *InputMapFuncs) Validate(f *Funcs) bool {
    65  	startTime := time.Now()
    66  	defer func() {
    67  		elapsed := time.Since(startTime)
    68  		f.iLog.PerformanceWithDuration("engine.funcs.InputMapFuncs.Validate", elapsed)
    69  	}()
    70  	/*	defer func() {
    71  		if err := recover(); err != nil {
    72  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.InputMapFuncs.Validate with error: %s", err))
    73  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.InputMapFuncs.Validate with error: %s", err)
    74  			return
    75  		}
    76  	}() */
    77  
    78  	data := f.Fobj.Mapdata
    79  
    80  	//var data map[string]interface{}
    81  
    82  	// Convert the string to JSON format
    83  	/*err := json.Unmarshal([]byte(mapstr), &data)
    84  	if err != nil {
    85  		f.iLog.Error(fmt.Sprintf("Error: %v", err))
    86  		return false
    87  	} */
    88  	//	outputs := make(map[string]interface{})
    89  
    90  	for key := range data {
    91  		mapedinput := data[key]
    92  		mapedoutput := key
    93  
    94  		var input types.Input
    95  		var output types.Output
    96  		foundinput := false
    97  		foundoutput := false
    98  		for i := range f.Fobj.Inputs {
    99  			if mapedinput == f.Fobj.Inputs[i].Name {
   100  				input = f.Fobj.Inputs[i]
   101  				foundinput = true
   102  				break
   103  			}
   104  		}
   105  
   106  		for i := range f.Fobj.Outputs {
   107  			if mapedoutput == f.Fobj.Outputs[i].Name {
   108  				output = f.Fobj.Outputs[i]
   109  				foundoutput = true
   110  				break
   111  			}
   112  		}
   113  
   114  		if foundinput && foundoutput {
   115  			if output.Datatype == types.String {
   116  				continue
   117  			}
   118  			if output.Datatype == types.Integer && input.Datatype != types.Integer {
   119  				f.iLog.Error(fmt.Sprintf("Output %s is Integer and input %s is not Integer", output.Name, input.Name))
   120  				return false
   121  			}
   122  			if output.Datatype == types.Float && (input.Datatype != types.Float && input.Datatype != types.Integer) {
   123  				f.iLog.Error(fmt.Sprintf("Output %s is float and input %s is not Integer or float", output.Name, input.Name))
   124  				return false
   125  			}
   126  
   127  			if output.Datatype == types.Bool && input.Datatype != types.Bool {
   128  				f.iLog.Error(fmt.Sprintf("Output %s is boolean and input %s is not boolean", output.Name, input.Name))
   129  				return false
   130  			}
   131  
   132  			if output.Datatype == types.DateTime && input.Datatype != types.DateTime {
   133  				f.iLog.Error(fmt.Sprintf("Output %s is date and input %s is not date", output.Name, input.Name))
   134  				return false
   135  			}
   136  
   137  			if output.List != input.List {
   138  				f.iLog.Error(fmt.Sprintf("Output %s and input %s are not same list flag", output.Name, input.Name))
   139  				return false
   140  			}
   141  
   142  		} else {
   143  			f.iLog.Error(fmt.Sprintf("Output %s or input %s are not found", mapedoutput, mapedinput))
   144  			return false
   145  		}
   146  
   147  	}
   148  
   149  	return true
   150  }