1package generator 2 3import "strings" 4 5// Params is a slice of Param. 6type Params []Param 7 8// Param is an argument to a function. 9type Param struct { 10 Name string 11 Type string 12 IsVariadic bool 13 IsSlice bool 14} 15 16// Slices returns those params that are a slice. 17func (p Params) Slices() Params { 18 var result Params 19 for i := range p { 20 if p[i].IsSlice { 21 result = append(result, p[i]) 22 } 23 } 24 return result 25} 26 27// HasLength returns true if there are params. It returns false if there are no 28// params. 29func (p Params) HasLength() bool { 30 return len(p) > 0 31} 32 33// WithPrefix builds a string representing a functions parameters, and adds a 34// prefix to each. 35func (p Params) WithPrefix(prefix string) string { 36 if len(p) == 0 { 37 return "" 38 } 39 40 params := []string{} 41 for i := range p { 42 if prefix == "" { 43 params = append(params, unexport(p[i].Name)) 44 } else { 45 params = append(params, prefix+unexport(p[i].Name)) 46 } 47 } 48 return strings.Join(params, ", ") 49} 50 51// AsArgs builds a string that represents the parameters to a function as 52// arguments to a function invocation. 53func (p Params) AsArgs() string { 54 if len(p) == 0 { 55 return "" 56 } 57 58 params := []string{} 59 for i := range p { 60 params = append(params, p[i].Type) 61 } 62 return strings.Join(params, ", ") 63} 64 65// AsNamedArgsWithTypes builds a string that represents parameters as named 66// arugments to a function, with associated types. 67func (p Params) AsNamedArgsWithTypes() string { 68 if len(p) == 0 { 69 return "" 70 } 71 72 params := []string{} 73 for i := range p { 74 params = append(params, unexport(p[i].Name)+" "+p[i].Type) 75 } 76 return strings.Join(params, ", ") 77} 78 79// AsNamedArgs builds a string that represents parameters as named arguments. 80func (p Params) AsNamedArgs() string { 81 if len(p) == 0 { 82 return "" 83 } 84 85 params := []string{} 86 for i := range p { 87 if p[i].IsSlice { 88 params = append(params, unexport(p[i].Name)+"Copy") 89 } else { 90 params = append(params, unexport(p[i].Name)) 91 } 92 } 93 return strings.Join(params, ", ") 94} 95 96// AsNamedArgsForInvocation builds a string that represents a function's 97// arguments as required for invocation of the function. 98func (p Params) AsNamedArgsForInvocation() string { 99 if len(p) == 0 { 100 return "" 101 } 102 103 params := []string{} 104 for i := range p { 105 if p[i].IsVariadic { 106 params = append(params, unexport(p[i].Name)+"...") 107 } else { 108 params = append(params, unexport(p[i].Name)) 109 } 110 } 111 return strings.Join(params, ", ") 112} 113 114// AsReturnSignature builds a string representing signature for the params of 115// a function. 116func (p Params) AsReturnSignature() string { 117 if len(p) == 0 { 118 return "" 119 } 120 if len(p) == 1 { 121 if p[0].IsVariadic { 122 return strings.Replace(p[0].Type, "...", "[]", -1) 123 } 124 return p[0].Type 125 } 126 result := "(" 127 for i := range p { 128 t := p[i].Type 129 if p[i].IsVariadic { 130 t = strings.Replace(t, "...", "[]", -1) 131 } 132 result = result + t 133 if i < len(p) { 134 result = result + ", " 135 } 136 } 137 result = result + ")" 138 return result 139} 140