-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflector.go
44 lines (37 loc) · 1.12 KB
/
reflector.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
package kosmo
import (
"reflect"
"runtime"
"strings"
)
func runtimeFunctionName(function reflect.Value) string {
nameWithPackage := runtime.FuncForPC(function.Pointer()).Name()
parts := strings.SplitAfter(nameWithPackage, ".")
return parts[len(parts)-1]
}
func reflectStructInformations(structType reflect.Type) (string, []reflect.StructField) {
fields := []reflect.StructField{}
for i := 0; i < structType.NumField(); i++ {
fields = append(fields, structType.Field(i))
}
return structType.Name(), fields
}
func reflectArgumentFromResolverFunction(fn reflect.Value) (reflect.Type, bool) {
if fn.Type().NumIn() == 0 {
return nil, false
}
return fn.Type().In(0), true
}
func reflectTypeKind(value interface{}) string {
return reflect.TypeOf(value).Kind().String()
}
func createFunctionStructArgumentFromMap(argumentType reflect.Type, argumentMap map[string]interface{}) reflect.Value {
raw := reflect.New(argumentType).Elem()
for key, field := range argumentMap {
raw.FieldByName(key).Set(reflect.ValueOf(field))
}
return raw
}
func getType(genVar interface{}) string {
return reflect.ValueOf(genVar).Type().Name()
}