Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
H
helm3
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
helm3
Commits
f4b7eb5b
Commit
f4b7eb5b
authored
Feb 21, 2017
by
Matt Butcher
Committed by
GitHub
Feb 21, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2000 from yuvipanda/add-tojson
Add ToJson and FromJson template functions
parents
28fbd42c
cd727506
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
0 deletions
+80
-0
files.go
pkg/chartutil/files.go
+29
-0
files_test.go
pkg/chartutil/files_test.go
+49
-0
engine.go
pkg/engine/engine.go
+2
-0
No files found.
pkg/chartutil/files.go
View file @
f4b7eb5b
...
...
@@ -17,6 +17,7 @@ package chartutil
import
(
"encoding/base64"
"encoding/json"
"path"
"strings"
...
...
@@ -189,3 +190,31 @@ func FromYaml(str string) map[string]interface{} {
}
return
m
}
// ToJson takes an interface, marshals it to json, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func
ToJson
(
v
interface
{})
string
{
data
,
err
:=
json
.
Marshal
(
v
)
if
err
!=
nil
{
// Swallow errors inside of a template.
return
""
}
return
string
(
data
)
}
// FromJson converts a YAML document into a map[string]interface{}.
//
// This is not a general-purpose JSON parser, and will not parse all valid
// YAML documents. Additionally, because its intended use is within templates
// it tolerates errors. It will insert the returned error message string into
// m["error"] in the returned map.
func
FromJson
(
str
string
)
map
[
string
]
interface
{}
{
m
:=
map
[
string
]
interface
{}{}
if
err
:=
json
.
Unmarshal
([]
byte
(
str
),
&
m
);
err
!=
nil
{
m
[
"Error"
]
=
err
.
Error
()
}
return
m
}
pkg/chartutil/files_test.go
View file @
f4b7eb5b
...
...
@@ -141,3 +141,52 @@ one:
t
.
Fatal
(
"Expected parser error"
)
}
}
func
TestToJson
(
t
*
testing
.
T
)
{
expect
:=
`{"foo":"bar"}`
v
:=
struct
{
Foo
string
`json:"foo"`
}{
Foo
:
"bar"
,
}
if
got
:=
ToJson
(
v
);
got
!=
expect
{
t
.
Errorf
(
"Expected %q, got %q"
,
expect
,
got
)
}
}
func
TestFromJson
(
t
*
testing
.
T
)
{
doc
:=
`{
"hello": "world",
"one": {
"two": "three"
}
}
`
dict
:=
FromJson
(
doc
)
if
err
,
ok
:=
dict
[
"Error"
];
ok
{
t
.
Fatalf
(
"Parse error: %s"
,
err
)
}
if
len
(
dict
)
!=
2
{
t
.
Fatal
(
"expected two elements."
)
}
world
:=
dict
[
"hello"
]
if
world
.
(
string
)
!=
"world"
{
t
.
Fatal
(
"Expected the world. Is that too much to ask?"
)
}
// This should fail because we don't currently support lists:
doc2
:=
`
[
"one",
"two",
"three"
]
`
dict
=
FromJson
(
doc2
)
if
_
,
ok
:=
dict
[
"Error"
];
!
ok
{
t
.
Fatal
(
"Expected parser error"
)
}
}
pkg/engine/engine.go
View file @
f4b7eb5b
...
...
@@ -72,6 +72,8 @@ func FuncMap() template.FuncMap {
extra
:=
template
.
FuncMap
{
"toYaml"
:
chartutil
.
ToYaml
,
"fromYaml"
:
chartutil
.
FromYaml
,
"toJson"
:
chartutil
.
ToJson
,
"fromJson"
:
chartutil
.
FromJson
,
// This is a placeholder for the "include" function, which is
// late-bound to a template. By declaring it here, we preserve the
...
...
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