github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/gauge/arg.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package gauge 19 20 import "fmt" 21 22 type ArgType string 23 24 const ( 25 Static ArgType = "static" 26 Dynamic ArgType = "dynamic" 27 TableArg ArgType = "table" 28 SpecialString ArgType = "special_string" 29 SpecialTable ArgType = "special_table" 30 ParameterPlaceholder = "{}" 31 ) 32 33 type ArgLookup struct { 34 //helps to access the index of an arg at O(1) 35 ParamIndexMap map[string]int 36 paramValue []paramNameValue 37 } 38 39 func (lookup ArgLookup) String() string { 40 return fmt.Sprintln(lookup.paramValue) 41 } 42 43 func (lookup *ArgLookup) AddArgName(argName string) { 44 if lookup.ParamIndexMap == nil { 45 lookup.ParamIndexMap = make(map[string]int) 46 lookup.paramValue = make([]paramNameValue, 0) 47 } 48 lookup.ParamIndexMap[argName] = len(lookup.paramValue) 49 lookup.paramValue = append(lookup.paramValue, paramNameValue{name: argName}) 50 } 51 52 func (lookup *ArgLookup) AddArgValue(param string, stepArg *StepArg) { 53 paramIndex, ok := lookup.ParamIndexMap[param] 54 if !ok { 55 panic(fmt.Sprintf("Accessing an invalid parameter (%s)", param)) 56 } 57 lookup.paramValue[paramIndex].stepArg = stepArg 58 } 59 60 func (lookup *ArgLookup) ContainsArg(param string) bool { 61 _, ok := lookup.ParamIndexMap[param] 62 return ok 63 } 64 65 func (lookup *ArgLookup) GetArg(param string) *StepArg { 66 paramIndex, ok := lookup.ParamIndexMap[param] 67 if !ok { 68 panic(fmt.Sprintf("Accessing an invalid parameter (%s)", param)) 69 } 70 return lookup.paramValue[paramIndex].stepArg 71 } 72 73 func (lookup *ArgLookup) GetCopy() *ArgLookup { 74 lookupCopy := new(ArgLookup) 75 for key, _ := range lookup.ParamIndexMap { 76 lookupCopy.AddArgName(key) 77 arg := lookup.GetArg(key) 78 if arg != nil { 79 lookupCopy.AddArgValue(key, &StepArg{Value: arg.Value, ArgType: arg.ArgType, Table: arg.Table, Name: arg.Name}) 80 } 81 } 82 return lookupCopy 83 } 84 85 func (lookup *ArgLookup) FromDataTableRow(datatable *Table, index int) *ArgLookup { 86 dataTableLookup := new(ArgLookup) 87 if !datatable.IsInitialized() { 88 return dataTableLookup 89 } 90 for _, header := range datatable.Headers { 91 dataTableLookup.AddArgName(header) 92 dataTableLookup.AddArgValue(header, &StepArg{Value: datatable.Get(header)[index].Value, ArgType: Static}) 93 } 94 return dataTableLookup 95 } 96 97 //create an empty lookup with only args to resolve dynamic params for steps 98 func (lookup *ArgLookup) FromDataTable(datatable *Table) *ArgLookup { 99 dataTableLookup := new(ArgLookup) 100 if !datatable.IsInitialized() { 101 return dataTableLookup 102 } 103 for _, header := range datatable.Headers { 104 dataTableLookup.AddArgName(header) 105 } 106 return dataTableLookup 107 } 108 109 type paramNameValue struct { 110 name string 111 stepArg *StepArg 112 } 113 114 func (paramNameValue paramNameValue) String() string { 115 return fmt.Sprintf("ParamName: %s, stepArg: %s", paramNameValue.name, paramNameValue.stepArg) 116 } 117 118 type StepArg struct { 119 Name string 120 Value string 121 ArgType ArgType 122 Table Table 123 } 124 125 func (stepArg *StepArg) String() string { 126 return fmt.Sprintf("{Name: %s,value %s,argType %s,table %v}", stepArg.Name, stepArg.Value, string(stepArg.ArgType), stepArg.Table) 127 }