/
encode_decode_files.go
154 lines (138 loc) · 5.5 KB
/
encode_decode_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package goakit
import (
"fmt"
"path/filepath"
"goa.design/goa/codegen"
"goa.design/goa/expr"
httpcodegen "goa.design/goa/http/codegen"
)
// EncodeDecodeFiles produces a set of go-kit transport encoders and decoders
// that wrap the corresponding generated goa functions.
func EncodeDecodeFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
fw := make([]*codegen.File, 2*len(root.API.HTTP.Services))
for i, r := range root.API.HTTP.Services {
fw[i] = serverEncodeDecode(genpkg, r)
}
for i, r := range root.API.HTTP.Services {
fw[i+len(root.API.HTTP.Services)] = clientEncodeDecode(genpkg, r)
}
return fw
}
// serverEncodeDecode returns the file defining the go-kit HTTP server encoding
// and decoding logic.
func serverEncodeDecode(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
path := filepath.Join(codegen.Gendir, "http", codegen.SnakeCase(svc.Name()), "kitserver", "encode_decode.go")
data := httpcodegen.HTTPServices.Get(svc.Name())
title := fmt.Sprintf("%s go-kit HTTP server encoders and decoders", svc.Name())
sections := []*codegen.SectionTemplate{
codegen.Header(title, "server", []*codegen.ImportSpec{
{Path: "context"},
{Path: "net/http"},
{Path: "strings"},
{Path: "github.com/go-kit/kit/transport/http", Name: "kithttp"},
{Path: "goa.design/goa", Name: "goa"},
{Path: "goa.design/goa/http", Name: "goahttp"},
{Path: genpkg + "/http/" + data.Service.Name + "/server"},
}),
}
for _, e := range data.Endpoints {
sections = append(sections, &codegen.SectionTemplate{
Name: "goakit-response-encoder",
Source: responseEncoderT,
Data: e,
})
if e.Payload.Ref != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "goakit-request-decoder",
Source: requestDecoderT,
Data: e,
})
}
if len(e.Errors) > 0 {
sections = append(sections, &codegen.SectionTemplate{
Name: "goakit-error-encoder",
Source: errorEncoderT,
Data: e,
})
}
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
// clientEncodeDecode returns the file defining the go-kit HTTP client encoding
// and decoding logic.
func clientEncodeDecode(genpkg string, svc *expr.HTTPServiceExpr) *codegen.File {
path := filepath.Join(codegen.Gendir, "http", codegen.SnakeCase(svc.Name()), "kitclient", "encode_decode.go")
title := fmt.Sprintf("%s go-kit HTTP client encoders and decoders", svc.Name())
data := httpcodegen.HTTPServices.Get(svc.Name())
sections := []*codegen.SectionTemplate{
codegen.Header(title, "client", []*codegen.ImportSpec{
{Path: "context"},
{Path: "net/http"},
{Path: "strings"},
{Path: "github.com/go-kit/kit/transport/http", Name: "kithttp"},
{Path: "goa.design/goa", Name: "goa"},
{Path: "goa.design/goa/http", Name: "goahttp"},
{Path: genpkg + "/http/" + data.Service.Name + "/client"},
}),
}
for _, e := range data.Endpoints {
if e.RequestEncoder != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "goakit-request-encoder",
Source: requestEncoderT,
Data: e,
})
}
if e.Result != nil || len(e.Errors) > 0 {
sections = append(sections, &codegen.SectionTemplate{
Name: "goakit-response-decoder",
Source: responseDecoderT,
Data: e,
})
}
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
// input: EndpointData
const requestEncoderT = `{{ printf "%s returns a go-kit EncodeRequestFunc suitable for encoding %s %s requests." .RequestEncoder .ServiceName .Method.Name | comment }}
func {{ .RequestEncoder }}(encoder func(*http.Request) goahttp.Encoder) kithttp.EncodeRequestFunc {
enc := client.{{ .RequestEncoder }}(encoder)
return func(_ context.Context, r *http.Request, v interface{}) error {
return enc(r, v)
}
}
`
// input: EndpointData
const requestDecoderT = `{{ printf "%s returns a go-kit DecodeRequestFunc suitable for decoding %s %s requests." .RequestDecoder .ServiceName .Method.Name | comment }}
func {{ .RequestDecoder }}(mux goahttp.Muxer, decoder func(*http.Request) goahttp.Decoder) kithttp.DecodeRequestFunc {
dec := server.{{ .RequestDecoder }}(mux, decoder)
return func(ctx context.Context, r *http.Request) (interface{}, error) {
r = r.WithContext(ctx)
return dec(r)
}
}
`
// input: EndpointData
const responseEncoderT = `{{ printf "%s returns a go-kit EncodeResponseFunc suitable for encoding %s %s responses." .ResponseEncoder .ServiceName .Method.Name | comment }}
func {{ .ResponseEncoder }}(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder) kithttp.EncodeResponseFunc {
return server.{{ .ResponseEncoder }}(encoder)
}
`
// input: EndpointData
const errorEncoderT = `{{ printf "%s returns a go-kit EncodeResponseFunc suitable for encoding errors returned by the %s %s endpoint." .ErrorEncoder .ServiceName .Method.Name | comment }}
func {{ .ErrorEncoder }}(encoder func(context.Context, http.ResponseWriter) goahttp.Encoder, formatter func(err error) goahttp.Statuser) kithttp.ErrorEncoder {
enc := server.{{ .ErrorEncoder }}(encoder, formatter)
return func(ctx context.Context, err error, w http.ResponseWriter) {
enc(ctx, w, err)
}
}
`
// input: EndpointData
const responseDecoderT = `{{ printf "%s returns a go-kit DecodeResponseFunc suitable for decoding %s %s responses." .ResponseDecoder .ServiceName .Method.Name | comment }}
func {{ .ResponseDecoder }}(decoder func(*http.Response) goahttp.Decoder) kithttp.DecodeResponseFunc {
dec := client.{{ .ResponseDecoder }}(decoder, false)
return func(ctx context.Context, resp *http.Response) (interface{}, error) {
return dec(resp)
}
}
`