Skip to content

Commit

Permalink
template fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ihadeed committed Oct 11, 2020
1 parent e91e167 commit a8b2a4a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 267 deletions.
247 changes: 0 additions & 247 deletions cmd/nats_client.go

This file was deleted.

52 changes: 32 additions & 20 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,18 @@ const timeout = time.Second * {{ .Timeout }}
innerCtx, _ := context.WithTimeout(ctx, timeout)
innerCtxT := opentracing.ContextWithSpan(innerCtx, replySpan)
{{ $param := index $method.Params 1 }}
{{ $hasResult := gt (len $method.Results) 1 }}
{{ if $hasResult }}
var result {{ template "type_ref_full" (index $method.Results 0) }}
{{ end }}
{{ $hasParam := gt (len $method.Params) 1 }}
{{ if $hasParam }}
{{ $param := index $method.Params 1 }}
{{ if eq $param.Type "string" -}}
{{ if $hasResult }}result, {{ end }} err = h.Server.{{ $method.Name }}(innerCtxT, string(msg.Data))
{{ else }}
Expand All @@ -138,6 +142,10 @@ const timeout = time.Second * {{ .Timeout }}
}
{{ if $hasResult }}result, {{ end }} err = h.Server.{{ $method.Name }}(innerCtxT, {{ if and $param.Pointer (not $param.Array) }}&{{ end }}data)
{{ end }}
{{ else }}
{{ if $hasResult }}result, {{ end }} err = h.Server.{{ $method.Name }}(innerCtxT)
{{ end }}
reply := autonats.GetReply()
defer autonats.PutReply(reply)
Expand Down Expand Up @@ -228,28 +236,32 @@ const timeout = time.Second * {{ .Timeout }}
return {{ $nilResult }} err
}
{{ $param := index $method.Params 0 }}
{{ $isString := eq $param.Type "string" }}
{{ if not $isString }}
var data []byte
data, err = jsoniter.Marshal({{ $param.Name }})
if err != nil {
{{ $hasParam := gt (len $method.Params) 1 }}
{{ if $hasParam }}
{{ $param := index $method.Params 1 }}
{{ $isString := eq $param.Type "string" }}
{{ if not $isString }}
var data []byte
data, err = jsoniter.Marshal({{ $param.Name }})
if err != nil {
reqSpan.LogFields(log.Error(err))
return {{ $nilResult }} err
}
{{ end }}
if _, err = t.Write(
{{- if eq $param.Type "string" -}}
[]byte({{ $param.Name }})
{{- else -}}
data
{{- end -}}
); err != nil {
reqSpan.LogFields(log.Error(err))
return {{ $nilResult }} err
}
{{ end }}
if _, err = t.Write(
{{- if eq $param.Type "string" -}}
[]byte($param.Name)
{{- else -}}
data
{{- end -}}
); err != nil {
reqSpan.LogFields(log.Error(err))
return {{ $nilResult }} err
}
{{ end }}
reqCtx, cancelFn := context.WithTimeout(reqCtx, timeout)
defer cancelFn()
Expand Down

0 comments on commit a8b2a4a

Please sign in to comment.