/
generator.go
executable file
·152 lines (131 loc) · 3.58 KB
/
generator.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
package protocol
import (
"github.com/nomos/go-lokas/log"
"github.com/nomos/go-lokas/util"
)
type GenType int
const (
GEN_UNDEFINED GenType = iota
GEN_GO
GEN_TS
GEN_JSON
GEN_CS
)
type GeneratorOption struct {
SourcePath string
DestPath string
}
type Generator struct {
logger log.ILogger
CsCamelCase bool
Models map[string]GeneratorFile
GoModels map[string]GeneratorFile
Protos map[string]GeneratorFile
GoIds *GoIdsFile
ProtoIds *ProtoIdsFile
Conf *ConfFile
GoStructObjects []*GoStructObject
GoEnumObjects []*GoEnumObject
ProtoMsgObjects []*ProtoMsgObject
ModelClassObjects []*ModelClassObject
ModelEnumObjects []*ModelEnumObject
ModelIdsObjects map[uint16]*ModelId
ModelErrorObjects map[int]*ModelError
ModelPackages map[string]*ModelPackageObject
TsGenerateFilePaths []string
CsImpl bool
TsModels []*TsModelFile
TsIds *TsIdsFile
TsEnums *TsEnumFile
TsClassObjects []*TsClassObject
TsEnumObjects []*TsEnumObject
//Schemas []*ModelClassObject
Individual bool
GoPath string
TsPath string
ProtoPath string
ModelPaths []string
CsPath string
Proto2GoCmdLinExec func(pack, protoPath, GoPath string) error
Proto2TsCmdLinExec func(pack, protoPath, GoPath string) error
}
func NewGenerator() *Generator {
ret := &Generator{}
ret.Clear()
return ret
}
func (this *Generator) SetLogger(logger log.ILogger) {
this.logger = logger
}
func (this *Generator) GetLogger() log.ILogger {
if util.IsNil(this.logger) {
return log.DefaultLogger()
}
return this.logger
}
func (this *Generator) SetProto2GoCmdLine(f func(pack, protoPath, GoPath string) error) {
this.Proto2GoCmdLinExec = f
}
func (this *Generator) SetProto2TsCmdLine(f func(pack, protoPath, TsPath string) error) {
this.Proto2TsCmdLinExec = f
}
func (this *Generator) GetErrorName(id int) string {
e, ok := this.ModelErrorObjects[id]
if ok {
return e.ErrorName
}
return ""
}
func (this *Generator) IsErrorName(s string) bool {
for _, v := range this.ModelErrorObjects {
if v.ErrorName == s {
return true
}
}
return false
}
func (this *Generator) Clear() {
this.GetLogger().Warn("Generator Clear")
this.Models = make(map[string]GeneratorFile)
this.GoModels = make(map[string]GeneratorFile)
this.Protos = make(map[string]GeneratorFile)
this.GoStructObjects = make([]*GoStructObject, 0)
this.GoEnumObjects = make([]*GoEnumObject, 0)
this.TsModels = make([]*TsModelFile, 0)
this.TsClassObjects = make([]*TsClassObject, 0)
this.TsEnumObjects = make([]*TsEnumObject, 0)
//this.Schemas = make([]*ModelClassObject, 0)
this.ProtoMsgObjects = make([]*ProtoMsgObject, 0)
this.ModelClassObjects = make([]*ModelClassObject, 0)
this.ModelEnumObjects = make([]*ModelEnumObject, 0)
this.ModelIdsObjects = make(map[uint16]*ModelId)
this.ModelErrorObjects = make(map[int]*ModelError)
this.ModelPackages = make(map[string]*ModelPackageObject)
this.TsGenerateFilePaths = []string{}
}
func (this *Generator) GetModelByName(s string) *ModelClassObject {
for _, v := range this.ModelClassObjects {
if v.ClassName == s {
return v
}
}
return nil
}
func (this *Generator) GetEnumByName(s string) *ModelEnumObject {
for _, v := range this.ModelEnumObjects {
if v.EnumName == s {
return v
}
}
return nil
}
func (this *Generator) SetOption(opt GeneratorOption) {
}
func (this *Generator) IsEnum(s string) bool {
for _, v := range this.ModelEnumObjects {
if v.EnumName == s {
return true
}
}
return false
}