Skip to content

Commit 73c4b56

Browse files
committed
Added parsing pipelines for routes
1 parent 6e7e848 commit 73c4b56

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

parse_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,20 @@ func TestParseRoutes(t *testing.T) {
4343
t.Errorf("Expected to get create_user route v4 schema, but got nil")
4444
}
4545
}
46+
47+
func TestParseRoute(t *testing.T) {
48+
routeLine := "post /login, name: 'login' | jwt {\"success\": true}"
49+
route, err := ParseRoute([]byte(routeLine))
50+
if err != nil {
51+
t.Errorf("Unexpected error: %v", err)
52+
}
53+
if len(route.PluginPipelines) != 1 {
54+
t.Errorf("Expected to parse 1 plugin pipeline, but got: %v", len(route.PluginPipelines))
55+
}
56+
if route.PluginPipelines[0].Name != "jwt" {
57+
t.Errorf("Expected plugin name to be 'jwt', but got: '%v'", route.PluginPipelines[0].Name)
58+
}
59+
if route.PluginPipelines[0].Argument["success"] == nil {
60+
t.Errorf("Expected plugin argument to have success key, but got none")
61+
}
62+
}

parser.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"github.com/BurntSushi/toml"
@@ -112,8 +113,12 @@ func ParseApiSettings(api *Api, line []byte) (bool, error) {
112113
}
113114

114115
func ParseRoute(line []byte) (*Route, error) {
115-
route := &Route{Versions: make(map[int]*RouteVersion)}
116-
chunks := bytes.Split(line, []byte(","))
116+
route := &Route{
117+
Versions: make(map[int]*RouteVersion),
118+
PluginPipelines: make([]*PluginPipeline, 0),
119+
}
120+
pipelines := bytes.Split(line, []byte("|"))
121+
chunks := bytes.Split(pipelines[0], []byte(","))
117122
urlParams := bytes.Split(chunks[0], []byte(" "))
118123
route.Method = strings.ToUpper(string(urlParams[0]))
119124
route.Path = string(urlParams[1])
@@ -139,9 +144,38 @@ func ParseRoute(line []byte) (*Route, error) {
139144
}
140145
}
141146
}
147+
if len(pipelines) > 0 {
148+
for i := 1; i < len(pipelines); i++ {
149+
pipeline := pipelines[i]
150+
pluginPipeline, err := ParsePluginPipeline(pipeline)
151+
if err != nil {
152+
return nil, err
153+
}
154+
route.PluginPipelines = append(route.PluginPipelines, pluginPipeline)
155+
}
156+
}
142157
return route, nil
143158
}
144159

160+
func ParsePluginPipeline(content []byte) (*PluginPipeline, error) {
161+
content = bytes.TrimSpace(content)
162+
chunks := bytes.Split(content, []byte(" "))
163+
if len(chunks) == 0 {
164+
return nil, errors.New("Plugin name not supplied in pipeline")
165+
}
166+
pp := &PluginPipeline{Name: string(chunks[0])}
167+
if len(chunks) == 1 {
168+
return pp, nil
169+
}
170+
jsonContent := bytes.Join(chunks[1:], []byte(" "))
171+
arg := make(map[string]interface{})
172+
err := json.Unmarshal(jsonContent, &arg)
173+
if err != nil {
174+
return nil, err
175+
}
176+
pp.Argument = arg
177+
return pp, nil
178+
}
145179
func ParseSchema(path string, route *Route) error {
146180
files, err := filepath.Glob(path + "/schemas/" + route.Name + ".v[0-9]*.schema")
147181
if err != nil {

route.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ import (
1212
)
1313

1414
type Route struct {
15-
Name string
16-
Method string
17-
Path string
18-
Collection bool
19-
Custom bool
20-
Versions map[int]*RouteVersion
15+
Name string
16+
Method string
17+
Path string
18+
Collection bool
19+
Custom bool
20+
Versions map[int]*RouteVersion
21+
PluginPipelines []*PluginPipeline
22+
}
23+
24+
type PluginPipeline struct {
25+
Name string
26+
Argument map[string]interface{}
2127
}
2228

2329
type RouteVersion struct {

0 commit comments

Comments
 (0)