Repository: moby/moby
File: pkg/plugins/pluginrpc-gen/template.go (Go)

Code before:
	}
	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.
}

var generatedTempl = template.Must(template.New("rpc_cient").Funcs(templFuncs).Parse(`
// Code generated by pluginrpc-gen. DO NOT EDIT.
{{ range $k, $v := .BuildTags }}
	// {{ tag $k }} {{ end }}

package {{ .Name }}

{{ imports .Imports }}

const (
	longTimeout  = 2 * time.Minute
	shortTimeout = 1 * time.Minute
)

type client interface{
	CallWithOptions(string, interface{}, interface{}, ...func(*plugins.RequestOpts)) error
}

type {{ .InterfaceType }}Proxy struct {
	client
}

{{ range .Functions }}
	type {{ $.InterfaceType }}Proxy{{ .Name }}Request struct{
		{{- range .Args }}
			{{ title .Name }} {{ .ArgType }} {{- end -}}
	}

	type {{ $.InterfaceType }}Proxy{{ .Name }}Response struct{
		{{- range .Returns }}
			{{ title .Name }} {{ marshalType .ArgType }} {{- end -}}
	}

	{{- if .Doc }}
	{{ .Doc }}
	{{- end }}
	func (pp *{{ $.InterfaceType }}Proxy) {{ .Name }}({{ printArgs .Args }}) ({{ printArgs .Returns }}) {
		var(
			req {{ $.InterfaceType }}Proxy{{ .Name }}Request
			ret {{ $.InterfaceType }}Proxy{{ .Name }}Response
		)
		{{ range .Args }}
			req.{{ title .Name }} = {{ lower .Name }} {{ end }}

		if err = pp.CallWithOptions("{{ $.RPCName }}.{{ .Name }}", req, &ret, plugins.WithRequestTimeout({{ .TimeoutType }}Timeout)); err != nil {
			return
		}

Review comment (refactor):
Don't really understand what the linter is complaining about here...

```
pkg/plugins/pluginrpc-gen/template.go:73:86: Duplicate words (interface{},) found (dupword)
var generatedTempl = template.Must(template.New("rpc_cient").Funcs(templFuncs).Parse(`
```

Code after:
// goduration formats a time.Duration as Go code (e.g., 2 * time.Minute)
func goduration(d time.Duration) string {
	seconds := int64(d.Seconds())
	switch {
	case d == 0:
		return "0 * time.Nanosecond"
	case seconds%(60*60) == 0 && seconds != 0:
		return fmt.Sprintf("%d * time.Hour", seconds/3600)
	case seconds%60 == 0 && seconds != 0:
		return fmt.Sprintf("%d * time.Minute", seconds/60)
	case seconds != 0:
		return fmt.Sprintf("%d * time.Second", seconds)
	default:
		// fallback to nanoseconds for sub-second durations
		return fmt.Sprintf("%d * time.Nanosecond", d.Nanoseconds())
	}
}

func title(s string) string {
	if strings.ToLower(s) == "id" {
		return "ID"
	}
	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.
}

var generatedTempl = template.Must(template.New("rpc_cient").Funcs(templFuncs).Parse(`
// Code generated by pluginrpc-gen. DO NOT EDIT.
{{ range $k, $v := .BuildTags }}
	// {{ tag $k }} {{ end }}

package {{ .Name }}

{{ imports .Imports }}

const (
	longTimeout  = {{ .LongTimeout | goduration }}
	shortTimeout = {{ .ShortTimeout | goduration }}
)

type client interface{
	CallWithOptions(serviceMethod string, args interface{}, ret interface{}, opts ...func(*plugins.RequestOpts)) error
}

type {{ .InterfaceType }}Proxy struct {
	client
}

{{ range .Functions }}
	type {{ $.InterfaceType }}Proxy{{ .Name }}Request struct{
		{{- range .Args }}
			{{ title .Name }} {{ .ArgType }} {{- end -}}