github.com/wulonghui/docker@v1.8.0-rc2/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 marshalType(t string) string {
    17  	switch t {
    18  	case "error":
    19  		// convert error types to plain strings to ensure the values are encoded/decoded properly
    20  		return "string"
    21  	default:
    22  		return t
    23  	}
    24  }
    25  
    26  func isErr(t string) bool {
    27  	switch t {
    28  	case "error":
    29  		return true
    30  	default:
    31  		return false
    32  	}
    33  }
    34  
    35  // Need to use this helper due to issues with go-vet
    36  func buildTag(s string) string {
    37  	return "+build " + s
    38  }
    39  
    40  var templFuncs = template.FuncMap{
    41  	"printArgs":   printArgs,
    42  	"marshalType": marshalType,
    43  	"isErr":       isErr,
    44  	"lower":       strings.ToLower,
    45  	"title":       strings.Title,
    46  	"tag":         buildTag,
    47  }
    48  
    49  var generatedTempl = template.Must(template.New("rpc_cient").Funcs(templFuncs).Parse(`
    50  // generated code - DO NOT EDIT
    51  {{ range $k, $v := .BuildTags }}
    52  	// {{ tag $k }} {{ end }}
    53  
    54  package {{ .Name }}
    55  
    56  import "errors"
    57  
    58  type client interface{
    59  	Call(string, interface{}, interface{}) error
    60  }
    61  
    62  type {{ .InterfaceType }}Proxy struct {
    63  	client
    64  }
    65  
    66  {{ range .Functions }}
    67  	type {{ $.InterfaceType }}Proxy{{ .Name }}Request struct{
    68  		{{ range .Args }}
    69  			{{ title .Name }} {{ .ArgType }} {{ end }}
    70  	}
    71  
    72  	type {{ $.InterfaceType }}Proxy{{ .Name }}Response struct{
    73  		{{ range .Returns }}
    74  			{{ title .Name }} {{ marshalType .ArgType }} {{ end }}
    75  	}
    76  
    77  	func (pp *{{ $.InterfaceType }}Proxy) {{ .Name }}({{ printArgs .Args }}) ({{ printArgs .Returns }}) {
    78  		var(
    79  			req {{ $.InterfaceType }}Proxy{{ .Name }}Request
    80  			ret {{ $.InterfaceType }}Proxy{{ .Name }}Response
    81  		)
    82  		{{ range .Args }}
    83  			req.{{ title .Name }} = {{ lower .Name }} {{ end }}
    84  		if err = pp.Call("{{ $.RPCName }}.{{ .Name }}", req, &ret); err != nil {
    85  			return
    86  		}
    87  		{{ range $r := .Returns }}
    88  			{{ if isErr .ArgType }}
    89  				if ret.{{ title .Name }} != "" {
    90  					{{ lower .Name }} = errors.New(ret.{{ title .Name }})
    91  				} {{ end }}
    92  			{{ if isErr .ArgType | not }} {{ lower .Name }} = ret.{{ title .Name }} {{ end }} {{ end }}
    93  
    94  		return
    95  	}
    96  {{ end }}
    97  `))