Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat: strings exprs support ToUpper and ToLower function (#364)
Browse files Browse the repository at this point in the history
* feat: strings exprs support ToUpper and ToLower function

Signed-off-by: Viet-Anh Duong <[email protected]>

* docs: add ToLower and ToUpper document

Signed-off-by: Viet-Anh Duong <[email protected]>
  • Loading branch information
vietanhduong authored Nov 23, 2021
1 parent de62f9e commit bd5c215
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
37 changes: 25 additions & 12 deletions docs/functions.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
### **time**

Time related functions.

<hr>
**`time.Now() Time`**

Executes function built-in Golang [time.Now](https://golang.org/pkg/time/#Now) function.
Returns an instance of Golang [Time](https://golang.org/pkg/time/#Time).
Executes function built-in Golang [time.Now](https://golang.org/pkg/time/#Now) function. Returns an instance of
Golang [Time](https://golang.org/pkg/time/#Time).

<hr>
**`time.Parse(val string) Time`**

Parses specified string using RFC3339 layout. Returns an instance of Golang [Time](https://golang.org/pkg/time/#Time).

### **strings**

String related functions.

<hr>
**`strings.ReplaceAll() string`**

Executes function built-in Golang [strings.ReplaceAll](https://pkg.go.dev/strings#ReplaceAll) function.

<hr>
**`strings.ToUpper() string`**

Executes function built-in Golang [strings.ToUpper](https://pkg.go.dev/strings#ToUpper) function.

<hr>
**`strings.ToLower() string`**

Executes function built-in Golang [strings.ToLower](https://pkg.go.dev/strings#ToLower) function.

### **sync**

<hr>
Expand All @@ -28,6 +40,7 @@ Executes function built-in Golang [strings.ReplaceAll](https://pkg.go.dev/string
Returns the `info` item value by given name stored in the Argo CD App sync operation.

### **repo**

Functions that provide additional information about Application source repository.
<hr>
**`repo.RepoURLToHTTPS(url string) string`**
Expand All @@ -46,7 +59,7 @@ Returns commit metadata. The commit must belong to the application source reposi

* `Message string` commit message
* `Author string` - commit author
* `Date time.Time` - commit creation date
* `Date time.Time` - commit creation date
* `Tags []string` - Associated tags

<hr>
Expand All @@ -56,15 +69,15 @@ Returns application details. `AppDetail` fields:

* `Type string` - AppDetail type
* `Helm HelmAppSpec` - Helm details
* Fields :
* `Name string`
* `ValueFiles []string`
* `Parameters []*v1alpha1.HelmParameter`
* `Values string`
* `FileParameters []*v1alpha1.HelmFileParameter`
* Methods :
* `GetParameterValueByName(Name string)` Retrieve value by name in Parameters field
* `GetFileParameterPathByName(Name string)` Retrieve path by name in FileParameters field
* Fields :
* `Name string`
* `ValueFiles []string`
* `Parameters []*v1alpha1.HelmParameter`
* `Values string`
* `FileParameters []*v1alpha1.HelmFileParameter`
* Methods :
* `GetParameterValueByName(Name string)` Retrieve value by name in Parameters field
* `GetFileParameterPathByName(Name string)` Retrieve path by name in FileParameters field
* `Ksonnet *apiclient.KsonnetAppSpec` - Ksonnet details
* `Kustomize *apiclient.KustomizeAppSpec` - Kustomize details
* `Directory *apiclient.DirectoryAppSpec` - Directory details
14 changes: 13 additions & 1 deletion expr/strings/strings.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package strings

import "strings"
import (
"strings"
)

func NewExprs() map[string]interface{} {
return map[string]interface{}{
"ReplaceAll": replaceAll,
"ToUpper": toUpper,
"ToLower": toLower,
}
}

func replaceAll(s, old, new string) string {
return strings.ReplaceAll(s, old, new)
}

func toUpper(s string) string {
return strings.ToUpper(s)
}

func toLower(s string) string {
return strings.ToLower(s)
}
46 changes: 46 additions & 0 deletions expr/strings/strings_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package strings

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -9,6 +10,8 @@ import (
func TestNewExprs(t *testing.T) {
funcs := []string{
"ReplaceAll",
"ToUpper",
"ToLower",
}

for _, fn := range funcs {
Expand All @@ -17,3 +20,46 @@ func TestNewExprs(t *testing.T) {
assert.True(t, hasFunc)
}
}

func TestReplaceAll(t *testing.T) {
exprs := NewExprs()

input := "test_replace"
expected := "test=replace"
replaceAllFn, ok := exprs["ReplaceAll"].(func(s, old, new string) string)
assert.True(t, ok)

actual := replaceAllFn(input, "_", "=")
assert.Equal(t, expected, actual)
}

func TestUpperAndLower(t *testing.T) {
testCases := []struct {
fn string
input string
expected string
}{
{
fn: "ToUpper",
input: "test",
expected: "TEST",
},
{
fn: "ToLower",
input: "TEST",
expected: "test",
},
}
exprs := NewExprs()

for _, testCase := range testCases {
t.Run(fmt.Sprintf("With success case: Func: %s", testCase.fn), func(tc *testing.T) {
toUpperFn, ok := exprs[testCase.fn].(func(s string) string)
assert.True(t, ok)

actual := toUpperFn(testCase.input)
assert.Equal(t, testCase.expected, actual)
})
}

}

0 comments on commit bd5c215

Please sign in to comment.