Skip to content

Commit 04a218e

Browse files
committed
code refactor
1 parent 986a982 commit 04a218e

File tree

2 files changed

+128
-140
lines changed

2 files changed

+128
-140
lines changed

gs/internal/gs_bean/bean.go

Lines changed: 127 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
// Package gs_bean provides the bean definition for the Go-Spring framework.
1718
package gs_bean
1819

1920
import (
@@ -24,9 +25,6 @@ import (
2425
"github.com/go-spring/spring-core/util"
2526
)
2627

27-
// refreshableType is the [reflect.Type] of the interface [gs.Refreshable].
28-
var refreshableType = reflect.TypeFor[gs.Refreshable]()
29-
3028
// BeanStatus represents the different lifecycle statuses of a bean.
3129
type BeanStatus int8
3230

@@ -62,6 +60,9 @@ func (status BeanStatus) String() string {
6260
}
6361
}
6462

63+
// refreshableType is the [reflect.Type] of the interface [gs.Refreshable].
64+
var refreshableType = reflect.TypeFor[gs.Refreshable]()
65+
6566
// BeanMetadata holds the metadata information of a bean.
6667
type BeanMetadata struct {
6768
f gs.Callable // Callable for the bean (ctor or method).
@@ -82,26 +83,94 @@ type BeanMetadata struct {
8283
refreshTag string // Refresh tag for the bean.
8384
}
8485

86+
// validLifeCycleFunc checks whether the provided function is a valid lifecycle function.
87+
func validLifeCycleFunc(fnType reflect.Type, beanType reflect.Type) bool {
88+
if !util.IsFuncType(fnType) || fnType.NumIn() != 1 {
89+
return false
90+
}
91+
if t := fnType.In(0); t.Kind() == reflect.Interface {
92+
if !beanType.Implements(t) {
93+
return false
94+
}
95+
} else if t != beanType {
96+
return false
97+
}
98+
return util.ReturnNothing(fnType) || util.ReturnOnlyError(fnType)
99+
}
100+
85101
// Init returns the initialization function of the bean.
86102
func (d *BeanMetadata) Init() gs.BeanInitFunc {
87103
return d.init
88104
}
89105

106+
// SetInit sets the initialization function for the bean.
107+
func (d *BeanDefinition) SetInit(fn gs.BeanInitFunc) {
108+
if validLifeCycleFunc(reflect.TypeOf(fn), d.Type()) {
109+
d.init = fn
110+
return
111+
}
112+
panic("init should be func(bean) or func(bean)error")
113+
}
114+
90115
// Destroy returns the destruction function of the bean.
91116
func (d *BeanMetadata) Destroy() gs.BeanDestroyFunc {
92117
return d.destroy
93118
}
94119

120+
// SetDestroy sets the destruction function for the bean.
121+
func (d *BeanDefinition) SetDestroy(fn gs.BeanDestroyFunc) {
122+
if validLifeCycleFunc(reflect.TypeOf(fn), d.Type()) {
123+
d.destroy = fn
124+
return
125+
}
126+
panic("destroy should be func(bean) or func(bean)error")
127+
}
128+
95129
// DependsOn returns the list of dependencies for the bean.
96130
func (d *BeanMetadata) DependsOn() []gs.BeanSelectorInterface {
97131
return d.dependsOn
98132
}
99133

134+
// SetDependsOn sets the list of dependencies for the bean.
135+
func (d *BeanDefinition) SetDependsOn(selectors ...gs.BeanSelectorInterface) {
136+
d.dependsOn = append(d.dependsOn, selectors...)
137+
}
138+
100139
// Exports returns the list of exported types for the bean.
101140
func (d *BeanMetadata) Exports() []reflect.Type {
102141
return d.exports
103142
}
104143

144+
// SetExport sets the exported interfaces for the bean.
145+
func (d *BeanDefinition) SetExport(exports ...reflect.Type) {
146+
for _, t := range exports {
147+
if t.Kind() != reflect.Interface {
148+
panic("only interface type can be exported")
149+
}
150+
exported := false
151+
for _, export := range d.exports {
152+
if t == export {
153+
exported = true
154+
break
155+
}
156+
}
157+
if exported {
158+
continue
159+
}
160+
d.exports = append(d.exports, t)
161+
}
162+
}
163+
164+
// Conditions returns the list of conditions for the bean.
165+
func (d *BeanMetadata) Conditions() []gs.Condition {
166+
return d.conditions
167+
}
168+
169+
// SetCondition adds a condition to the list of conditions for the bean.
170+
func (d *BeanDefinition) SetCondition(conditions ...gs.Condition) {
171+
d.conditions = append(d.conditions, conditions...)
172+
}
173+
105174
// ConfigurationBean returns whether the bean is a configuration bean.
106175
func (d *BeanMetadata) ConfigurationBean() bool {
107176
return d.configurationBean
@@ -112,6 +181,21 @@ func (d *BeanMetadata) ConfigurationParam() gs.ConfigurationParam {
112181
return d.configurationParam
113182
}
114183

184+
// SetConfiguration sets the configuration flag and parameters for the bean.
185+
func (d *BeanDefinition) SetConfiguration(param ...gs.ConfigurationParam) {
186+
d.configurationBean = true
187+
if len(param) == 0 {
188+
return
189+
}
190+
x := param[0]
191+
if len(x.Includes) > 0 {
192+
d.configurationParam.Includes = x.Includes
193+
}
194+
if len(x.Excludes) > 0 {
195+
d.configurationParam.Excludes = x.Excludes
196+
}
197+
}
198+
115199
// Refreshable returns whether the bean is refreshable.
116200
func (d *BeanMetadata) Refreshable() bool {
117201
return d.refreshable
@@ -122,27 +206,23 @@ func (d *BeanMetadata) RefreshTag() string {
122206
return d.refreshTag
123207
}
124208

125-
// Conditions returns the list of conditions for the bean.
126-
func (d *BeanMetadata) Conditions() []gs.Condition {
127-
return d.conditions
128-
}
129-
130-
// File returns the file where the bean is defined.
131-
func (d *BeanMetadata) File() string {
132-
return d.file
209+
// SetRefreshable sets the refreshable flag and tag for the bean.
210+
func (d *BeanDefinition) SetRefreshable(tag string) {
211+
if !d.Type().Implements(refreshableType) {
212+
panic("must implement gs.Refreshable interface")
213+
}
214+
d.refreshable = true
215+
d.refreshTag = tag
133216
}
134217

135-
// Line returns the line number where the bean is defined.
136-
func (d *BeanMetadata) Line() int {
137-
return d.line
218+
// FileLine returns the file and line number for the bean.
219+
func (d *BeanMetadata) FileLine() (string, int) {
220+
return d.file, d.line
138221
}
139222

140-
// Class returns the class type of the bean.
141-
func (d *BeanMetadata) Class() string {
142-
if d.f == nil {
143-
return "object bean"
144-
}
145-
return "constructor bean"
223+
// SetFileLine sets the file and line number for the bean.
224+
func (d *BeanDefinition) SetFileLine(file string, line int) {
225+
d.file, d.line = file, line
146226
}
147227

148228
// BeanRuntime holds runtime information about the bean.
@@ -172,16 +252,16 @@ func (d *BeanRuntime) Interface() interface{} {
172252
return d.v.Interface()
173253
}
174254

175-
// Status returns the current status of the bean.
176-
func (d *BeanRuntime) Status() BeanStatus {
177-
return StatusWired
178-
}
179-
180255
// Callable returns the callable for the bean.
181256
func (d *BeanRuntime) Callable() gs.Callable {
182257
return nil
183258
}
184259

260+
// Status returns the current status of the bean.
261+
func (d *BeanRuntime) Status() BeanStatus {
262+
return StatusWired
263+
}
264+
185265
// String returns a string representation of the bean.
186266
func (d *BeanRuntime) String() string {
187267
return d.name
@@ -193,16 +273,31 @@ type BeanDefinition struct {
193273
*BeanRuntime
194274
}
195275

196-
// TypeAndName returns the type and name of the bean.
197-
func (d *BeanDefinition) TypeAndName() (reflect.Type, string) {
198-
return d.Type(), d.Name()
276+
// NewBean creates a new bean definition.
277+
func NewBean(t reflect.Type, v reflect.Value, f gs.Callable, name string) *BeanDefinition {
278+
return &BeanDefinition{
279+
BeanMetadata: &BeanMetadata{
280+
f: f,
281+
status: StatusDefault,
282+
},
283+
BeanRuntime: &BeanRuntime{
284+
t: t,
285+
v: v,
286+
name: name,
287+
},
288+
}
199289
}
200290

201291
// Callable returns the callable for the bean.
202292
func (d *BeanDefinition) Callable() gs.Callable {
203293
return d.f
204294
}
205295

296+
// SetName sets the name of the bean.
297+
func (d *BeanDefinition) SetName(name string) {
298+
d.name = name
299+
}
300+
206301
// Status returns the current status of the bean.
207302
func (d *BeanDefinition) Status() BeanStatus {
208303
return d.status
@@ -213,119 +308,12 @@ func (d *BeanMetadata) SetStatus(status BeanStatus) {
213308
d.status = status
214309
}
215310

216-
// SetFileLine sets the file and line number for the bean.
217-
func (d *BeanDefinition) SetFileLine(file string, line int) {
218-
d.file, d.line = file, line
219-
}
220-
221-
// SetName sets the name of the bean.
222-
func (d *BeanDefinition) SetName(name string) {
223-
d.name = name
224-
}
225-
226-
// validLifeCycleFunc checks whether the provided function is a valid lifecycle function.
227-
func validLifeCycleFunc(fnType reflect.Type, beanType reflect.Type) bool {
228-
if !util.IsFuncType(fnType) || fnType.NumIn() != 1 {
229-
return false
230-
}
231-
if t := fnType.In(0); t.Kind() == reflect.Interface {
232-
if !beanType.Implements(t) {
233-
return false
234-
}
235-
} else if t != beanType {
236-
return false
237-
}
238-
return util.ReturnNothing(fnType) || util.ReturnOnlyError(fnType)
239-
}
240-
241-
// SetInit sets the initialization function for the bean.
242-
func (d *BeanDefinition) SetInit(fn gs.BeanInitFunc) {
243-
if validLifeCycleFunc(reflect.TypeOf(fn), d.Type()) {
244-
d.init = fn
245-
return
246-
}
247-
panic("init should be func(bean) or func(bean)error")
248-
}
249-
250-
// SetDestroy sets the destruction function for the bean.
251-
func (d *BeanDefinition) SetDestroy(fn gs.BeanDestroyFunc) {
252-
if validLifeCycleFunc(reflect.TypeOf(fn), d.Type()) {
253-
d.destroy = fn
254-
return
255-
}
256-
panic("destroy should be func(bean) or func(bean)error")
257-
}
258-
259-
// SetCondition adds a condition to the list of conditions for the bean.
260-
func (d *BeanDefinition) SetCondition(conditions ...gs.Condition) {
261-
d.conditions = append(d.conditions, conditions...)
262-
}
263-
264-
// SetDependsOn sets the list of dependencies for the bean.
265-
func (d *BeanDefinition) SetDependsOn(selectors ...gs.BeanSelectorInterface) {
266-
d.dependsOn = append(d.dependsOn, selectors...)
267-
}
268-
269-
// SetExport sets the exported interfaces for the bean.
270-
func (d *BeanDefinition) SetExport(exports ...reflect.Type) {
271-
for _, t := range exports {
272-
if t.Kind() != reflect.Interface {
273-
panic("only interface type can be exported")
274-
}
275-
exported := false
276-
for _, export := range d.exports {
277-
if t == export {
278-
exported = true
279-
break
280-
}
281-
}
282-
if exported {
283-
continue
284-
}
285-
d.exports = append(d.exports, t)
286-
}
287-
}
288-
289-
// SetConfiguration sets the configuration flag and parameters for the bean.
290-
func (d *BeanDefinition) SetConfiguration(param ...gs.ConfigurationParam) {
291-
d.configurationBean = true
292-
if len(param) == 0 {
293-
return
294-
}
295-
x := param[0]
296-
if len(x.Includes) > 0 {
297-
d.configurationParam.Includes = x.Includes
298-
}
299-
if len(x.Excludes) > 0 {
300-
d.configurationParam.Excludes = x.Excludes
301-
}
302-
}
303-
304-
// SetRefreshable sets the refreshable flag and tag for the bean.
305-
func (d *BeanDefinition) SetRefreshable(tag string) {
306-
if !d.Type().Implements(refreshableType) {
307-
panic("must implement gs.Refreshable interface")
308-
}
309-
d.refreshable = true
310-
d.refreshTag = tag
311+
// TypeAndName returns the type and name of the bean.
312+
func (d *BeanDefinition) TypeAndName() (reflect.Type, string) {
313+
return d.Type(), d.Name()
311314
}
312315

313316
// String returns a string representation of the bean.
314317
func (d *BeanDefinition) String() string {
315-
return fmt.Sprintf("%s name:%q %s:%d", d.Class(), d.name, d.file, d.line)
316-
}
317-
318-
// NewBean creates a new bean definition.
319-
func NewBean(t reflect.Type, v reflect.Value, f gs.Callable, name string) *BeanDefinition {
320-
return &BeanDefinition{
321-
BeanMetadata: &BeanMetadata{
322-
f: f,
323-
status: StatusDefault,
324-
},
325-
BeanRuntime: &BeanRuntime{
326-
t: t,
327-
v: v,
328-
name: name,
329-
},
330-
}
318+
return fmt.Sprintf("name:%q %s:%d", d.name, d.file, d.line)
331319
}

gs/internal/gs_core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ func (c *resolvingStage) scanConfiguration(bd *gs_bean.BeanDefinition) ([]*gs_be
514514
name := bd.Name() + "_" + m.Name
515515
b := gs_bean.NewBean(v.Type(), v, f, name)
516516
b.SetFileLine(file, line)
517-
gs.NewBeanDefinition(b).Condition(gs_cond.OnBean(bd))
517+
b.SetCondition(gs_cond.OnBean(bd))
518518
newBeans = append(newBeans, b)
519519
}
520520
break

0 commit comments

Comments
 (0)