-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
42 lines (38 loc) · 968 Bytes
/
strings.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
package walk
import (
"errors"
"reflect"
)
// Strings walks over all strings of the first parameter and executes the given function on it
func Strings(i interface{}, fn func(string) string) error {
rv := reflect.ValueOf(i)
if rv.Kind() != reflect.Ptr && rv.Kind() != reflect.Slice {
return errors.New("Couldn't set the value - need pointer or slice as argument")
}
return strings(rv, fn)
}
func strings(rv reflect.Value, fn func(string) string) error {
switch rv.Kind() {
case reflect.String:
applyString(rv, fn)
case reflect.Ptr:
return strings(rv.Elem(), fn)
case reflect.Slice:
for i := 0; i < rv.Len(); i++ {
iv := rv.Index(i)
if iv.Kind() == reflect.String {
applyString(iv, fn)
}
}
case reflect.Struct:
for i := 0; i < rv.NumField(); i++ {
if rv.Field(i).CanSet() {
applyString(rv.Field(i), fn)
}
}
}
return nil
}
func applyString(rv reflect.Value, fn func(string) string) {
rv.SetString(fn(rv.String()))
}