-
Notifications
You must be signed in to change notification settings - Fork 66
/
join.go
45 lines (38 loc) · 1.07 KB
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package string
import (
"fmt"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression/function"
"strings"
)
func init() {
_ = function.Register(&fnJoin{})
}
type fnJoin struct {
}
func (fnJoin) Name() string {
return "join"
}
func (fnJoin) Sig() (paramTypes []data.Type, isVariadic bool) {
return []data.Type{data.TypeArray, data.TypeString}, false
}
func (fnJoin) Eval(params ...interface{}) (interface{}, error) {
a, err := coerce.ToArray(params[0])
if err != nil {
return nil, fmt.Errorf("error converting string.join first argument [%+v] to array: %s", params[0], err.Error())
}
str, err := coerce.ToString(params[1])
if err != nil {
return nil, fmt.Errorf("error converting string.join second argument [%+v] to string: %s", params[0], err.Error())
}
return strings.Join(toStringArray(a), str), nil
}
func toStringArray(array []interface{}) []string {
var tmpArray []string
for _, a := range array {
str, _ := coerce.ToString(a)
tmpArray = append(tmpArray, str)
}
return tmpArray
}