Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save denisdubovitskiy/5bfe8dec281737c137fc481195cc0474 to your computer and use it in GitHub Desktop.

Select an option

Save denisdubovitskiy/5bfe8dec281737c137fc481195cc0474 to your computer and use it in GitHub Desktop.
template
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
clientv3 "go.etcd.io/etcd/client/v3"
)
{{ $decorator := .Vars.DecoratorName }}
type {{ $decorator }} struct {
base {{.Interface.Type}}
}
// {{ .Vars.ConstructorName }}...
func {{ .Vars.ConstructorName }} (base {{.Interface.Type}}) {{ $decorator }} {
return {{ $decorator }} {base: base}
}
{{ range $method := .Interface.Methods }}
// {{ $method.Name }}...
func (_d {{ $decorator }}) {{ $method.Name }}(
{{- /* именуем принимаемые параметры */}}
{{ range $param := $method.Params -}}
{{- $param.Name }} {{ $param.Type -}},
{{ end -}}
) (
{{- /* именуем возвращаемые параметры */}}
{{ range $param := $method.Results -}}
{{- $param.Name }} {{ $param.Type -}},
{{ end -}}
) {
{{- if $method.AcceptsContext}}
span, ctx := opentracing.StartSpanFromContext(ctx, "etcd/{{ $method.Name }}")
defer span.Finish()
ext.SpanKindRPCClient.Set(span)
ext.Component.Set(span, "etcd-client")
{{ range $param := $method.Params -}}
{{/* https://github.com/golang/go/issues/20531 */}}
{{- if eq $param.Name "ctx" }}{{ continue }}{{ end -}}
{{- if and (eq $param.Name "m") (eq $param.Type "*clientv3.AlarmMember") }}{{ continue }}{{ end -}}
{{/* пропускаем авторизационные методы */}}
{{- if eq $method.Name "Authenticate" }}{{ continue }}{{ end -}}
{{- if eq $method.Name "UserAdd" }}{{ continue }}{{ end -}}
{{- if eq $method.Name "UserAddWithOptions" }}{{ continue }}{{ end -}}
{{- if eq $method.Name "UserChangePassword" }}{{ continue }}{{ end -}}
span.LogKV("{{ $param.Name }}", {{ $param.Name }})
{{ end }}
{{- if $method.ReturnsError }}
defer func() {
ext.Error.Set(span, err != nil)
if err != nil {
span.LogKV("error", err.Error())
}
}()
{{ end -}}
{{ end }}
{{/* присваивание к именованным параметрам - хак шаблонов */}}
{{/* https://stackoverflow.com/questions/50085038/detect-last-item-inside-an-array-using-range-inside-go-templates */}}
{{ range $i, $result := $method.Results }}{{ if $i }},{{ end }}{{ $result.Name }}{{ end -}}
= _d.base.{{ $method.Name }}(
{{- range $param := $method.Params }}
{{ $param.Name }}{{- if $param.Variadic -}}...{{- end -}},
{{- end }}
)
return
}
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment