Skip to content

Commit 7e656e1

Browse files
committed
Added default api version 0
1 parent c76cfb7 commit 7e656e1

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

parse_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ func TestParseRoutes(t *testing.T) {
2121
if api.Routes[0].Collection != true {
2222
t.Errorf("Expected to get path collection to be true, but got false")
2323
}
24-
if api.Routes[0].Schema != nil {
24+
if api.Routes[0].Versions[0].Schema != nil {
2525
t.Errorf("Expected to get no route schema, but got")
2626
}
27-
if api.Routes[1].Schema == nil {
27+
if api.Routes[1].Versions[0].Schema == nil {
2828
t.Errorf("Expected to get route schema, but got nil")
2929
}
3030
if api.Version != 5 {

parser.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func ParseApiSettings(api *Api, line []byte) (bool, error) {
107107
}
108108

109109
func ParseRoute(line []byte) (*Route, error) {
110-
route := &Route{}
110+
route := &Route{Versions: make(map[int]*RouteVersion)}
111111
chunks := bytes.Split(line, []byte(","))
112112
urlParams := bytes.Split(chunks[0], []byte(" "))
113113
route.Method = strings.ToUpper(string(urlParams[0]))
@@ -146,7 +146,10 @@ func ParseSchema(path string, route *Route) error {
146146
if err != nil {
147147
return err
148148
}
149-
route.Schema = schema
149+
if route.Versions[0] == nil {
150+
route.Versions[0] = &RouteVersion{Version: 0}
151+
}
152+
route.Versions[0].Schema = schema
150153
return nil
151154
}
152155

@@ -159,7 +162,10 @@ func ParseSqlTemplate(path string, route *Route) error {
159162
if err != nil {
160163
return err
161164
}
162-
route.SqlTemplate = tmpl
165+
if route.Versions[0] == nil {
166+
route.Versions[0] = &RouteVersion{Version: 0}
167+
}
168+
route.Versions[0].SqlTemplate = tmpl
163169
return nil
164170
}
165171

route.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import (
1111
)
1212

1313
type Route struct {
14-
Name string
15-
Method string
16-
Path string
17-
Collection bool
18-
Custom bool
19-
Schema *gojsonschema.Schema
20-
SqlTemplate *template.Template
14+
Name string
15+
Method string
16+
Path string
17+
Collection bool
18+
Custom bool
19+
Versions map[int]*RouteVersion
2120
}
2221

2322
type RouteVersion struct {
@@ -27,11 +26,15 @@ type RouteVersion struct {
2726
}
2827

2928
func (self *Route) validate(params map[string]interface{}, version int) (string, error) {
30-
if self.Schema == nil {
29+
route := self.Versions[version]
30+
if route == nil {
31+
return "", fmt.Errorf("Route version %v missing from %v route", version, self.Name)
32+
}
33+
if route.Schema == nil {
3134
return "", nil
3235
}
3336
documentLoader := gojsonschema.NewGoLoader(params)
34-
result, err := self.Schema.Validate(documentLoader)
37+
result, err := route.Schema.Validate(documentLoader)
3538
if err != nil {
3639
return "", err
3740
}
@@ -50,6 +53,10 @@ func (self *Route) validate(params map[string]interface{}, version int) (string,
5053
}
5154

5255
func (self *Route) Sql(params map[string]interface{}, version int) (string, error) {
56+
route := self.Versions[version]
57+
if route == nil {
58+
return "", fmt.Errorf("Route version %v missing from %v route", version, self.Name)
59+
}
5360
var out bytes.Buffer
5461
response, err := self.validate(params, version)
5562
if err != nil {
@@ -61,7 +68,7 @@ func (self *Route) Sql(params map[string]interface{}, version int) (string, erro
6168
if !self.Custom {
6269
out.Write([]byte("with response_table as ("))
6370
}
64-
err = self.SqlTemplate.Execute(&out, params)
71+
err = route.SqlTemplate.Execute(&out, params)
6572
if err != nil {
6673
return "", err
6774
}

route_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ func TestRouteSql(t *testing.T) {
2929
t.Errorf("Expected not to get error, but got: %v", err)
3030
}
3131
route := &Route{
32-
SqlTemplate: tmpl,
33-
Schema: schema,
32+
Versions: map[int]*RouteVersion{
33+
0: {
34+
Version: 0,
35+
SqlTemplate: tmpl,
36+
Schema: schema,
37+
},
38+
},
3439
}
3540
params := make(map[string]interface{})
3641
params["id"] = 23

0 commit comments

Comments
 (0)