Skip to content

Commit e23b149

Browse files
authored
Merge pull request #370 from nginx-proxy/toupper-tolower
Add toLower and toUpper template functions
2 parents 59c838a + cde65d4 commit e23b149

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
379379
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
380380
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
381381
* *`trim $string`*: Removes whitespace from both sides of `$string`.
382+
* *`toLower $string`*: Replace capital letters in `$string` to lowercase.
383+
* *`toUpper $string`*: Replace lowercase letters in `$string` to uppercase.
382384
* *`when $condition $trueValue $falseValue`*: Returns the `$trueValue` when the `$condition` is `true` and the `$falseValue` otherwise
383385
* *`where $items $fieldPath $value`*: Filters an array or slice based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value. Returns an array of items having that value.
384386
* *`whereNot $items $fieldPath $value`*: Filters an array or slice based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value. Returns an array of items **not** having that value.

internal/dockergen/template.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@ func trim(s string) string {
408408
return strings.TrimSpace(s)
409409
}
410410

411+
// toLower return the string in lower case
412+
func toLower(s string) string {
413+
return strings.ToLower(s)
414+
}
415+
416+
// toUpper return the string in upper case
417+
func toUpper(s string) string {
418+
return strings.ToUpper(s)
419+
}
420+
411421
// when returns the trueValue when the condition is true and the falseValue otherwise
412422
func when(condition bool, trueValue, falseValue interface{}) interface{} {
413423
if condition {
@@ -446,6 +456,8 @@ func newTemplate(name string) *template.Template {
446456
"trimPrefix": trimPrefix,
447457
"trimSuffix": trimSuffix,
448458
"trim": trim,
459+
"toLower": toLower,
460+
"toUpper": toUpper,
449461
"when": when,
450462
"where": where,
451463
"whereNot": whereNot,

internal/dockergen/template_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,18 @@ func TestTrim(t *testing.T) {
737737
}
738738
}
739739

740+
func TestToLower(t *testing.T) {
741+
const str = ".RaNd0m StrinG_"
742+
const lowered = ".rand0m string_"
743+
assert.Equal(t, lowered, toLower(str), "Unexpected value from toLower()")
744+
}
745+
746+
func TestToUpper(t *testing.T) {
747+
const str = ".RaNd0m StrinG_"
748+
const uppered = ".RAND0M STRING_"
749+
assert.Equal(t, uppered, toUpper(str), "Unexpected value from toUpper()")
750+
}
751+
740752
func TestDict(t *testing.T) {
741753
containers := []*RuntimeContainer{
742754
{

0 commit comments

Comments
 (0)