-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_rename.go
39 lines (31 loc) · 1.13 KB
/
type_rename.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
package yarql
import (
"log"
"reflect"
"strings"
)
var renamedTypes = map[string]string{}
// TypeRename renames the graphql type of the input type
// By default the typename of the struct is used but you might want to change this form time to time and with this you can
func TypeRename(goType interface{}, newName string, force ...bool) string {
t := reflect.TypeOf(goType)
originalName := t.Name()
if originalName == "" {
log.Panicf("GraphQl Can only rename struct type with type name\n")
}
if t.Kind() != reflect.Struct {
log.Panicf("GraphQl Cannot rename type of %s with name: %s and package: %s, can only rename Structs\n", t.Kind().String(), originalName, t.PkgPath())
}
newName = strings.TrimSpace(newName)
if len(newName) == 0 {
log.Panicf("GraphQl cannot rename to empty string on type: %s %s\n", t.PkgPath(), originalName)
}
if len(force) == 0 || !force[0] {
err := validGraphQlName([]byte(newName))
if err != nil {
log.Panicf("GraphQl cannot rename typeof of %s with name %s to %s, err: %s", t.Kind().String(), originalName, newName, err.Error())
}
}
renamedTypes[originalName] = newName
return newName
}