-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
127 lines (118 loc) · 2.92 KB
/
route.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/xeipuuv/gojsonschema"
"sort"
"strings"
"text/template"
)
type Route struct {
Name string
Method string
Path string
Collection bool
Custom bool
Versions map[int]*RouteVersion
PluginPipelines []*PluginPipeline
}
type PluginPipeline struct {
Name string
Argument map[string]interface{}
}
type RouteVersion struct {
Version int
Schema *gojsonschema.Schema
SqlTemplate *template.Template
}
func (self *Route) validate(params interface{}, version int) (string, error) {
route := self.Versions[version]
if route == nil {
return "", fmt.Errorf("Route version %v missing from %v route", version, self.Name)
}
if route.Schema == nil {
return "", nil
}
documentLoader := gojsonschema.NewGoLoader(params)
result, err := route.Schema.Validate(documentLoader)
if err != nil {
return "", err
}
if !result.Valid() {
errors := make(map[string]string)
for _, resultErr := range result.Errors() {
errors[resultErr.Field()] = resultErr.Description()
}
errorsJson, err := json.Marshal(errors)
if err != nil {
return "", err
}
return string(errorsJson), nil
}
return "", nil
}
func (self *Route) Sql(data map[string]interface{}, version int) (string, error) {
version = self.GetAvailableVersion(version)
route := self.Versions[version]
if route == nil {
return "", fmt.Errorf("Route version %v missing from %v route", version, self.Name)
}
var out bytes.Buffer
response, err := self.validate(data["params"], version)
if err != nil {
return "", err
}
if response != "" {
return response, errors.New("schema validation failed")
}
if !self.Custom {
out.Write([]byte("with response_table as ("))
}
err = route.SqlTemplate.Execute(&out, data)
if err != nil {
return "", err
}
if self.Custom {
return out.String(), nil
}
if self.Collection {
out.Write([]byte(") select array_to_json(array_agg(row_to_json(t))) as value from (select * from response_table) t"))
} else {
out.Write([]byte(") select row_to_json(t) as value from (select * from response_table) t"))
}
return out.String(), nil
}
func (self *Route) GetAvailableVersion(version int) int {
if self.Versions[version] != nil {
return version
}
versions := make([]int, 0, len(self.Versions))
for version := range self.Versions {
if version != 0 {
versions = append(versions, version)
}
}
sort.Ints(versions)
for i := 0; i < len(versions); i++ {
if versions[i] > version {
return versions[i]
}
}
if self.Versions[0] != nil {
return 0
}
return version
}
func quoteString(value interface{}) string {
stringValue := fmt.Sprintf("%v", value)
stringValue = strings.Replace(stringValue, "'", "''", -1)
return "'" + stringValue + "'"
}
func makeTemplate(t string) (*template.Template, error) {
funcMap := template.FuncMap{
"quote": quoteString,
}
return template.New("").Funcs(funcMap).Parse(t)
}