Skip to content

Commit 922aed3

Browse files
lvan100lianghuan
authored andcommitted
feat(core): add RootBean & created on demand
1 parent d740aad commit 922aed3

File tree

12 files changed

+51
-13
lines changed

12 files changed

+51
-13
lines changed

conf/reader/prop/prop.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ func Read(b []byte) (map[string]any, error) {
2323

2424
p := properties.NewProperties()
2525
p.DisableExpansion = true
26-
_ = p.Load(b, properties.UTF8) // always no error
26+
if err := p.Load(b, properties.UTF8); err != nil {
27+
return nil, err
28+
}
2729

2830
ret := make(map[string]any)
2931
for k, v := range p.Map() {

conf/reader/prop/prop_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ import (
2424

2525
func TestRead(t *testing.T) {
2626

27+
t.Run("error", func(t *testing.T) {
28+
_, err := Read([]byte(`=1`))
29+
assert.ThatError(t, err).Matches(`properties: Line 1: "1"`)
30+
})
31+
2732
t.Run("basic type", func(t *testing.T) {
2833
r, err := Read([]byte(`
2934
empty=

gs/gs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ func Component[T any](i T) T {
280280
return i
281281
}
282282

283+
// RootBean registers a root bean definition.
284+
func RootBean(b *RegisteredBean) {
285+
gs_app.GS.C.RootBean(b)
286+
}
287+
283288
// Object registers a bean definition for a given object.
284289
func Object(i any) *RegisteredBean {
285290
b := gs_bean.NewBean(reflect.ValueOf(i))

gs/internal/gs_app/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (app *App) RunWith(fn func(ctx context.Context) error) error {
108108
// loading, IoC container refreshing, dependency injection, and runs
109109
// runners, jobs and servers.
110110
func (app *App) Start() error {
111-
app.C.Object(app)
111+
app.C.RootBean(app.C.Object(app))
112112

113113
// loads the layered app properties
114114
var p conf.Properties

gs/internal/gs_app/app_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ func TestApp(t *testing.T) {
9191
t.Cleanup(Reset)
9292

9393
app := NewApp()
94-
app.C.Provide(func() (*http.Server, error) {
94+
app.C.RootBean(app.C.Provide(func() (*http.Server, error) {
9595
return nil, errors.New("fail to create bean")
96-
})
96+
}))
9797
err := app.Run()
9898
assert.ThatError(t, err).Matches("fail to create bean")
9999
})

gs/internal/gs_app/boot.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func (b *BootImpl) Config() *gs_conf.BootConfig {
7171
return b.p
7272
}
7373

74+
// RootBean registers a root bean definition.
75+
func (b *BootImpl) RootBean(x *gs.RegisteredBean) {
76+
b.c.RootBean(x)
77+
}
78+
7479
// Object registers an object bean.
7580
func (b *BootImpl) Object(i any) *gs.RegisteredBean {
7681
b.flag = true
@@ -103,7 +108,7 @@ func (b *BootImpl) Run() error {
103108
if !b.flag {
104109
return nil
105110
}
106-
b.c.Object(b)
111+
b.c.RootBean(b.c.Object(b))
107112

108113
var p conf.Properties
109114

gs/internal/gs_app/boot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func TestBoot(t *testing.T) {
6060
t.Cleanup(Reset)
6161

6262
boot := NewBoot().(*BootImpl)
63-
boot.Provide(func() (*bytes.Buffer, error) {
63+
boot.RootBean(boot.Provide(func() (*bytes.Buffer, error) {
6464
return nil, errors.New("fail to create bean")
65-
})
65+
}))
6666
err := boot.Run()
6767
assert.ThatError(t, err).Matches("fail to create bean")
6868
})

gs/internal/gs_core/core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (c *Container) Refresh(p conf.Properties) error {
4343
return err
4444
}
4545
c.Injecting = injecting.New(p)
46-
if err := c.Injecting.Refresh(c.Beans()); err != nil {
46+
if err := c.Injecting.Refresh(c.Roots(), c.Beans()); err != nil {
4747
return err
4848
}
4949
c.Resolving = nil

gs/internal/gs_core/core_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestContainer(t *testing.T) {
5050

5151
t.Run("inject error", func(t *testing.T) {
5252
c := New()
53-
c.Provide(func(addr string) *http.Server { return nil })
53+
c.RootBean(c.Provide(func(addr string) *http.Server { return nil }))
5454
err := c.Refresh(conf.New())
5555
assert.ThatError(t, err).Matches("parse tag .* error: invalid syntax")
5656
})

gs/internal/gs_core/injecting/injecting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c *Injecting) RefreshProperties(p conf.Properties) error {
8080
}
8181

8282
// Refresh loads and wires all provided bean definitions.
83-
func (c *Injecting) Refresh(beans []*gs_bean.BeanDefinition) (err error) {
83+
func (c *Injecting) Refresh(roots, beans []*gs_bean.BeanDefinition) (err error) {
8484
allowCircularReferences := cast.ToBool(c.p.Data().Get("spring.allow-circular-references"))
8585
forceAutowireIsNullable := cast.ToBool(c.p.Data().Get("spring.force-autowire-is-nullable"))
8686

@@ -113,7 +113,7 @@ func (c *Injecting) Refresh(beans []*gs_bean.BeanDefinition) (err error) {
113113

114114
// Injects all beans
115115
r.state = Refreshing
116-
for _, b := range beans {
116+
for _, b := range roots {
117117
if err = r.wireBean(b, stack); err != nil {
118118
return err
119119
}

0 commit comments

Comments
 (0)