-
Notifications
You must be signed in to change notification settings - Fork 4
/
planner.go
172 lines (151 loc) · 4.91 KB
/
planner.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
package genus
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/pkg/errors"
"github.com/yangyuqian/genus/generator/orm"
"github.com/yangyuqian/genus/types"
)
type Planner interface{}
func NewPackagePlanner(specPath string) (planner *PackagePlanner, err error) {
raw, err := ioutil.ReadFile(specPath)
if err != nil {
return nil, err
}
return &PackagePlanner{RawSpec: raw}, nil
}
// Planner for generations for single Go package
// Load plan definitions from a json specification
type PackagePlanner struct {
RawSpec []byte
Spec *Spec
Plans []Plan
}
func (pl *PackagePlanner) Perform() (err error) {
for _, plan := range pl.Plans {
if err = plan.Render(); err != nil {
return err
}
}
return
}
func (pl *PackagePlanner) Plan() (err error) {
if pl.Spec == nil {
pl.Spec = &Spec{}
if err = json.Unmarshal(pl.RawSpec, pl.Spec); err != nil {
return err
}
}
for _, planItem := range pl.Spec.PlanItems {
switch planItem.PlanType {
case types.SINGLETON.String():
plan := &SingletonPlan{
Suffix: StringWithDefault(planItem.Suffix, pl.Spec.Suffix),
TemplateDir: StringWithDefault(planItem.TemplateDir, pl.Spec.TemplateDir),
BaseDir: StringWithDefault(planItem.BaseDir, pl.Spec.BaseDir),
BasePackage: StringWithDefault(planItem.BasePackage, pl.Spec.BasePackage),
SkipExists: BoolWithDefault(planItem.SkipExists, pl.Spec.SkipExists),
SkipFormat: BoolWithDefault(planItem.SkipFormat, pl.Spec.SkipFormat),
SkipFixImports: BoolWithDefault(planItem.SkipFixImports, pl.Spec.SkipFixImports),
Merge: BoolWithDefault(planItem.Merge, pl.Spec.Merge),
RelativePackage: planItem.RelativePackage,
TemplateNames: planItem.TemplateNames,
Filename: planItem.Filename,
Package: planItem.Package,
Imports: planItem.Imports,
}
// Global data
if plan.Data == nil && len(planItem.Data) <= 0 && pl.Spec.Data != nil {
planItem.Data = pl.Spec.Data
}
// plan item data
if len(planItem.Data) > 0 && plan.Data == nil {
plan.Data = planItem.Data[0]
} else if ext := pl.Spec.Extension; ext != nil {
// extension data
// For empty data, try to build data from extension metadata
data, err := BuildSingletonData(ext.Framework, ext.Data)
if err != nil {
return err
}
if len(data) > 0 {
plan.Data = data[0]
}
}
pl.Plans = append(pl.Plans, plan)
case types.REPEATABLE.String():
plan := &RepeatablePlan{
SingletonPlan: SingletonPlan{
Suffix: StringWithDefault(planItem.Suffix, pl.Spec.Suffix),
TemplateDir: StringWithDefault(planItem.TemplateDir, pl.Spec.TemplateDir),
BaseDir: StringWithDefault(planItem.BaseDir, pl.Spec.BaseDir),
BasePackage: StringWithDefault(planItem.BasePackage, pl.Spec.BasePackage),
SkipExists: BoolWithDefault(planItem.SkipExists, pl.Spec.SkipExists),
SkipFormat: BoolWithDefault(planItem.SkipFormat, pl.Spec.SkipFormat),
SkipFixImports: BoolWithDefault(planItem.SkipFixImports, pl.Spec.SkipFixImports),
Merge: BoolWithDefault(planItem.Merge, pl.Spec.Merge),
RelativePackage: planItem.RelativePackage,
Filename: planItem.Filename,
Package: planItem.Package,
TemplateNames: planItem.TemplateNames,
Imports: planItem.Imports,
},
}
// Global data
if plan.Data == nil && len(planItem.Data) <= 0 && pl.Spec.Data != nil {
planItem.Data = pl.Spec.Data
}
// plan item data
if len(planItem.Data) > 0 && plan.Data == nil {
plan.Data = planItem.Data
}
// extension data
if ext := pl.Spec.Extension; ext != nil && len(planItem.Data) <= 0 {
data, err := BuildRepeatableData(ext.Framework, ext.Data)
if err != nil {
return err
}
plan.Data = data
}
pl.Plans = append(pl.Plans, plan)
default:
return errors.New(fmt.Sprintf("Invalid plan type %s, PlanType must be either SINGLETON or REPEATABLE"))
}
}
return
}
func BuildSingletonData(framework string, data json.RawMessage) (o []interface{}, err error) {
switch framework {
case "sqlboiler":
opts := orm.DataOpts{Framework: framework}
if err = json.Unmarshal(data, &opts); err != nil {
return nil, err
}
opts.Driver, err = orm.DeadDriverByName(opts.DriverName)
if err != nil {
return nil, err
}
return orm.BuildSingletonData(opts)
default:
return nil, errors.Errorf("Framework <%s> is not supported", framework)
}
return
}
func BuildRepeatableData(framework string, data json.RawMessage) (o []interface{}, err error) {
switch framework {
case "sqlboiler":
opts := orm.DataOpts{}
if err = json.Unmarshal(data, &opts); err != nil {
return nil, err
}
opts.Driver, err = orm.DeadDriverByName(opts.DriverName)
if err != nil {
return nil, err
}
return orm.BuildRepeatableData(opts)
default:
return nil, errors.Errorf("Framework <%s> is not supported", framework)
}
return
}