Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
ca1b96f9
Commit
ca1b96f9
authored
Nov 06, 2018
by
Viktor Vassilyev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(Template): use interface http.FileSystem
parent
771fe354
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
73 deletions
+59
-73
fs.go
fs.go
+51
-15
template.go
template.go
+7
-6
template_test.go
template_test.go
+1
-52
No files found.
fs.go
View file @
ca1b96f9
...
...
@@ -6,25 +6,61 @@ import (
"path/filepath"
)
type
IFileSystem
interface
{
http
.
FileSystem
Walk
(
string
,
filepath
.
WalkFunc
)
error
}
// A File is returned by a FileSystem's Open method and can be
// served by the FileServer implementation.
//
// The methods should behave the same as those on an *os.File.
type
File
struct
{
*
os
.
File
}
type
FileSystem
struct
{
}
func
(
d
FileSystem
)
Open
(
name
string
)
(
http
.
File
,
error
)
{
return
os
.
Open
(
name
)
}
func
(
d
FileSystem
)
Walk
(
root
string
,
walkFn
filepath
.
WalkFunc
)
error
{
return
filepath
.
Walk
(
root
,
walkFn
)
func
Walk
(
fs
http
.
FileSystem
,
root
string
,
walkFn
filepath
.
WalkFunc
)
error
{
f
,
err
:=
fs
.
Open
(
root
)
if
err
!=
nil
{
return
err
}
info
,
err
:=
f
.
Stat
()
if
err
!=
nil
{
err
=
walkFn
(
root
,
nil
,
err
)
}
else
{
err
=
walk
(
fs
,
root
,
info
,
walkFn
)
}
if
err
==
filepath
.
SkipDir
{
return
nil
}
return
err
}
// walk recursively descends path, calling walkFn.
func
walk
(
fs
http
.
FileSystem
,
path
string
,
info
os
.
FileInfo
,
walkFn
filepath
.
WalkFunc
)
error
{
var
err
error
if
!
info
.
IsDir
()
{
return
walkFn
(
path
,
info
,
nil
)
}
dir
,
err
:=
fs
.
Open
(
path
)
defer
dir
.
Close
()
dirs
,
err
:=
dir
.
Readdir
(
-
1
)
err1
:=
walkFn
(
path
,
info
,
err
)
// If err != nil, walk can't walk into this directory.
// err1 != nil means walkFn want walk to skip this directory or stop walking.
// Therefore, if one of err and err1 isn't nil, walk will return.
if
err
!=
nil
||
err1
!=
nil
{
// The caller's behavior is controlled by the return value, which is decided
// by walkFn. walkFn may ignore err and return nil.
// If walkFn returns SkipDir, it will be handled by the caller.
// So walk should return whatever walkFn returns.
return
err1
}
for
_
,
fileInfo
:=
range
dirs
{
filename
:=
filepath
.
Join
(
path
,
fileInfo
.
Name
())
err
=
walk
(
fs
,
filename
,
fileInfo
,
walkFn
)
if
err
!=
nil
{
if
!
fileInfo
.
IsDir
()
||
err
!=
filepath
.
SkipDir
{
return
err
}
}
}
return
nil
}
template.go
View file @
ca1b96f9
...
...
@@ -20,6 +20,7 @@ import (
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
...
...
@@ -201,7 +202,7 @@ func BuildTemplate(dir string, files ...string) error {
root
:
dir
,
files
:
make
(
map
[
string
][]
string
),
}
err
=
fs
.
Walk
(
dir
,
func
(
path
string
,
f
os
.
FileInfo
,
err
error
)
error
{
err
=
Walk
(
fs
,
dir
,
func
(
path
string
,
f
os
.
FileInfo
,
err
error
)
error
{
return
self
.
visit
(
path
,
f
,
err
)
})
if
err
!=
nil
{
...
...
@@ -235,7 +236,7 @@ func BuildTemplate(dir string, files ...string) error {
return
nil
}
func
getTplDeep
(
root
string
,
fs
I
FileSystem
,
file
string
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
func
getTplDeep
(
root
string
,
fs
http
.
FileSystem
,
file
string
,
parent
string
,
t
*
template
.
Template
)
(
*
template
.
Template
,
[][]
string
,
error
)
{
var
fileAbsPath
string
var
rParent
string
var
err
error
...
...
@@ -279,7 +280,7 @@ func getTplDeep(root string, fs IFileSystem, file string, parent string, t *temp
return
t
,
allSub
,
nil
}
func
getTemplate
(
root
string
,
fs
I
FileSystem
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
getTemplate
(
root
string
,
fs
http
.
FileSystem
,
file
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
template
.
New
(
file
)
.
Delims
(
BConfig
.
WebConfig
.
TemplateLeft
,
BConfig
.
WebConfig
.
TemplateRight
)
.
Funcs
(
beegoTplFuncMap
)
var
subMods
[][]
string
t
,
subMods
,
err
=
getTplDeep
(
root
,
fs
,
file
,
""
,
t
)
...
...
@@ -294,7 +295,7 @@ func getTemplate(root string, fs IFileSystem, file string, others ...string) (t
return
}
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
fs
I
FileSystem
,
subMods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
func
_getTemplate
(
t0
*
template
.
Template
,
root
string
,
fs
http
.
FileSystem
,
subMods
[][]
string
,
others
...
string
)
(
t
*
template
.
Template
,
err
error
)
{
t
=
t0
for
_
,
m
:=
range
subMods
{
if
len
(
m
)
==
2
{
...
...
@@ -347,9 +348,9 @@ func _getTemplate(t0 *template.Template, root string, fs IFileSystem, subMods []
return
}
type
templateFSFunc
func
()
I
FileSystem
type
templateFSFunc
func
()
http
.
FileSystem
func
defaultFSFunc
()
I
FileSystem
{
func
defaultFSFunc
()
http
.
FileSystem
{
return
FileSystem
{}
}
func
SetTemplateFSFunc
(
fnt
templateFSFunc
)
{
...
...
template_test.go
View file @
ca1b96f9
...
...
@@ -267,57 +267,6 @@ type TestingFileSystem struct {
func
(
d
TestingFileSystem
)
Open
(
name
string
)
(
http
.
File
,
error
)
{
return
d
.
assetfs
.
Open
(
name
)
}
func
(
d
TestingFileSystem
)
Walk
(
root
string
,
walkFn
filepath
.
WalkFunc
)
error
{
f
,
err
:=
d
.
Open
(
root
)
if
err
!=
nil
{
return
err
}
info
,
err
:=
f
.
Stat
()
if
err
!=
nil
{
err
=
walkFn
(
root
,
nil
,
err
)
}
else
{
err
=
d
.
walk
(
root
,
info
,
walkFn
)
}
if
err
==
filepath
.
SkipDir
{
return
nil
}
return
err
}
// walk recursively descends path, calling walkFn.
func
(
d
TestingFileSystem
)
walk
(
path
string
,
info
os
.
FileInfo
,
walkFn
filepath
.
WalkFunc
)
error
{
var
err
error
if
!
info
.
IsDir
()
{
return
walkFn
(
path
,
info
,
nil
)
}
dir
,
err
:=
d
.
Open
(
path
)
defer
dir
.
Close
()
dirs
,
err
:=
dir
.
Readdir
(
-
1
)
err1
:=
walkFn
(
path
,
info
,
err
)
// If err != nil, walk can't walk into this directory.
// err1 != nil means walkFn want walk to skip this directory or stop walking.
// Therefore, if one of err and err1 isn't nil, walk will return.
if
err
!=
nil
||
err1
!=
nil
{
// The caller's behavior is controlled by the return value, which is decided
// by walkFn. walkFn may ignore err and return nil.
// If walkFn returns SkipDir, it will be handled by the caller.
// So walk should return whatever walkFn returns.
return
err1
}
for
_
,
fileInfo
:=
range
dirs
{
filename
:=
filepath
.
Join
(
path
,
fileInfo
.
Name
())
err
=
d
.
walk
(
filename
,
fileInfo
,
walkFn
)
if
err
!=
nil
{
if
!
fileInfo
.
IsDir
()
||
err
!=
filepath
.
SkipDir
{
return
err
}
}
}
return
nil
}
var
outputBinData
=
`<!DOCTYPE html>
<html>
...
...
@@ -341,7 +290,7 @@ var outputBinData = `<!DOCTYPE html>
`
func
TestFsBinData
(
t
*
testing
.
T
)
{
SetTemplateFSFunc
(
func
()
I
FileSystem
{
SetTemplateFSFunc
(
func
()
http
.
FileSystem
{
return
TestingFileSystem
{
&
assetfs
.
AssetFS
{
Asset
:
testdata
.
Asset
,
AssetDir
:
testdata
.
AssetDir
,
AssetInfo
:
testdata
.
AssetInfo
}}
})
dir
:=
"views"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment