-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot.go
More file actions
40 lines (30 loc) · 1.11 KB
/
Copy pathdot.go
File metadata and controls
40 lines (30 loc) · 1.11 KB
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
package dot_notation
import (
"reflect"
"strings"
)
func Notate(out interface{}, notation string) interface{} {
if notation != "" && isStruct(out) {
var notations []string
notations = strings.Split(notation, ".")
if len(notations) == 0 && notation != "" {
notations = append(notations, notation)
}
var r reflect.Value
var newNotation string
if reflect.TypeOf(out).Kind() == reflect.Ptr {
r = reflect.ValueOf(out).Elem()
} else if reflect.TypeOf(reflect.ValueOf(out)).Kind() == reflect.Struct {
r = reflect.ValueOf(out)
}
if strings.Contains(notation, ".") {
newNotation = strings.ReplaceAll(notation, notations[0]+".", "")
}
return Notate(r.FieldByName(strings.Title(notations[0])).Interface(), newNotation)
}
return out
}
func isStruct(who interface{}) bool {
//reflect.Indirect(reflect.ValueOf(who)).Kind() // actually i haven't try indirect with non-pointer struct yet and don't know what's gonna happen when i use it.
return (reflect.ValueOf(who).Kind() == reflect.Ptr && reflect.ValueOf(who).Elem().Kind() == reflect.Struct) || (reflect.ValueOf(who).Kind() == reflect.Struct)
}