Debian Codegraph @

generate/type_string.go

raw symbols SCIP
1 // Copyright 2015 Aaron Jacobs. All Rights Reserved.
2 // Author: [email protected] (Aaron Jacobs)
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package generate
17
18 import (
19 "fmt"
20 "log"
21 "reflect"
22 "strings"
23 )
24
25 // Return the string that should be used to refer to the supplied type within
26 // the given package. The output is not guaranteed to be pretty, and should be
27 // run through a tool like gofmt afterward.
28 //
29 // For example, a pointer to an io.Reader may be rendered as "*Reader" or
30 // "*io.Reader" depending on whether the package path is "io" or not.
31 func typeString(
32 t reflect.Type,
33 pkgPath string) (s string) {
34 // Is this type named? If so we use its name, possibly with a package prefix.
35 //
36 // Examples:
37 //
38 // int
39 // string
40 // error
41 // gcs.Bucket
42 //
43 if t.Name() != "" {
44 if t.PkgPath() == pkgPath {
45 s = t.Name()
46 } else {
47 s = t.String()
48 }
49
50 return
51 }
52
53 // This type is unnamed. Recurse.
54 switch t.Kind() {
55 case reflect.Array:
56 s = fmt.Sprintf("[%d]%s", t.Len(), typeString(t.Elem(), pkgPath))
57
58 case reflect.Chan:
59 s = fmt.Sprintf("%s %s", t.ChanDir(), typeString(t.Elem(), pkgPath))
60
61 case reflect.Func:
62 s = typeString_Func(t, pkgPath)
63
64 case reflect.Interface:
65 s = typeString_Interface(t, pkgPath)
66
67 case reflect.Map:
68 s = fmt.Sprintf(
69 "map[%s]%s",
70 typeString(t.Key(), pkgPath),
71 typeString(t.Elem(), pkgPath))
72
73 case reflect.Ptr:
74 s = fmt.Sprintf("*%s", typeString(t.Elem(), pkgPath))
75
76 case reflect.Slice:
77 s = fmt.Sprintf("[]%s", typeString(t.Elem(), pkgPath))
78
79 case reflect.Struct:
80 s = typeString_Struct(t, pkgPath)
81
82 default:
83 log.Panicf("Unhandled kind %v for type: %v", t.Kind(), t)
84 }
85
86 return
87 }
88
89 func typeString_FuncOrMethod(
90 name string,
91 t reflect.Type,
92 pkgPath string) (s string) {
93 // Deal with input types.
94 var in []string
95 for i := 0; i < t.NumIn(); i++ {
96 in = append(in, typeString(t.In(i), pkgPath))
97 }
98
99 // And output types.
100 var out []string
101 for i := 0; i < t.NumOut(); i++ {
102 out = append(out, typeString(t.Out(i), pkgPath))
103 }
104
105 // Put it all together.
106 s = fmt.Sprintf(
107 "%s(%s) (%s)",
108 name,
109 strings.Join(in, ", "),
110 strings.Join(out, ", "))
111
112 return
113 }
114
115 func typeString_Func(
116 t reflect.Type,
117 pkgPath string) (s string) {
118 return typeString_FuncOrMethod("func", t, pkgPath)
119 }
120
121 func typeString_Struct(
122 t reflect.Type,
123 pkgPath string) (s string) {
124 var fields []string
125 for i := 0; i < t.NumField(); i++ {
126 f := t.Field(i)
127 fString := fmt.Sprintf("%s %s", f.Name, typeString(f.Type, pkgPath))
128 fields = append(fields, fString)
129 }
130
131 s = fmt.Sprintf("struct { %s }", strings.Join(fields, "; "))
132 return
133 }
134
135 func typeString_Interface(
136 t reflect.Type,
137 pkgPath string) (s string) {
138 var methods []string
139 for i := 0; i < t.NumMethod(); i++ {
140 m := t.Method(i)
141 mString := typeString_FuncOrMethod(m.Name, m.Type, pkgPath)
142 methods = append(methods, mString)
143 }
144
145 s = fmt.Sprintf("interface { %s }", strings.Join(methods, "; "))
146 return
147 }