Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
f28d8b86
Commit
f28d8b86
authored
Mar 26, 2016
by
Craig Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding some useful utility functions for templates
parent
5989eb06
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
0 deletions
+69
-0
middleware/context.go
middleware/context.go
+38
-0
middleware/context_test.go
middleware/context_test.go
+31
-0
No files found.
middleware/context.go
View file @
f28d8b86
...
...
@@ -2,6 +2,7 @@ package middleware
import
(
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
...
...
@@ -219,3 +220,40 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin
return
buf
.
String
(),
nil
}
// ToLower will convert the given string to lower case.
func
(
c
Context
)
ToLower
(
s
string
)
string
{
return
strings
.
ToLower
(
s
)
}
// ToUpper will convert the given string to upper case.
func
(
c
Context
)
ToUpper
(
s
string
)
string
{
return
strings
.
ToUpper
(
s
)
}
// Split is a passthrough to strings.Split. It will split the first argument at each instance of the seperator and return a slice of strings.
func
(
c
Context
)
Split
(
s
string
,
sep
string
)
[]
string
{
return
strings
.
Split
(
s
,
sep
)
}
// Slice will convert the given arguments into a slice.
func
(
c
Context
)
Slice
(
elems
...
interface
{})
[]
interface
{}
{
return
elems
}
// Map will convert the arguments into a map. It expects alternating string keys and values. This is useful for building more complicated data structures
// if you are using subtemplates or things like that.
func
(
c
Context
)
Map
(
values
...
interface
{})
(
map
[
string
]
interface
{},
error
)
{
if
len
(
values
)
%
2
!=
0
{
return
nil
,
fmt
.
Errorf
(
"Map expects an even number of arguments"
)
}
dict
:=
make
(
map
[
string
]
interface
{},
len
(
values
)
/
2
)
for
i
:=
0
;
i
<
len
(
values
);
i
+=
2
{
key
,
ok
:=
values
[
i
]
.
(
string
)
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"Map keys must be strings"
)
}
dict
[
key
]
=
values
[
i
+
1
]
}
return
dict
,
nil
}
middleware/context_test.go
View file @
f28d8b86
...
...
@@ -11,6 +11,8 @@ import (
"strings"
"testing"
"time"
"text/template"
)
func
TestInclude
(
t
*
testing
.
T
)
{
...
...
@@ -611,3 +613,32 @@ func getContextOrFail(t *testing.T) Context {
func
getTestPrefix
(
testN
int
)
string
{
return
fmt
.
Sprintf
(
"Test [%d]: "
,
testN
)
}
func
TestTemplates
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
tmpl
,
expected
string
}{
{
`{{.ToUpper "aAA"}}`
,
"AAA"
},
{
`{{"bbb" | .ToUpper}}`
,
"BBB"
},
{
`{{.ToLower "CCc"}}`
,
"ccc"
},
{
`{{range (.Split "a,b,c" ",")}}{{.}}{{end}}`
,
"abc"
},
{
`{{range .Split "a,b,c" ","}}{{.}}{{end}}`
,
"abc"
},
{
`{{range .Slice "a" "b" "c"}}{{.}}{{end}}`
,
"abc"
},
{
`{{with .Map "A" "a" "B" "b" "c" "d"}}{{.A}}{{.B}}{{.c}}{{end}}`
,
"abd"
},
}
for
i
,
test
:=
range
tests
{
ctx
:=
getContextOrFail
(
t
)
tmpl
,
err
:=
template
.
New
(
""
)
.
Parse
(
test
.
tmpl
)
if
err
!=
nil
{
t
.
Errorf
(
"Test %d: %s"
,
i
,
err
)
continue
}
buf
:=
&
bytes
.
Buffer
{}
err
=
tmpl
.
Execute
(
buf
,
ctx
)
if
err
!=
nil
{
t
.
Errorf
(
"Test %d: %s"
,
i
,
err
)
continue
}
if
buf
.
String
()
!=
test
.
expected
{
t
.
Errorf
(
"Test %d: Results do not match. '%s' != '%s'"
,
i
,
buf
.
String
(),
test
.
expected
)
}
}
}
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