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
976f5182
Commit
976f5182
authored
Oct 29, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
caddyfile: Better string and number handling
parent
0f19df8a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
10 deletions
+39
-10
caddy/caddyfile/json.go
caddy/caddyfile/json.go
+8
-4
caddy/caddyfile/json_test.go
caddy/caddyfile/json_test.go
+31
-6
No files found.
caddy/caddyfile/json.go
View file @
976f5182
...
@@ -25,7 +25,7 @@ func ToJSON(caddyfile []byte) ([]byte, error) {
...
@@ -25,7 +25,7 @@ func ToJSON(caddyfile []byte) ([]byte, error) {
block
:=
ServerBlock
{
Body
:
make
(
map
[
string
]
interface
{})}
block
:=
ServerBlock
{
Body
:
make
(
map
[
string
]
interface
{})}
for
_
,
host
:=
range
sb
.
HostList
()
{
for
_
,
host
:=
range
sb
.
HostList
()
{
block
.
Hosts
=
append
(
block
.
Hosts
,
host
)
block
.
Hosts
=
append
(
block
.
Hosts
,
strings
.
TrimSuffix
(
host
,
":"
)
)
}
}
for
dir
,
tokens
:=
range
sb
.
Tokens
{
for
dir
,
tokens
:=
range
sb
.
Tokens
{
...
@@ -107,7 +107,7 @@ func FromJSON(jsonBytes []byte) ([]byte, error) {
...
@@ -107,7 +107,7 @@ func FromJSON(jsonBytes []byte) ([]byte, error) {
if
i
>
0
{
if
i
>
0
{
result
+=
", "
result
+=
", "
}
}
result
+=
host
result
+=
strings
.
TrimSuffix
(
host
,
":"
)
}
}
result
+=
jsonToText
(
sb
.
Body
,
1
)
result
+=
jsonToText
(
sb
.
Body
,
1
)
}
}
...
@@ -122,11 +122,15 @@ func jsonToText(scope interface{}, depth int) string {
...
@@ -122,11 +122,15 @@ func jsonToText(scope interface{}, depth int) string {
switch
val
:=
scope
.
(
type
)
{
switch
val
:=
scope
.
(
type
)
{
case
string
:
case
string
:
if
strings
.
ContainsAny
(
val
,
"
\"
\n\t\r
"
)
{
result
+=
` "`
+
strings
.
Replace
(
val
,
"
\"
"
,
"
\\\"
"
,
-
1
)
+
`"`
}
else
{
result
+=
" "
+
val
result
+=
" "
+
val
}
case
int
:
case
int
:
result
+=
" "
+
strconv
.
Itoa
(
val
)
result
+=
" "
+
strconv
.
Itoa
(
val
)
case
float64
:
case
float64
:
result
+=
" "
+
fmt
.
Sprintf
(
"%
f
"
,
val
)
result
+=
" "
+
fmt
.
Sprintf
(
"%
v
"
,
val
)
case
bool
:
case
bool
:
result
+=
" "
+
fmt
.
Sprintf
(
"%t"
,
val
)
result
+=
" "
+
fmt
.
Sprintf
(
"%t"
,
val
)
case
map
[
string
]
interface
{}
:
case
map
[
string
]
interface
{}
:
...
...
caddy/caddyfile/json_test.go
View file @
976f5182
...
@@ -6,26 +6,26 @@ var tests = []struct {
...
@@ -6,26 +6,26 @@ var tests = []struct {
caddyfile
,
json
string
caddyfile
,
json
string
}{
}{
{
// 0
{
// 0
caddyfile
:
`foo
:
{
caddyfile
:
`foo {
root /bar
root /bar
}`
,
}`
,
json
:
`[{"hosts":["foo
:
"],"body":{"root":["/bar"]}}]`
,
json
:
`[{"hosts":["foo"],"body":{"root":["/bar"]}}]`
,
},
},
{
// 1
{
// 1
caddyfile
:
`host1
:, host2:
{
caddyfile
:
`host1
, host2
{
dir {
dir {
def
def
}
}
}`
,
}`
,
json
:
`[{"hosts":["host1
:","host2:
"],"body":{"dir":[{"def":null}]}}]`
,
json
:
`[{"hosts":["host1
","host2
"],"body":{"dir":[{"def":null}]}}]`
,
},
},
{
// 2
{
// 2
caddyfile
:
`host1
:, host2:
{
caddyfile
:
`host1
, host2
{
dir abc {
dir abc {
def ghi
def ghi
}
}
}`
,
}`
,
json
:
`[{"hosts":["host1
:","host2:
"],"body":{"dir":["abc",{"def":["ghi"]}]}}]`
,
json
:
`[{"hosts":["host1
","host2
"],"body":{"dir":["abc",{"def":["ghi"]}]}}]`
,
},
},
{
// 3
{
// 3
caddyfile
:
`host1:1234, host2:5678 {
caddyfile
:
`host1:1234, host2:5678 {
...
@@ -34,6 +34,31 @@ var tests = []struct {
...
@@ -34,6 +34,31 @@ var tests = []struct {
}`
,
}`
,
json
:
`[{"hosts":["host1:1234","host2:5678"],"body":{"dir":["abc",{}]}}]`
,
json
:
`[{"hosts":["host1:1234","host2:5678"],"body":{"dir":["abc",{}]}}]`
,
},
},
{
// 4
caddyfile
:
`host {
foo "bar baz"
}`
,
json
:
`[{"hosts":["host"],"body":{"foo":["bar baz"]}}]`
,
},
{
// 5
caddyfile
:
`host, host:80 {
foo "bar \"baz\""
}`
,
json
:
`[{"hosts":["host","host:80"],"body":{"foo":["bar \"baz\""]}}]`
,
},
{
// 6
caddyfile
:
`host {
foo "bar
baz"
}`
,
json
:
`[{"hosts":["host"],"body":{"foo":["bar\nbaz"]}}]`
,
},
{
// 7
caddyfile
:
`host {
dir 123 4.56 true
}`
,
json
:
`[{"hosts":["host"],"body":{"dir":["123","4.56","true"]}}]`
,
// NOTE: I guess we assume numbers and booleans should be encoded as strings...?
},
}
}
func
TestToJSON
(
t
*
testing
.
T
)
{
func
TestToJSON
(
t
*
testing
.
T
)
{
...
...
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