/
model.go
217 lines (191 loc) · 5.6 KB
/
model.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2023 Vedran Vuk. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This file contains bast object model types.
package bast
// Declaration represents a top level declaration in a Go file.
type Declaration interface {
// GetName returns the Declaration name.
GetName() string
}
// Package contians info about a Go package.
type Package struct {
// Name is the package name, without path.
Name string
// Files is a list of files in the package.
Files map[string]*File
}
// File contians info about a Go source file.
type File struct {
// Comments are the file comments, grouped by separation, including docs.
Comments [][]string
// Doc is the file doc comment.
Doc []string
// Name is the File name, without path.
Name string
// Imports is a list of file imports.
Imports map[string]*Import
// Declarations is a list of top level declarations in the file.
Declarations map[string]Declaration
}
// Import contians info about an import.
type Import struct {
// Comment is the import comment.
Comment []string
// Doc is the import doc.
Doc []string
// Name is the import name, possibly empty, "." or some custom name.
Name string
// Path is the import path.
Path string
}
// Func contains info about a function.
type Func struct {
// Comment is the func comment.
Comment []string
// Doc is the func doc comment.
Doc []string
// Name is the func name.
Name string
// TypeParams are type parameters.
TypeParams map[string]*Field
// Params is a list of func arguments.
Params map[string]*Field
// Results is a list of func returns.
Results map[string]*Field
}
// Method contains info about a method.
type Method struct {
// Func embeds all Func properties.
Func
// Receiver is the method receiver.
Receivers map[string]*Field
}
// Const contains info about a constant.
type Const struct {
// Comment is the const comment.
Comment []string
// Doc is the const doc comment.
Doc []string
// Name is the constant name.
Name string
// Type is the const type, empty if undpecified.
Type string
// Value is the const value, empty if undpecified.
Value string
}
// Var contains info about a variable.
type Var struct {
// Comment is the const comment.
Comment []string
// Doc is the const doc comment.
Doc []string
// Name is the constant name.
Name string
// Type is the const type, empty if undpecified.
Type string
// Value is the const value, empty if undpecified.
Value string
}
// Type contains info about a type.
type Type struct {
// Comment is the struct comment.
Comment []string
// Doc is the struct doc comment.
Doc []string
// Name is the struct name.
Name string
// Type is Type's underlying type.
Type string
// IsAlias is true if type is an alias of the type it derives from.
IsAlias bool
}
// Interface contains info about an interface.
type Interface struct {
// Comment is the interface comment.
Comment []string
// Doc is the interface doc comment.
Doc []string
// Name is the interface name.
Name string
// Methods is a list of methods defined by the interface.
Methods map[string]*Method
}
// Struct contains info about a struct.
type Struct struct {
// Comment is the struct comment.
Comment []string
// Doc is the struct doc comment.
Doc []string
// Name is the struct name.
Name string
// Fields is a list of struct fields.
Fields map[string]*Field
}
// Field contains info about a struct field, method receiver, or method or func
// type params, params or results.
type Field struct {
// Comment is the field comment.
Comment []string
// Doc is the field doc comment.
Doc []string
// Name is the field name.
Name string
// Type is the field type.
Type string
// Tag is the field raw tag string.
Tag string
}
// NewPackage returns a new *Package.
func NewPackage() *Package {
return &Package{
Files: make(map[string]*File),
}
}
// NewFile returns a new *File.
func NewFile() *File {
return &File{
Imports: make(map[string]*Import),
Declarations: make(map[string]Declaration),
}
}
// NewImport returns a new *Import.
func NewImport() *Import { return &Import{} }
// NewFunc returns a new *Func.
func NewFunc() *Func {
return &Func{
TypeParams: make(map[string]*Field),
Params: make(map[string]*Field),
Results: make(map[string]*Field),
}
}
// NewMethod returns a new *Method.
func NewMethod() *Method {
return &Method{
Func: *NewFunc(),
Receivers: map[string]*Field{},
}
}
// NewConst returns a new *Const.
func NewConst() *Const { return &Const{} }
// NewVar returns a new *Var.
func NewVar() *Var { return &Var{} }
// NewType returns a new *Type.
func NewType() *Type { return &Type{} }
// NewInterface returns a new *Interface.
func NewInterface() *Interface { return &Interface{Methods: make(map[string]*Method)} }
// NewStruct returns a new *Struct.
func NewStruct() *Struct { return &Struct{Fields: make(map[string]*Field)} }
// NewField returns a new *Field.
func NewField() *Field { return &Field{} }
func (self *Package) GetName() string { return self.Name }
func (self *File) GetName() string { return self.Name }
func (self *Import) GetName() string { return self.Name }
func (self *Func) GetName() string { return self.Name }
func (self *Method) GetName() string { return self.Name }
func (self *Var) GetName() string { return self.Name }
func (self *Const) GetName() string { return self.Name }
func (self *Type) GetName() string { return self.Name }
func (self *Interface) GetName() string { return self.Name }
func (self *Struct) GetName() string { return self.Name }
func (self *Field) GetName() string { return self.Name }