gopkg.in/docker/docker.v23@v23.0.11/pkg/plugins/pluginrpc-gen/template.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"text/template"
     6  )
     7  
     8  func printArgs(args []arg) string {
     9  	var argStr []string
    10  	for _, arg := range args {
    11  		argStr = append(argStr, arg.String())
    12  	}
    13  	return strings.Join(argStr, ", ")
    14  }
    15  
    16  func buildImports(specs []importSpec) string {
    17  	if len(specs) == 0 {
    18  		return `import "errors"`
    19  	}
    20  	imports := "import(\n"
    21  	imports += "\t\"errors\"\n"
    22  	for _, i := range specs {
    23  		imports += "\t" + i.String() + "\n"
    24  	}
    25  	imports += ")"
    26  	return imports
    27  }
    28  
    29  func marshalType(t string) string {
    30  	switch t {
    31  	case "error":
    32  		// convert error types to plain strings to ensure the values are encoded/decoded properly
    33  		return "string"
    34  	default:
    35  		return t
    36  	}
    37  }
    38  
    39  func isErr(t string) bool {
    40  	switch t {
    41  	case "error":
    42  		return true
    43  	default:
    44  		return false
    45  	}
    46  }
    47  
    48  // Need to use this helper due to issues with go-vet
    49  func buildTag(s string) string {
    50  	return "+build " + s
    51  }
    52  
    53  var templFuncs = template.FuncMap{
    54  	"printArgs":   printArgs,
    55  	"marshalType": marshalType,
    56  	"isErr":       isErr,
    57  	"lower":       strings.ToLower,
    58  	"title":       title,
    59  	"tag":         buildTag,
    60  	"imports":     buildImports,
    61  }
    62  
    63  func title(s string) string {
    64  	if strings.ToLower(s) == "id" {
    65  		return "ID"
    66  	}
    67  	return strings.Title(s) //nolint:staticcheck // SA1019: strings.Title is deprecated: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.
    68  }
    69  
    70  var generatedTempl = template.Must(template.New("rpc_cient").Funcs(templFuncs).Parse(`
    71  // generated code - DO NOT EDIT
    72  {{ range $k, $v := .BuildTags }}
    73  	// {{ tag $k }} {{ end }}
    74  
    75  package {{ .Name }}
    76  
    77  {{ imports .Imports }}
    78  
    79  type client interface{
    80  	Call(string, interface{}, interface{}) error
    81  }
    82  
    83  type {{ .InterfaceType }}Proxy struct {
    84  	client
    85  }
    86  
    87  {{ range .Functions }}
    88  	type {{ $.InterfaceType }}Proxy{{ .Name }}Request struct{
    89  		{{ range .Args }}
    90  			{{ title .Name }} {{ .ArgType }} {{ end }}
    91  	}
    92  
    93  	type {{ $.InterfaceType }}Proxy{{ .Name }}Response struct{
    94  		{{ range .Returns }}
    95  			{{ title .Name }} {{ marshalType .ArgType }} {{ end }}
    96  	}
    97  
    98  	func (pp *{{ $.InterfaceType }}Proxy) {{ .Name }}({{ printArgs .Args }}) ({{ printArgs .Returns }}) {
    99  		var(
   100  			req {{ $.InterfaceType }}Proxy{{ .Name }}Request
   101  			ret {{ $.InterfaceType }}Proxy{{ .Name }}Response
   102  		)
   103  		{{ range .Args }}
   104  			req.{{ title .Name }} = {{ lower .Name }} {{ end }}
   105  		if err = pp.Call("{{ $.RPCName }}.{{ .Name }}", req, &ret); err != nil {
   106  			return
   107  		}
   108  		{{ range $r := .Returns }}
   109  			{{ if isErr .ArgType }}
   110  				if ret.{{ title .Name }} != "" {
   111  					{{ lower .Name }} = errors.New(ret.{{ title .Name }})
   112  				} {{ end }}
   113  			{{ if isErr .ArgType | not }} {{ lower .Name }} = ret.{{ title .Name }} {{ end }} {{ end }}
   114  
   115  		return
   116  	}
   117  {{ end }}
   118  `))