github.com/djui/moq@v0.3.3/internal/template/template_data.go (about) 1 package template 2 3 import ( 4 "fmt" 5 "go/types" 6 "strings" 7 8 "github.com/djui/moq/internal/registry" 9 ) 10 11 // Data is the template data used to render the Moq template. 12 type Data struct { 13 PkgName string 14 SrcPkgQualifier string 15 Imports []*registry.Package 16 Mocks []MockData 17 StubImpl bool 18 SkipEnsure bool 19 } 20 21 // MocksSomeMethod returns true of any one of the Mocks has at least 1 22 // method. 23 func (d Data) MocksSomeMethod() bool { 24 for _, m := range d.Mocks { 25 if len(m.Methods) > 0 { 26 return true 27 } 28 } 29 30 return false 31 } 32 33 // MockData is the data used to generate a mock for some interface. 34 type MockData struct { 35 InterfaceName string 36 MockName string 37 TypeParams []TypeParamData 38 Methods []MethodData 39 } 40 41 // MethodData is the data which represents a method on some interface. 42 type MethodData struct { 43 Name string 44 Params []ParamData 45 Returns []ParamData 46 } 47 48 // ArgList is the string representation of method parameters, ex: 49 // 's string, n int, foo bar.Baz'. 50 func (m MethodData) ArgList() string { 51 params := make([]string, len(m.Params)) 52 for i, p := range m.Params { 53 params[i] = p.MethodArg() 54 } 55 return strings.Join(params, ", ") 56 } 57 58 // ArgCallList is the string representation of method call parameters, 59 // ex: 's, n, foo'. In case of a last variadic parameter, it will be of 60 // the format 's, n, foos...' 61 func (m MethodData) ArgCallList() string { 62 params := make([]string, len(m.Params)) 63 for i, p := range m.Params { 64 params[i] = p.CallName() 65 } 66 return strings.Join(params, ", ") 67 } 68 69 // ReturnArgTypeList is the string representation of method return 70 // types, ex: 'bar.Baz', '(string, error)'. 71 func (m MethodData) ReturnArgTypeList() string { 72 params := make([]string, len(m.Returns)) 73 for i, p := range m.Returns { 74 params[i] = p.TypeString() 75 } 76 if len(m.Returns) > 1 { 77 return fmt.Sprintf("(%s)", strings.Join(params, ", ")) 78 } 79 return strings.Join(params, ", ") 80 } 81 82 // ReturnArgNameList is the string representation of values being 83 // returned from the method, ex: 'foo', 's, err'. 84 func (m MethodData) ReturnArgNameList() string { 85 params := make([]string, len(m.Returns)) 86 for i, p := range m.Returns { 87 params[i] = p.Name() 88 } 89 return strings.Join(params, ", ") 90 } 91 92 type TypeParamData struct { 93 ParamData 94 Constraint types.Type 95 } 96 97 // ParamData is the data which represents a parameter to some method of 98 // an interface. 99 type ParamData struct { 100 Var *registry.Var 101 Variadic bool 102 } 103 104 // Name returns the name of the parameter. 105 func (p ParamData) Name() string { 106 return p.Var.Name 107 } 108 109 // MethodArg is the representation of the parameter in the function 110 // signature, ex: 'name a.Type'. 111 func (p ParamData) MethodArg() string { 112 if p.Variadic { 113 return fmt.Sprintf("%s ...%s", p.Name(), p.TypeString()[2:]) 114 } 115 return fmt.Sprintf("%s %s", p.Name(), p.TypeString()) 116 } 117 118 // CallName returns the string representation of the parameter to be 119 // used for a method call. For a variadic paramter, it will be of the 120 // format 'foos...'. 121 func (p ParamData) CallName() string { 122 if p.Variadic { 123 return p.Name() + "..." 124 } 125 return p.Name() 126 } 127 128 // TypeString returns the string representation of the type of the 129 // parameter. 130 func (p ParamData) TypeString() string { 131 return p.Var.TypeString() 132 }