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
ab4e20c2
Commit
ab4e20c2
authored
Jun 27, 2016
by
Matt Butcher
Committed by
GitHub
Jun 27, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #888 from technosophos/fix/887-no-value-fix
fix(tiller): stop printing <no value> when var is missing
parents
5cd8f4d0
96ac6ebc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
1 deletion
+20
-1
engine.go
pkg/engine/engine.go
+15
-1
engine_test.go
pkg/engine/engine_test.go
+5
-0
No files found.
pkg/engine/engine.go
View file @
ab4e20c2
...
...
@@ -19,6 +19,7 @@ package engine
import
(
"bytes"
"fmt"
"strings"
"text/template"
"github.com/Masterminds/sprig"
...
...
@@ -31,6 +32,9 @@ type Engine struct {
// FuncMap contains the template functions that will be passed to each
// render call. This may only be modified before the first call to Render.
FuncMap
template
.
FuncMap
// If strict is enabled, template rendering will fail if a template references
// a value that was not passed in.
Strict
bool
}
// New creates a new Go template Engine instance.
...
...
@@ -92,6 +96,13 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) {
// to share common blocks, but to make the entire thing feel like a file-based
// template engine.
t
:=
template
.
New
(
"gotpl"
)
if
e
.
Strict
{
t
.
Option
(
"missingkey=error"
)
}
else
{
// Not that zero will attempt to add default values for types it knows,
// but will still emit <no value> for others. We mitigate that later.
t
.
Option
(
"missingkey=zero"
)
}
files
:=
[]
string
{}
for
fname
,
r
:=
range
tpls
{
t
=
t
.
New
(
fname
)
.
Funcs
(
e
.
FuncMap
)
...
...
@@ -107,7 +118,10 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) {
if
err
:=
t
.
ExecuteTemplate
(
&
buf
,
file
,
tpls
[
file
]
.
vals
);
err
!=
nil
{
return
map
[
string
]
string
{},
fmt
.
Errorf
(
"render error in %q: %s"
,
file
,
err
)
}
rendered
[
file
]
=
buf
.
String
()
// Work around the issue where Go will emit "<no value>" even if Options(missing=zero)
// is set. Since missing=error will never get here, we do not need to handle
// the Strict case.
rendered
[
file
]
=
strings
.
Replace
(
buf
.
String
(),
"<no value>"
,
""
,
-
1
)
buf
.
Reset
()
}
...
...
pkg/engine/engine_test.go
View file @
ab4e20c2
...
...
@@ -46,6 +46,7 @@ func TestRender(t *testing.T) {
Templates
:
[]
*
chart
.
Template
{
{
Name
:
"test1"
,
Data
:
[]
byte
(
"{{.outer | title }} {{.inner | title}}"
)},
{
Name
:
"test2"
,
Data
:
[]
byte
(
"{{.global.callme | lower }}"
)},
{
Name
:
"test3"
,
Data
:
[]
byte
(
"{{.noValue}}"
)},
},
Values
:
&
chart
.
Config
{
Raw
:
"outer: DEFAULT
\n
inner: DEFAULT"
,
...
...
@@ -82,6 +83,10 @@ func TestRender(t *testing.T) {
if
out
[
"test2"
]
!=
expect
{
t
.
Errorf
(
"Expected %q, got %q"
,
expect
,
out
[
"test2"
])
}
expect
=
""
if
out
[
"test3"
]
!=
expect
{
t
.
Errorf
(
"Expected %q, got %q"
,
expect
,
out
[
"test3"
])
}
if
_
,
err
:=
e
.
Render
(
c
,
v
);
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %s"
,
err
)
...
...
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