github.com/getgauge/gauge@v1.6.9/gauge/arg.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package gauge
     8  
     9  import (
    10  	"fmt"
    11  )
    12  
    13  type ArgType string
    14  
    15  const (
    16  	Static               ArgType = "static"
    17  	Dynamic              ArgType = "dynamic"
    18  	TableArg             ArgType = "table"
    19  	SpecialString        ArgType = "special_string"
    20  	SpecialTable         ArgType = "special_table"
    21  	ParameterPlaceholder         = "{}"
    22  )
    23  
    24  type ArgLookup struct {
    25  	//helps to access the index of an arg at O(1)
    26  	ParamIndexMap map[string]int
    27  	paramValue    []paramNameValue
    28  }
    29  
    30  func (lookup ArgLookup) String() string {
    31  	return fmt.Sprintln(lookup.paramValue)
    32  }
    33  
    34  func (lookup *ArgLookup) AddArgName(argName string) {
    35  	if lookup.ParamIndexMap == nil {
    36  		lookup.ParamIndexMap = make(map[string]int)
    37  		lookup.paramValue = make([]paramNameValue, 0)
    38  	}
    39  	lookup.ParamIndexMap[argName] = len(lookup.paramValue)
    40  	lookup.paramValue = append(lookup.paramValue, paramNameValue{name: argName})
    41  }
    42  
    43  func (lookup *ArgLookup) AddArgValue(param string, stepArg *StepArg) error {
    44  	paramIndex, ok := lookup.ParamIndexMap[param]
    45  	if !ok {
    46  		return fmt.Errorf("Accessing an invalid parameter (%s)", param)
    47  	}
    48  	stepArg.Name = param
    49  	lookup.paramValue[paramIndex].stepArg = stepArg
    50  	return nil
    51  }
    52  
    53  func (lookup *ArgLookup) ContainsArg(param string) bool {
    54  	_, ok := lookup.ParamIndexMap[param]
    55  	return ok
    56  }
    57  
    58  func (lookup *ArgLookup) GetArg(param string) (*StepArg, error) {
    59  	paramIndex, ok := lookup.ParamIndexMap[param]
    60  	if !ok {
    61  		return nil, fmt.Errorf("Accessing an invalid parameter (%s)", param)
    62  	}
    63  	return lookup.paramValue[paramIndex].stepArg, nil
    64  }
    65  
    66  func (lookup *ArgLookup) GetCopy() (*ArgLookup, error) {
    67  	lookupCopy := new(ArgLookup)
    68  	var err error
    69  	for key := range lookup.ParamIndexMap {
    70  		lookupCopy.AddArgName(key)
    71  		var arg *StepArg
    72  		arg, err = lookup.GetArg(key)
    73  		if arg != nil {
    74  			err = lookupCopy.AddArgValue(key, &StepArg{Value: arg.Value, ArgType: arg.ArgType, Table: arg.Table, Name: arg.Name})
    75  		}
    76  	}
    77  	return lookupCopy, err
    78  }
    79  
    80  func (lookup *ArgLookup) ReadDataTableRow(datatable *Table, index int) error {
    81  	if !datatable.IsInitialized() {
    82  		return nil
    83  	}
    84  	for _, header := range datatable.Headers {
    85  		lookup.AddArgName(header)
    86  		tableCells, _ := datatable.Get(header)
    87  		err := lookup.AddArgValue(header, &StepArg{Value: tableCells[index].Value, ArgType: Static})
    88  		if err != nil {
    89  			return err
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  //FromDataTables creates an empty lookup with only args to resolve dynamic params for steps from given list of tables
    96  func (lookup *ArgLookup) FromDataTables(tables ...*Table) *ArgLookup {
    97  	dataTableLookup := new(ArgLookup)
    98  	for _, table := range tables {
    99  		if table.IsInitialized() {
   100  			for _, header := range table.Headers {
   101  				dataTableLookup.AddArgName(header)
   102  			}
   103  		}
   104  	}
   105  	return dataTableLookup
   106  }
   107  
   108  type paramNameValue struct {
   109  	name    string
   110  	stepArg *StepArg
   111  }
   112  
   113  func (paramNameValue paramNameValue) String() string {
   114  	return fmt.Sprintf("ParamName: %s, stepArg: %s", paramNameValue.name, paramNameValue.stepArg)
   115  }
   116  
   117  type StepArg struct {
   118  	Name    string
   119  	Value   string
   120  	ArgType ArgType
   121  	Table   Table
   122  }
   123  
   124  func (stepArg *StepArg) String() string {
   125  	return fmt.Sprintf("{Name: %s,value %s,argType %s,table %v}", stepArg.Name, stepArg.Value, string(stepArg.ArgType), stepArg.Table)
   126  }
   127  
   128  func (stepArg *StepArg) ArgValue() string {
   129  	switch stepArg.ArgType {
   130  	case Static, Dynamic:
   131  		return stepArg.Value
   132  	case TableArg:
   133  		return "table"
   134  	case SpecialString, SpecialTable:
   135  		return stepArg.Name
   136  	}
   137  	return ""
   138  }
   139  
   140  type ExecutionArg struct {
   141  	Name  string
   142  	Value []string
   143  }