-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathheaders.go
81 lines (79 loc) Β· 2.29 KB
/
headers.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
package dsl
import (
"goa.design/goa/eval"
"goa.design/goa/expr"
)
// Headers describes HTTP request/response or gRPC response headers.
// When used in a HTTP expression, it groups a set of Header expressions and
// makes it possible to list required headers using the Required function.
// When used in a GRPC response expression, it defines the headers to be sent
// in the response metadata.
//
// To define HTTP headers, Headers must appear in an Service HTTP expression
// to define request headers common to all the service methods. Headers may
// also appear in a method, response or error HTTP expression to define the
// HTTP endpoint request and response headers.
//
// To define gRPC response header metadata, Headers must appear in a GRPC
// response expression.
//
// Headers accepts one argument which is a function listing the headers.
//
// Example:
//
// // HTTP headers
//
// var _ = Service("cellar", func() {
// HTTP(func() {
// Headers(func() {
// Header("version:Api-Version", String, "API version", func() {
// Enum("1.0", "2.0")
// })
// Required("version")
// })
// })
// })
//
// // gRPC response header metadata
//
// var CreateResult = ResultType("application/vnd.create", func() {
// Attributes(func() {
// Field(1, "name", String, "Name of the created resource")
// Field(2, "href", String, "Href of the created resource")
// })
// })
//
// Method("create", func() {
// Payload(CreatePayload)
// Result(CreateResult)
// GRPC(func() {
// Response(func() {
// Code(CodeOK)
// Headers(func() {
// Attribute("name") // "name" sent in the header metadata
// })
// })
// })
// })
//
func Headers(args interface{}) {
fn, ok := args.(func())
if !ok {
eval.InvalidArgError("function", args)
return
}
switch e := eval.Current().(type) {
case *expr.GRPCResponseExpr:
attr := &expr.AttributeExpr{}
if eval.Execute(fn, attr) {
e.Headers = expr.NewMappedAttributeExpr(attr)
}
default:
h := headers(eval.Current())
if h == nil {
eval.IncompatibleDSL()
return
}
eval.Execute(fn, h)
}
}