github.com/Capventis/moq@v0.2.6-0.20220316100624-05dd47497214/internal/template/template_data.go (about)

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