Skip to content

Commit

Permalink
Use t.pkgs helper instead of hardcoded package names.
Browse files Browse the repository at this point in the history
  • Loading branch information
iheanyi committed Nov 27, 2019
1 parent e5b2188 commit e8fbbb1
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions protoc-gen-twirp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ func (t *twirp) generateUtils() {
t.P()

t.P(`// doProtobufRequest makes a Protobuf request to the remote Twirp service.`)
t.P(`func doProtobufRequest(ctx `, t.pkgs["context"], `.Context, client HTTPClient, hooks *twirp.ClientHooks, url string, in, out `, t.pkgs["proto"], `.Message) (err error) {`)
t.P(`func doProtobufRequest(ctx `, t.pkgs["context"], `.Context, client HTTPClient, hooks *`, t.pkgs["twirp"], `.ClientHooks, url string, in, out `, t.pkgs["proto"], `.Message) (err error) {`)
t.P(` reqBodyBytes, err := `, t.pkgs["proto"], `.Marshal(in)`)
t.P(` if err != nil {`)
t.P(` return wrapInternal(err, "failed to marshal proto request")`)
Expand Down Expand Up @@ -738,7 +738,7 @@ func (t *twirp) generateUtils() {
t.P()

t.P(`// doJSONRequest makes a JSON request to the remote Twirp service.`)
t.P(`func doJSONRequest(ctx `, t.pkgs["context"], `.Context, client HTTPClient, hooks *twirp.ClientHooks, url string, in, out `, t.pkgs["proto"], `.Message) (err error) {`)
t.P(`func doJSONRequest(ctx `, t.pkgs["context"], `.Context, client HTTPClient, hooks *`, t.pkgs["twirp"], `.ClientHooks, url string, in, out `, t.pkgs["proto"], `.Message) (err error) {`)
t.P(` reqBody := `, t.pkgs["bytes"], `.NewBuffer(nil)`)
t.P(` marshaler := &`, t.pkgs["jsonpb"], `.Marshaler{OrigName: true}`)
t.P(` if err = marshaler.Marshal(reqBody, in); err != nil {`)
Expand Down Expand Up @@ -899,18 +899,18 @@ func (t *twirp) generateClient(name string, file *descriptor.FileDescriptorProto
t.P(`type `, structName, ` struct {`)
t.P(` client HTTPClient`)
t.P(` urls [`, methCnt, `]string`)
t.P(` opts twirp.ClientOptions`)
t.P(` opts `, t.pkgs["twirp"], `.ClientOptions`)
t.P(`}`)
t.P()
t.P(`// `, newClientFunc, ` creates a `, name, ` client that implements the `, servName, ` interface.`)
t.P(`// It communicates using `, name, ` and can be configured with a custom HTTPClient.`)
t.P(`func `, newClientFunc, `(addr string, client HTTPClient, opt ...twirp.ClientOption) `, servName, ` {`)
t.P(`func `, newClientFunc, `(addr string, client HTTPClient, opt ...`, t.pkgs["twirp"], `.ClientOption) `, servName, ` {`)
t.P(` var httpClient HTTPClient = client`)
t.P(` if c, ok := client.(*`, t.pkgs["http"], `.Client); ok {`)
t.P(` httpClient = withoutRedirects(c)`)
t.P(` }`)
t.P()
t.P(` opts := twirp.DefaultClientOptions()`)
t.P(` opts := `, t.pkgs["twirp"], `.DefaultClientOptions()`)
t.P(` for _, o := range opt {`)
t.P(` o(&opts)`)
t.P(` }`)
Expand Down Expand Up @@ -945,9 +945,9 @@ func (t *twirp) generateClient(name string, file *descriptor.FileDescriptorProto
t.P(` out := new(`, outputType, `)`)
t.P(` err := do`, name, `Request(ctx, c.client, c.opts.Hooks, c.urls[`, strconv.Itoa(i), `], in, out)`)
t.P(` if err != nil {`)
t.P(` twerr, ok := err.(twirp.Error)`)
t.P(` twerr, ok := err.(`, t.pkgs["twirp"], `.Error)`)
t.P(` if !ok {`)
t.P(` twerr = twirp.InternalErrorWith(err)`)
t.P(` twerr = `, t.pkgs["twirp"], `.InternalErrorWith(err)`)
t.P(` }`)
t.P(` callClientError(ctx, c.opts.Hooks, twerr)`)
t.P(` return nil, err`)
Expand All @@ -963,21 +963,21 @@ func (t *twirp) generateClient(name string, file *descriptor.FileDescriptorProto
}

func (t *twirp) generateClientHooks() {
t.P(`func callClientRequestFinished(ctx context.Context, h *twirp.ClientHooks) {`)
t.P(`func callClientRequestFinished(ctx `, t.pkgs["context"], `.Context, h *`, t.pkgs["twirp"], `.ClientHooks) {`)
t.P(` if h == nil || h.RequestFinished == nil {`)
t.P(` return`)
t.P(` }`)
t.P(` h.RequestFinished(ctx)`)
t.P(`}`)
t.P()
t.P(`func callClientRequestPrepared(ctx context.Context, h *twirp.ClientHooks, req *http.Request) (context.Context, error) {`)
t.P(`func callClientRequestPrepared(ctx `, t.pkgs["context"], `.Context, h *`, t.pkgs["twirp"], `.ClientHooks, req *http.Request) (`, t.pkgs["context"], `.Context, error) {`)
t.P(` if h == nil || h.RequestPrepared == nil {`)
t.P(` return ctx, nil`)
t.P(` }`)
t.P(` return h.RequestPrepared(ctx, req)`)
t.P(`}`)
t.P()
t.P(`func callClientError(ctx context.Context, h *twirp.ClientHooks, err twirp.Error) context.Context {`)
t.P(`func callClientError(ctx `, t.pkgs["context"], `.Context, h *`, t.pkgs["twirp"], `.ClientHooks, err `, t.pkgs["twirp"], `.Error) `, t.pkgs["context"], `.Context {`)
t.P(` if h == nil || h.Error == nil {`)
t.P(` return ctx`)
t.P(` }`)
Expand Down

0 comments on commit e8fbbb1

Please sign in to comment.