Skip to content

Commit 757c79a

Browse files
lvan100lianghuan
authored andcommitted
Refactoring: optimizes code comments
1 parent e1aa74f commit 757c79a

File tree

13 files changed

+192
-83
lines changed

13 files changed

+192
-83
lines changed

gs/examples/bookman/.cover/cover.html

Lines changed: 104 additions & 43 deletions
Large diffs are not rendered by default.

gs/examples/miniapi/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@ import (
2323
)
2424

2525
func main() {
26+
// Register an HTTP handler for the "/echo" endpoint.
2627
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
2728
_, _ = w.Write([]byte("hello world!"))
2829
})
30+
31+
// Start the Go-Spring framework.
32+
// Compared to http.ListenAndServe, gs.Run() starts a full-featured application context with:
33+
// - Auto Configuration: Automatically loads properties and beans.
34+
// - Property Binding: Binds external configs (YAML, ENV) into structs.
35+
// - Dependency Injection: Wires beans automatically.
36+
// - Dynamic Refresh: Updates configs at runtime without restart.
2937
gs.Run()
3038
}
3139

gs/examples/noweb/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
)
2222

2323
func main() {
24+
// Disable the built-in HTTP service.
2425
gs.Web(false).Run()
2526
}
2627

gs/examples/startup/README.md

Whitespace-only changes.

gs/gs.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,38 @@ func As[T any]() reflect.Type {
4848
type Arg = gs.Arg
4949

5050
// TagArg returns a TagArg with the specified tag.
51+
// Used for property binding or object injection when providing constructor parameters.
5152
func TagArg(tag string) Arg {
5253
return gs_arg.Tag(tag)
5354
}
5455

5556
// ValueArg returns a ValueArg with the specified value.
57+
// Used to provide specific values for constructor parameters.
5658
func ValueArg(v interface{}) Arg {
5759
return gs_arg.Value(v)
5860
}
5961

6062
// IndexArg returns an IndexArg with the specified index and argument.
63+
// When most constructor parameters can use default values, IndexArg helps reduce configuration effort.
6164
func IndexArg(n int, arg Arg) Arg {
6265
return gs_arg.Index(n, arg)
6366
}
6467

65-
// BindArg returns an BindArg for the specified function and arguments.
68+
// BindArg returns a BindArg for the specified function and arguments.
69+
// Used to provide argument binding for option-style constructor parameters.
6670
func BindArg(fn interface{}, args ...Arg) *gs_arg.BindArg {
6771
return gs_arg.Bind(fn, args...)
6872
}
6973

7074
/************************************ cond ***********************************/
7175

7276
type (
73-
CondFunc = gs.CondFunc
7477
Condition = gs.Condition
7578
CondContext = gs.CondContext
7679
)
7780

7881
// OnFunc creates a Condition based on the provided function.
79-
func OnFunc(fn CondFunc) Condition {
82+
func OnFunc(fn func(ctx CondContext) (bool, error)) Condition {
8083
return gs_cond.OnFunc(fn)
8184
}
8285

@@ -137,6 +140,11 @@ func None(conditions ...Condition) Condition {
137140

138141
/************************************ ioc ************************************/
139142

143+
type (
144+
BeanID = gs.BeanID
145+
BeanMock = gs.BeanMock
146+
)
147+
140148
type (
141149
Dync[T any] = gs_dync.Value[T]
142150
)
@@ -201,21 +209,19 @@ type AppStarter struct{}
201209

202210
// Web enables or disables the built-in web server.
203211
func Web(enable bool) *AppStarter {
204-
if !enable {
205-
EnableSimpleHttpServer(false)
206-
}
212+
EnableSimpleHttpServer(enable)
207213
return &AppStarter{}
208214
}
209215

210216
// Run runs the app and waits for an interrupt signal to exit.
211217
func (s *AppStarter) Run() {
212-
printBanner()
213218
var err error
214219
defer func() {
215220
if err != nil {
216221
syslog.Errorf("app run failed: %s", err.Error())
217222
}
218223
}()
224+
printBanner()
219225
if err = B.(*gs_app.BootImpl).Run(); err != nil {
220226
return
221227
}
@@ -274,7 +280,7 @@ func GroupRegister(fn func(p conf.Properties) ([]*BeanDefinition, error)) {
274280

275281
// RefreshProperties refreshes the app configuration.
276282
func RefreshProperties() error {
277-
p, err := Config().Refresh()
283+
p, err := gs_app.GS.P.Refresh()
278284
if err != nil {
279285
return err
280286
}

gs/gstest/gstest.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
"github.com/go-spring/spring-core/gs"
2828
"github.com/go-spring/spring-core/gs/internal/gs_app"
29-
"github.com/lvan100/go-assert"
3029
)
3130

3231
func init() {
@@ -50,7 +49,10 @@ func MockFor[T any](name ...string) BeanMock[T] {
5049

5150
// With registers a mock bean.
5251
func (m BeanMock[T]) With(obj T) {
53-
gs_app.GS.C.Mock(obj, m.selector)
52+
gs_app.GS.C.AddMock(gs.BeanMock{
53+
Object: obj,
54+
Target: m.selector,
55+
})
5456
}
5557

5658
type runArg struct {
@@ -110,6 +112,8 @@ func Get[T any](t *testing.T) T {
110112
// Wire injects dependencies into the object.
111113
func Wire[T any](t *testing.T, obj T) T {
112114
err := gs_app.GS.C.Wire(obj)
113-
assert.Nil(t, err)
115+
if err != nil {
116+
t.Fatal(err)
117+
}
114118
return obj
115119
}

gs/gstest/gstest_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,19 @@ func TestMain(m *testing.M) {
4242
}
4343

4444
func TestGSTest(t *testing.T) {
45+
// The dao.Dao object was not successfully created,
46+
// and the corresponding injection will also fail.
47+
// The following log will be printed on the console:
48+
// autowire error: TagArg::GetArgValue error << bind path=string type=string error << property dao.addr not exist
49+
4550
a := gstest.Get[*app.App](t)
4651
assert.That(t, a.Name).Equal("test")
52+
4753
s := gstest.Wire(t, new(struct {
4854
App *app.App `autowire:""`
4955
Service *biz.Service `autowire:""`
5056
}))
57+
assert.Nil(t, s.Service.Dao)
5158
assert.That(t, s.App.Name).Equal("test")
5259
assert.That(t, s.Service.Hello("xyz")).Equal("hello xyz")
5360
}

gs/http.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import (
2121
"net"
2222
"net/http"
2323
"time"
24-
25-
"github.com/go-spring/spring-core/gs/internal/gs_cond"
2624
)
2725

2826
func init() {
2927
// Register the default ServeMux as a bean if no other ServeMux instance exists
3028
Object(http.DefaultServeMux).Condition(
31-
gs_cond.OnMissingBean[*http.ServeMux](),
29+
OnMissingBean[*http.ServeMux](),
3230
OnProperty(EnableSimpleHttpServerProp).HavingValue("true").MatchIfMissing(),
3331
)
3432

gs/internal/gs/gs.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ type CondContext interface {
108108
Find(s BeanSelector) ([]CondBean, error)
109109
}
110110

111-
// CondFunc is a function type that determines whether a condition is satisfied.
112-
type CondFunc func(ctx CondContext) (bool, error)
113-
114111
/************************************* arg ***********************************/
115112

116113
// Arg is an interface for retrieving argument values in function parameter binding.
@@ -159,6 +156,12 @@ type Server interface {
159156

160157
/*********************************** bean ************************************/
161158

159+
// BeanMock defines a mock object and its target bean selector for overriding.
160+
type BeanMock struct {
161+
Object interface{} // Mock instance to replace the target bean
162+
Target BeanSelector // Selector to identify the target bean
163+
}
164+
162165
// BeanID represents the unique identifier for a bean.
163166
type BeanID struct {
164167
Type reflect.Type

gs/internal/gs_cond/cond.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ import (
3030
// onFunc is an implementation of [gs.Condition] that wraps a function.
3131
// It allows a condition to be evaluated based on the result of a function.
3232
type onFunc struct {
33-
fn gs.CondFunc
33+
fn func(ctx gs.CondContext) (bool, error)
3434
}
3535

3636
// OnFunc creates a Conditional that evaluates using a custom function.
37-
func OnFunc(fn gs.CondFunc) gs.Condition {
37+
func OnFunc(fn func(ctx gs.CondContext) (bool, error)) gs.Condition {
3838
return &onFunc{fn: fn}
3939
}
4040

0 commit comments

Comments
 (0)