Skip to content

Commit 4982503

Browse files
committed
update routes
- nest routes with route directive ‘require’. Required (imported) directives will have parent paths prefixed. - move provider/go statement init/execution prior to sub route execution.
1 parent 0268f66 commit 4982503

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

loader.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func Load(path string) (*Project, error) {
2323
var ext = filepath.Ext(path)
2424

2525
switch ext {
26-
case ".yml":
27-
err = LoadYAML(path, env)
28-
default:
29-
err = LoadSE(path, env)
26+
case ".yml":
27+
err = LoadYAML(path, env)
28+
default:
29+
err = LoadSE(path, env)
3030
}
3131

3232
if err != nil {

transpiler/http_gen.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func ProcessRoute(p *samb.Project, r samb.Route, path string, providers []string
4949

5050
providers = append(providers, r.Provide...)
5151

52+
if len(r.Require) > 0 {
53+
r.Route = ImportRoutes(r)
54+
}
55+
5256
if len(r.Route) > 0 {
5357

5458
newRoot := path + r.Path
@@ -75,9 +79,27 @@ func ProcessRoute(p *samb.Project, r samb.Route, path string, providers []string
7579
return
7680
}
7781

78-
h := GetHandler(p, r, providers)
82+
middleware,h := GetHandler(p, r, providers)
83+
84+
def = WrapEndpoint(path, r, middleware + def + h)
85+
86+
return
87+
}
88+
89+
// ImportRoutes will get the route definitions
90+
// specfied by route field Require
91+
func ImportRoutes(r samb.Route) (result []samb.Route) {
7992

80-
def = WrapEndpoint(path, r, def + h)
93+
for _,req := range r.Require {
94+
95+
file, err := samb.Load(req)
96+
97+
if err != nil {
98+
panic(err)
99+
}
100+
101+
result = append(result,file.Routes.Route...)
102+
}
81103

82104
return
83105
}

transpiler/transpiler_util_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ func TestWrapEndpoint(t *testing.T) {
125125
result := WrapEndpoint("", tt.route, "")
126126

127127
if result != expectedValues[i] {
128-
println(result)
129-
println("///")
128+
130129
t.Errorf("got %v, want %v", result, expectedValues[i])
131130
}
132131

@@ -136,13 +135,11 @@ func TestWrapEndpoint(t *testing.T) {
136135

137136
func TestGetHandler(t *testing.T) {
138137

139-
blankString := `
140-
`
138+
blankString := "\n"
141139

142140
expectedValues := []string{
143141
blankString,
144-
blankString,
145-
`
142+
blankString,`
146143
//
147144
var Foo = string("Foo")
148145
@@ -155,11 +152,10 @@ var Foo = string("Foo")
155152
for i, tt := range routeTests {
156153
t.Run(string(i), func(t *testing.T) {
157154

158-
result := GetHandler(testProject, tt.route, tt.route.Provide)
155+
result,_ := GetHandler(testProject, tt.route, tt.route.Provide)
159156

160157
if result != expectedValues[i] {
161-
println(result)
162-
println("///")
158+
163159
t.Errorf("got %v, want %v", result, expectedValues[i])
164160
}
165161

transpiler/transpiler_utils.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ func WrapEndpoint(path string, r samb.Route, h string) string {
3434
// GetHandler Generates the Go code executed specified to
3535
// be executed by
3636
// a samb route.
37-
func GetHandler(p *samb.Project, r samb.Route, providers []string) (handler string) {
37+
func GetHandler(p *samb.Project, r samb.Route, providers []string) (middleware, handler string) {
3838

3939
gocode := GetCustomCode(r)
40-
endPointCode := gocode + r.Handler
41-
var providerInitializer = GetProviderInits(p, providers, endPointCode)
40+
var providerInitializer = GetProviderInits(p, providers, gocode + r.Handler)
4241

43-
handler = providerInitializer + endPointCode
42+
middleware = providerInitializer + gocode
4443

4544
if r.Handler != "" {
46-
handler = "\n\ntools.ShortenPath(basePath, r)\n" + handler + "\nreturn"
45+
handler = "\n\ntools.ShortenPath(basePath, r)\n" + r.Handler + "\nreturn"
4746
}
4847

4948
return

types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type Route struct {
6868
// if the request path matches this
6969
// routes' path
7070
Strict bool
71+
// Require will import and nest the
72+
// routes from the specified SAMB sources.
73+
Require []string
7174
}
7275

7376
// Array of Go statements

0 commit comments

Comments
 (0)