Skip to content

Commit

Permalink
replaced the stringsJoin handler to be a custom function that will al…
Browse files Browse the repository at this point in the history
…low an array instead of only []string, and will attempt to convert all members to string, so the resulting string array will be passed to strings.Join
  • Loading branch information
jucardi committed May 29, 2020
1 parent ce6769f commit a69b04e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ There are a few options to install the CLI tool.
1) Download the binary

```bash
sudo curl -L https://github.com/jucardi/infuse/releases/download/v1.0.4/infuse-$(uname -s)-$(uname -m) -o /usr/local/bin/infuse
sudo curl -L https://github.com/jucardi/infuse/releases/download/v1.0.5/infuse-$(uname -s)-$(uname -m) -o /usr/local/bin/infuse
```

2) Apply executable permissions
Expand Down
26 changes: 23 additions & 3 deletions templates/helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package helpers
import (
"encoding/json"
"fmt"
"github.com/jucardi/infuse/util/log"
"gopkg.in/yaml.v2"
"os"
"reflect"
"strings"

"github.com/jucardi/go-streams/streams"
"github.com/jucardi/infuse/util/log"
"gopkg.in/yaml.v2"
)

/** In this file are defined the generic helpers that may work for different template types. */
Expand All @@ -19,7 +22,7 @@ func RegisterCommon(manager IHelpersManager) {
_ = manager.Register("lowercase", strings.ToLower, "Returns a copy of the string s with all Unicode letters mapped to their lower case.")
_ = manager.Register("title", strings.ToTitle, "Returns a copy of the string s with all Unicode letters mapped to their title case.")
_ = manager.Register("stringsReplace", strings.Replace, "Returns a copy of the string s with the first n non-overlapping instances of old replaced by new.")
_ = manager.Register("stringsJoin", strings.Join, "Concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.")
_ = manager.Register("stringsJoin", stringsJoin, "Concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.")
_ = manager.Register("stringsSplit", strings.Split, "Slices s into all substrings separated by sep and returns a slice of the substrings between those separators.")
_ = manager.Register("stringsTrim", strings.Trim, "Returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.")
_ = manager.Register("stringsTrimLeft", strings.TrimLeft, "Returns a slice of the string s with all leading Unicode code points contained in cutset removed.")
Expand All @@ -40,6 +43,23 @@ func RegisterCommon(manager IHelpersManager) {

/** String helpers */

func stringsJoin(array interface{}, sep string) string {
if kind := reflect.TypeOf(array).Kind(); kind != reflect.Array && kind != reflect.Slice {
log.Panic("the first argument must be a valid array", kind.String())
}
val := reflect.ValueOf(array)
if !val.IsValid() {
return ""
}
arr := streams.From(array).Map(func(i interface{}) interface{} {
if str, ok := i.(string); ok {
return str
}
return fmt.Sprint(i)
}).ToArray().([]string)
return strings.Join(arr, sep)
}

func stringFn(arg interface{}) string {
return fmt.Sprintf("%+v", arg)
}
Expand Down

0 comments on commit a69b04e

Please sign in to comment.