This repository has been archived by the owner on May 16, 2018. It is now read-only.
/
spec.go
81 lines (72 loc) · 2.19 KB
/
spec.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 transformation
import (
"github.com/gogo/protobuf/types"
"github.com/solo-io/gloo/pkg/protoutil"
)
// this goes on the route extension
type RouteExtension struct {
Parameters *Parameters `json:"parameters,omitempty"`
ResponseTemplate *Template `json:"response_template,omitempty"`
ResponseParams *Parameters `json:"response_parameters,omitempty"`
}
type Parameters struct {
// headers that will be used to derive the data for processing the output templates
// if no syntax containing {variables} are detected in the header value,
// the whole value will be substituted by its name into the template
// for example:
/*
input:
header_parmeters:
x-header-foo: bar
output:
body_template: "{\"path\": {{ path }}}"
*/
Headers map[string]string `json:"headers"`
Path string `json:"path,omitempty"`
Authority string `json:"authority"`
//TODO: support query params
//TODO: support form params
}
// SPECIAL VARIABLE NAMES:
// these variables will be provided even if there is no parameter source
// path
// method
// scheme
// authority
// any field from the request body, assuming it's json
// this goes on the function spec
// or on the response transformation
type Template struct {
Path string `json:"path"`
Header map[string]string `json:"headers"`
// body is a pointer because, if null, pass through original body
Body *string `json:"body"`
// if enabled, the request body will be passed through untouched
PassthroughBody bool `json:"passthrough_body"`
//TODO: support query template
//TODO: support form template
}
func DecodeRouteExtension(generic *types.Struct) (RouteExtension, error) {
var s RouteExtension
err := protoutil.UnmarshalStruct(generic, &s)
return s, err
}
func EncodeRouteExtension(spec RouteExtension) *types.Struct {
v1Spec, err := protoutil.MarshalStruct(spec)
if err != nil {
panic(err)
}
return v1Spec
}
func DecodeFunctionSpec(generic *types.Struct) (Template, error) {
var s Template
err := protoutil.UnmarshalStruct(generic, &s)
return s, err
}
func EncodeFunctionSpec(spec Template) *types.Struct {
v1Spec, err := protoutil.MarshalStruct(spec)
if err != nil {
panic(err)
}
return v1Spec
}