-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodels.go
145 lines (121 loc) · 2.83 KB
/
models.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package ifacecodegen
import (
"fmt"
"strings"
)
// Type is a Google-golang type definition that can be rendered into a valid
// Google-golang code snippet.
type Type interface {
String() string
}
// TypeBuiltin is a built in Google-golang type such as "string" or "bool".
type TypeBuiltin string
func (t TypeBuiltin) String() string { return string(t) }
// TypeExported is a user defined type that is exported from a package.
type TypeExported struct {
Package string
Type Type
}
func (t *TypeExported) String() string {
return fmt.Sprintf("%s.%s", t.Package, t.Type.String())
}
// TypeArray is a slice or array type.
type TypeArray struct {
Len int
Type Type
}
func (t *TypeArray) String() string {
var result = "[]"
if t.Len > -1 {
result = fmt.Sprintf("[%d]", t.Len)
}
return result + t.Type.String()
}
// TypeChan is a channel type.
type TypeChan struct {
ReadOnly bool
WriteOnly bool
Type Type
}
func (t *TypeChan) String() string {
var result = "chan "
if t.ReadOnly {
result = "<-chan "
}
if t.WriteOnly {
result = "chan<- "
}
return result + t.Type.String()
}
// TypeVariadic is any type that is prefixed by Ellipsis.
type TypeVariadic struct {
Type Type
}
func (t *TypeVariadic) String() string {
return "..." + t.Type.String()
}
// TypeFunc is an input type of a function.
type TypeFunc struct {
In []Type
Out []Type
}
func (t *TypeFunc) String() string {
var outParams []string
var inParams []string
for _, param := range t.In {
inParams = append(inParams, param.String())
}
for _, param := range t.Out {
outParams = append(outParams, param.String())
}
var outString = strings.Join(outParams, ", ")
if len(outParams) <= 1 {
outString = " " + outString
}
if len(outParams) > 1 {
outString = " (" + outString + ")"
}
var inString = strings.Join(inParams, ", ")
return strings.TrimSpace("func(" + inString + ")" + outString)
}
// TypeMap is a user defined map type.
type TypeMap struct {
Key Type
Value Type
}
func (t *TypeMap) String() string {
return fmt.Sprintf("map[%s]%s", t.Key.String(), t.Value.String())
}
// TypePointer is a pointer to another type.
type TypePointer struct {
Type Type
}
func (t *TypePointer) String() string {
return "*" + t.Type.String()
}
// Package is a container for all exported interfaces of a Google-golang package.
type Package struct {
Name string
Interfaces []*Interface
}
// Import is a package name and path that is imported by another package.
type Import struct {
Package string
Path string
}
// Interface is an exported interface defined in a package.
type Interface struct {
Name string
Methods []*Method
}
// Method is a named function attached to an interface.
type Method struct {
Name string
In []*Parameter
Out []*Parameter
}
// Parameter is a named parameter used by a Method.
type Parameter struct {
Name string
Type Type
}