Skip to content

Commit 895192b

Browse files
committed
Moved template functions to its own file, and moved some of the functions from revel into it as well.
Moved module functions into module.go Enhanced fakeapp so it worked with tests Added ControllerType to route so lookups by name are not necessary (still required for wildcard matches though) Some code formatting
1 parent 620d98e commit 895192b

File tree

10 files changed

+506
-400
lines changed

10 files changed

+506
-400
lines changed

controller.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type Controller struct {
4040
Validation *Validation // Data validation helpers
4141
}
4242

43+
// The map of controllers, controllers are mapped by using the namespace|controller_name as the key
44+
var controllers = make(map[string]*ControllerType)
45+
4346
// NewController returns new controller instance for Request and Response
4447
func NewController(req *Request, resp *Response) *Controller {
4548
return &Controller{
@@ -285,9 +288,18 @@ func (c *Controller) Message(message string, args ...interface{}) (value string)
285288
// It sets the following properties: Name, Action, Type, MethodType
286289
func (c *Controller) SetAction(controllerName, methodName string) error {
287290

291+
return c.SetTypeAction(controllerName, methodName, nil)
292+
}
293+
// SetAction sets the assigns the Controller type, sets the action and initializes the controller
294+
func (c *Controller) SetTypeAction(controllerName, methodName string, typeOfController *ControllerType) error {
295+
288296
// Look up the controller and method types.
289-
if c.Type = ControllerTypeByName(controllerName, anyModule); c.Type==nil {
290-
return errors.New("revel/controller: failed to find controller " + controllerName)
297+
if typeOfController== nil {
298+
if c.Type = ControllerTypeByName(controllerName, anyModule); c.Type == nil {
299+
return errors.New("revel/controller: failed to find controller " + controllerName)
300+
}
301+
} else {
302+
c.Type = typeOfController
291303
}
292304

293305
// Note method name is case insensitive search
@@ -303,6 +315,7 @@ func (c *Controller) SetAction(controllerName, methodName string) error {
303315

304316
return nil
305317
}
318+
306319
func ControllerTypeByName(controllerName string, moduleSource *Module) (c *ControllerType) {
307320
var found bool
308321
if c, found = controllers[controllerName]; !found {
@@ -413,6 +426,8 @@ type MethodArg struct {
413426
Type reflect.Type
414427
}
415428

429+
// Adds the controller to the controllers map using its namespace, also adds it to the module list of controllers.
430+
// If the controller is in the main application it is added without its namespace as well.
416431
func AddControllerType(moduleSource *Module,controllerType reflect.Type,methods []*MethodType) (newControllerType *ControllerType) {
417432
if moduleSource==nil {
418433
moduleSource = appModule
@@ -448,18 +463,16 @@ func (ct *ControllerType) Method(name string) *MethodType {
448463
return nil
449464
}
450465

451-
// The controller name without the namespace
466+
// The controller name with the namespace
452467
func (ct *ControllerType) Name() (string) {
453468
return ct.Namespace + ct.ShortName()
454469
}
455470

456-
// The controller name with the namespace
471+
// The controller name without the namespace
457472
func (ct *ControllerType) ShortName() (string) {
458473
return strings.ToLower(ct.Type.Name())
459474
}
460475

461-
var controllers = make(map[string]*ControllerType)
462-
463476
// RegisterController registers a Controller and its Methods with Revel.
464477
func RegisterController(c interface{}, methods []*MethodType) {
465478
// De-star the controller type

fakeapp_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ func registerControllers() {
126126
},
127127
RenderArgNames: map[int][]string{},
128128
},
129+
{
130+
Name: "Index",
131+
Args: []*MethodArg{
132+
{Name: "foo", Type: reflect.TypeOf((*string)(nil))},
133+
{Name: "bar", Type: reflect.TypeOf((*string)(nil))},
134+
},
135+
RenderArgNames: map[int][]string{},
136+
},
129137
})
130138
}
131139
func startFakeBookingApp() {

module.go

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package revel
22

33
import (
4+
"go/build"
5+
"path/filepath"
46
"sort"
57
"strings"
6-
"path/filepath"
7-
"go/build"
88
)
99

1010
// Module specific functions
1111
type Module struct {
1212
Name, ImportPath, Path string
13-
ControllerTypeList []*ControllerType
13+
ControllerTypeList []*ControllerType
1414
}
1515

16-
const namespaceSeperator = "|" // ., : are already used
16+
// The namespace separator constant
17+
const namespaceSeperator = "|" // (note cannot be . or : as this is already used for routes)
1718

1819
var (
19-
anyModule = &Module{}
20-
appModule = &Module{Name:"App"}
20+
Modules []*Module // The list of modules in use
21+
anyModule = &Module{} // Wildcard search for controllers for a module (for backward compatible lookups)
22+
appModule = &Module{Name: "App"} // The app module
2123
)
2224

2325
// Returns the namespace for the module in the format `module_name|`
@@ -27,24 +29,73 @@ func (m *Module) Namespace() (namespace string) {
2729
}
2830

2931
// Returns the named controller and action that is in this module
30-
func (m *Module) ControllerByName(name,action string)(ctype *ControllerType) {
32+
func (m *Module) ControllerByName(name, action string) (ctype *ControllerType) {
3133
comparision := name
32-
if strings.Index(name,namespaceSeperator)<0 {
33-
comparision = m.Namespace() + name
34+
if strings.Index(name, namespaceSeperator) < 0 {
35+
comparision = m.Namespace() + name
3436
}
35-
for _,c := range m.ControllerTypeList {
36-
if strings.Index(c.Name(),comparision)>-1 {
37+
for _, c := range m.ControllerTypeList {
38+
if strings.Index(c.Name(), comparision) > -1 {
3739
ctype = c
3840
break
3941
}
4042
}
4143
return
4244
}
45+
46+
// Adds the controller type to this module
4347
func (m *Module) AddController(ct *ControllerType) {
44-
m.ControllerTypeList = append(m.ControllerTypeList,ct)
48+
m.ControllerTypeList = append(m.ControllerTypeList, ct)
4549
}
4650

51+
// Based on the full path given return the relevant module
52+
// Only to be used on initialization
53+
func ModuleFromPath(path string, addGopathToPath bool) (module *Module) {
54+
gopathList := filepath.SplitList(build.Default.GOPATH)
55+
56+
// See if the path exists in the module based
57+
for i := range Modules {
58+
if addGopathToPath {
59+
for _, gopath := range gopathList {
60+
if strings.HasPrefix(gopath+"/src/"+path, Modules[i].Path) {
61+
module = Modules[i]
62+
break
63+
}
64+
}
65+
} else {
66+
if strings.HasPrefix(path, Modules[i].Path) {
67+
module = Modules[i]
68+
break
69+
}
70+
71+
}
72+
73+
if module != nil {
74+
break
75+
}
76+
}
77+
return
78+
}
4779

80+
// ModuleByName returns the module of the given name, if loaded, case insensitive.
81+
func ModuleByName(name string) (m *Module, found bool) {
82+
// If the name ends with the namespace separator remove it
83+
if name[len(name)-1] == []byte(namespaceSeperator)[0] {
84+
name = name[:len(name)-1]
85+
}
86+
name = strings.ToLower(name)
87+
if name==strings.ToLower(appModule.Name) {
88+
return appModule,true
89+
}
90+
for _, module := range Modules {
91+
if strings.ToLower(module.Name) == name {
92+
return module, true
93+
}
94+
}
95+
return nil, false
96+
}
97+
98+
// Loads the modules specified in the config
4899
func loadModules() {
49100
keys := []string{}
50101
for _, key := range Config.Options("module.") {
@@ -77,32 +128,26 @@ func loadModules() {
77128
}
78129
}
79130

80-
// Based on the full path given return the relevant module
81-
// Only be used on initialization
82-
func ModuleFromPath(path string, addGopathToPath bool) (module *Module) {
83-
gopathList := filepath.SplitList(build.Default.GOPATH)
84-
85-
// See if the path exists in the module based
86-
for i := range Modules {
87-
if addGopathToPath {
88-
for _, gopath := range gopathList {
89-
if strings.HasPrefix(gopath+"/src/"+path, Modules[i].Path) {
90-
module = Modules[i]
91-
break
92-
}
93-
}
94-
} else {
95-
if strings.HasPrefix(path, Modules[i].Path) {
96-
module = Modules[i]
97-
break
98-
}
99-
131+
//
132+
func addModule(name, importPath, modulePath string) {
133+
Modules = append(Modules, &Module{Name: name, ImportPath: importPath, Path: modulePath})
134+
if codePath := filepath.Join(modulePath, "app"); DirExists(codePath) {
135+
CodePaths = append(CodePaths, codePath)
136+
if viewsPath := filepath.Join(modulePath, "app", "views"); DirExists(viewsPath) {
137+
TemplatePaths = append(TemplatePaths, viewsPath)
100138
}
139+
}
101140

102-
if module!=nil {
103-
break
104-
}
141+
INFO.Print("Loaded module ", filepath.Base(modulePath))
142+
143+
// Hack: There is presently no way for the testrunner module to add the
144+
// "test" subdirectory to the CodePaths. So this does it instead.
145+
if importPath == Config.StringDefault("module.testrunner", "github.com/revel/modules/testrunner") {
146+
INFO.Print("Found testrunner module, adding `tests` path ", filepath.Join(BasePath, "tests"))
147+
CodePaths = append(CodePaths, filepath.Join(BasePath, "tests"))
148+
}
149+
if testsPath := filepath.Join(modulePath, "tests"); DirExists(testsPath) {
150+
INFO.Print("Found tests path ", testsPath)
151+
CodePaths = append(CodePaths, testsPath)
105152
}
106-
return
107153
}
108-

namespace.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77

88
// Module matching template syntax allows for modules to replace this text with the name of the module declated on import
99
// this allows the reverse router to use correct syntax
10-
// Match _RNS_.static| or _RNS_|
11-
var namespaceReplacment, _ = regexp.Compile(`(_RNS_)(\.(.*?))?\|`)
10+
// Match _RNS_.static or _RNS_|
11+
var namespaceReplacement = regexp.MustCompile(`(_RNS_)(\.(.*?))?\|`)
1212

1313
// Function to replace the bytes data that may match the _RNS_ namespace specifier,
1414
// the replacement will be the current module.Name
1515
func namespaceReplace(fileBytes []byte, module *Module) ([]byte){
1616
newBytes, lastIndex := &bytes.Buffer{}, 0
17-
matches := namespaceReplacment.FindAllSubmatchIndex(fileBytes,-1)
17+
matches := namespaceReplacement.FindAllSubmatchIndex(fileBytes,-1)
1818
for _, match := range matches {
1919
// Write up to first bytes
2020
newBytes.Write(fileBytes[lastIndex:match[0]])

revel.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ var (
7474
// 3. user supplied configs (...) - User configs can override/add any from above
7575
ConfPaths []string
7676

77-
Modules []*Module
78-
7977
// Server config.
8078
//
8179
// Alert: This is how the app is configured, which may be different from
@@ -368,39 +366,6 @@ func ResolveImportPath(importPath string) (string, error) {
368366
return modPkg.Dir, nil
369367
}
370368

371-
func addModule(name, importPath, modulePath string) {
372-
Modules = append(Modules, &Module{Name: name, ImportPath: importPath, Path: modulePath})
373-
if codePath := filepath.Join(modulePath, "app"); DirExists(codePath) {
374-
CodePaths = append(CodePaths, codePath)
375-
if viewsPath := filepath.Join(modulePath, "app", "views"); DirExists(viewsPath) {
376-
TemplatePaths = append(TemplatePaths, viewsPath)
377-
}
378-
}
379-
380-
INFO.Print("Loaded module ", filepath.Base(modulePath))
381-
382-
// Hack: There is presently no way for the testrunner module to add the
383-
// "test" subdirectory to the CodePaths. So this does it instead.
384-
if importPath == Config.StringDefault("module.testrunner", "github.com/revel/modules/testrunner") {
385-
INFO.Print("Found testrunner module, adding `tests` path ", filepath.Join(BasePath, "tests"))
386-
CodePaths = append(CodePaths, filepath.Join(BasePath, "tests"))
387-
}
388-
if testsPath := filepath.Join(modulePath, "tests"); DirExists(testsPath) {
389-
INFO.Print("Found tests path ", testsPath)
390-
CodePaths = append(CodePaths, testsPath)
391-
}
392-
}
393-
394-
// ModuleByName returns the module of the given name, if loaded.
395-
func ModuleByName(name string) (m *Module, found bool) {
396-
for _, module := range Modules {
397-
if module.Name == name {
398-
return module, true
399-
}
400-
}
401-
return nil, false
402-
}
403-
404369
// CheckInit method checks `revel.Initialized` if not initialized it panics
405370
func CheckInit() {
406371
if !Initialized {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy