-
Notifications
You must be signed in to change notification settings - Fork 3
/
project.go
238 lines (222 loc) · 6.21 KB
/
project.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package mkdoc
import (
"fmt"
"github.com/thewinds/mkdoc/schema"
"golang.org/x/sync/errgroup"
"strings"
"sync"
)
type Project struct {
Config *Config
Scanners []DocScanner `yaml:"-"`
Generators []DocGenerator `yaml:"-"`
pluginArgs map[string]string
refObjects map[LangObjectId]*Object
defaultLoaderCfg *ObjectLoaderConfig
muObj sync.Mutex
}
func NewProject(config *Config) (*Project, error) {
project := &Project{Config: config, pluginArgs: make(map[string]string)}
if err := project.checkScanner(); err != nil {
return nil, err
}
if err := project.checkGenerator(); err != nil {
return nil, err
}
project.refObjects = make(map[LangObjectId]*Object)
project.defaultLoaderCfg = &ObjectLoaderConfig{*project.Config}
//if config.BaseType != "" {
// baseTypeObj := &Object{
// ID: config.BaseType,
// Type: &ObjectType{Name: "object"},
// }
// project.refObjects[baseTypeObj.ID] = baseTypeObj
//}
return project, nil
}
func (project *Project) checkScanner() error {
var okScanners []DocScanner
scanners := GetDocScanners()
if len(project.Config.Scanner) == 0 {
return fmt.Errorf("please use at least one scanner")
}
for _, name := range project.Config.Scanner {
if scanners[name] == nil {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("scanner \"%s\" is not found,you can choose scanner below :\n", name))
for n := range scanners {
sb.WriteString(fmt.Sprintf(" %s\n", n))
}
return fmt.Errorf(sb.String())
}
okScanners = append(okScanners, scanners[name])
}
project.Scanners = okScanners
return nil
}
func (project *Project) checkGenerator() error {
var okGenerators []DocGenerator
generators := GetGenerators()
if len(project.Config.Generator) == 0 {
return fmt.Errorf("please use at least one generator")
}
for _, name := range project.Config.Generator {
if generators[name] == nil {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("generator \"%s\" is not found,you can choose generator below :\n", name))
for n := range generators {
sb.WriteString(fmt.Sprintf(" %s\n", n))
}
return fmt.Errorf(sb.String())
}
okGenerators = append(okGenerators, generators[name])
}
project.Generators = okGenerators
return nil
}
func (project *Project) AddLangObject(id LangObjectId, value *Object) {
project.muObj.Lock()
defer project.muObj.Unlock()
if _, exist := project.refObjects[id]; !exist {
project.refObjects[id] = value
}
}
func (project *Project) GetLangObject(id LangObjectId) *Object {
project.muObj.Lock()
defer project.muObj.Unlock()
return project.refObjects[id]
}
func (project *Project) Objects() map[LangObjectId]*Object {
project.muObj.Lock()
defer project.muObj.Unlock()
return project.refObjects
}
func (project *Project) parseSchemaExtension(ext *schema.Extension) (Extension, error) {
switch ext.Name {
case "go_tag":
return new(ExtensionGoTag).Parse(ext)
default:
return new(ExtensionUnknown).Parse(ext)
}
}
func (project *Project) parseSchemaObject(object *schema.Object) (*Object, error) {
obj := Object{
ID: object.ID,
Type: (*ObjectType)(object.Type),
Fields: nil,
Extensions: nil,
Loaded: true,
}
for _, field := range object.Fields {
objField := &ObjectField{
Name: field.Name,
Desc: field.Desc,
Type: (*ObjectType)(field.Type),
}
for _, ext := range field.Extensions {
if ext != nil {
extParsed, err := project.parseSchemaExtension(ext)
if err != nil {
return nil, err
}
objField.Extensions = append(objField.Extensions, extParsed)
}
}
obj.Fields = append(obj.Fields, objField)
}
for _, ext := range object.Extensions {
extParsed, err := project.parseSchemaExtension(ext)
if err != nil {
return nil, err
}
obj.Extensions = append(obj.Extensions, extParsed)
}
return &obj, nil
}
func (project *Project) ParseSchemaAPI(api *schema.API) (*API, error) {
loader := GetObjectLoader(api.Language)
if loader == nil {
return nil, fmt.Errorf("object loader for language %s not found", api.Language)
}
loader.SetConfig(project.defaultLoaderCfg)
a := &API{
API: *api,
Mime: &MimeType{In: api.MimeIn, Out: api.MimeOut},
}
if len(a.Mime.In) == 0 {
a.Mime.In = project.Config.Mime.In
}
if len(a.Mime.Out) == 0 {
a.Mime.Out = project.Config.Mime.Out
}
if len(api.InType) > 0 {
objId, err := loader.GetObjectId(TypeScope{api.SourceFileName, api.InType})
if err != nil {
return nil, err
}
id := LangObjectId{Lang: api.Language, Id: objId}
a.InType = objId
a.InArgument = project.GetLangObject(id)
}
if len(api.OutType) > 0 {
objId, err := loader.GetObjectId(TypeScope{api.SourceFileName, api.OutType})
if err != nil {
return nil, err
}
id := LangObjectId{Lang: api.Language, Id: objId}
a.OutType = objId
a.OutArgument = project.GetLangObject(id)
}
return a, nil
}
func (project *Project) LoadObjects(schemaDef *schema.Schema) error {
// load object from schema object define
for _, object := range schemaDef.Objects {
id := LangObjectId{Lang: object.Language, Id: object.ID}
obj, err := project.parseSchemaObject(object)
if err != nil {
return err
}
project.AddLangObject(id, obj)
}
langTs := make(map[string][]TypeScope)
for _, api := range schemaDef.APIs {
if len(api.InType) > 0 {
ts := TypeScope{FileName: api.SourceFileName, TypeName: api.InType}
langTs[api.Language] = append(langTs[api.Language], ts)
}
if len(api.OutType) > 0 {
ts := TypeScope{FileName: api.SourceFileName, TypeName: api.OutType}
langTs[api.Language] = append(langTs[api.Language], ts)
}
}
for lang := range langTs {
if GetObjectLoader(lang) == nil {
return fmt.Errorf("object loader for language %s not found", lang)
}
}
eg := errgroup.Group{}
for lang, typeScopes := range langTs {
loader := GetObjectLoader(lang)
loader.SetConfig(project.defaultLoaderCfg)
for id, object := range project.Objects() {
if id.Lang == loader.Lang() {
if err := loader.Add(object); err != nil {
return err
}
}
}
eg.Go(func() error {
objs, err := loader.LoadAll(typeScopes)
if err != nil {
return err
}
for _, obj := range objs {
id := LangObjectId{Lang: lang, Id: obj.ID}
project.AddLangObject(id, obj)
}
return nil
})
}
return eg.Wait()
}