-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
tpl: Add default function #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de13eee
tpl: Add default function
moorereason c118ae1
docs: add better default example
moorereason 018bb35
tpl: fix default function
moorereason 63d7374
tpl: Add time note to default func description
moorereason e8032fa
tpl: Add custom index function
moorereason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,6 @@ import ( | |
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "github.com/spf13/cast" | ||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| "html/template" | ||
| "math/rand" | ||
| "path" | ||
|
|
@@ -29,6 +26,10 @@ import ( | |
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cast" | ||
| "github.com/spf13/viper" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| type tstNoStringer struct { | ||
|
|
@@ -1839,6 +1840,44 @@ func TestDateFormat(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestDefault(t *testing.T) { | ||
| for i, this := range []struct { | ||
| dflt interface{} | ||
| given interface{} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need nil-variants for these. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
| expected interface{} | ||
| }{ | ||
| {"5", 0, "5"}, | ||
|
|
||
| {"test1", "set", "set"}, | ||
| {"test2", "", "test2"}, | ||
|
|
||
| {[2]int{10, 20}, [2]int{1, 2}, [2]int{1, 2}}, | ||
| {[2]int{10, 20}, [0]int{}, [2]int{10, 20}}, | ||
|
|
||
| {[]string{"one"}, []string{"uno"}, []string{"uno"}}, | ||
| {[]string{"one"}, []string{}, []string{"one"}}, | ||
|
|
||
| {map[string]int{"one": 1}, map[string]int{"uno": 1}, map[string]int{"uno": 1}}, | ||
| {map[string]int{"one": 1}, map[string]int{}, map[string]int{"one": 1}}, | ||
|
|
||
| {10, 1, 1}, | ||
| {10, 0, 10}, | ||
|
|
||
| {float32(10), float32(1), float32(1)}, | ||
| {float32(10), 0, float32(10)}, | ||
|
|
||
| {complex(2, -2), complex(1, -1), complex(1, -1)}, | ||
| {complex(2, -2), complex(0, 0), complex(2, -2)}, | ||
|
|
||
| {struct{ f string }{f: "one"}, struct{ f string }{}, struct{ f string }{}}, | ||
| } { | ||
| res := dfault(this.dflt, this.given) | ||
| if !reflect.DeepEqual(this.expected, res) { | ||
| t.Errorf("[%d] default returned %v, but expected %v", i, res, this.expected) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestSafeHTML(t *testing.T) { | ||
| for i, this := range []struct { | ||
| str string | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should add Zero for time.Time (time.IsZero) to the list of not set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.