Skip to content

Commit 2dd2233

Browse files
authored
Merge pull request #6 from go-spring-projects/redesign-bean-event
Redesign the AppEvent interface to get away from Go-Spring dependence
2 parents 6a32f12 + 4558021 commit 2dd2233

File tree

9 files changed

+71
-58
lines changed

9 files changed

+71
-58
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func main() {
9090
}
9191

9292
// Output:
93-
// time=2023-09-25T14:50:32.927+08:00 level=INFO source=main.go:14 msg="Hello world" logger=go-spring type=main.MyApp
93+
// time=2023-09-25T14:50:32.927+08:00 level=INFO source=main.go:14 msg="Hello world" logger=go-spring
9494
```
9595

9696
#### Bean register
@@ -483,9 +483,9 @@ func main() {
483483
}
484484

485485
// Output:
486-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello primary logger","logger":"app","type":"main.App"}
487-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello system logger","logger":"sys","type":"main.App"}
488-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello trace logger","logger":"trace","type":"main.App"}
486+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello primary logger","logger":"app"}
487+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello system logger","logger":"sys"}
488+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello trace logger","logger":"trace"}
489489
```
490490

491491
### Dependent order event

README_CN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func main() {
9090
}
9191

9292
// Output:
93-
// time=2023-09-25T14:50:32.927+08:00 level=INFO source=main.go:14 msg="Hello world" logger=go-spring type=main.MyApp
93+
// time=2023-09-25T14:50:32.927+08:00 level=INFO source=main.go:14 msg="Hello world" logger=go-spring
9494
```
9595

9696
#### Bean register
@@ -482,9 +482,9 @@ func main() {
482482
}
483483

484484
// Output:
485-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello primary logger","logger":"app","type":"main.App"}
486-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello system logger","logger":"sys","type":"main.App"}
487-
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello trace logger","logger":"trace","type":"main.App"}
485+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello primary logger","logger":"app"}
486+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello system logger","logger":"sys"}
487+
// {"time":"2023-10-27T12:10:14.8040121+08:00","level":"INFO","msg":"hello trace logger","logger":"trace"}
488488
```
489489

490490
### 依赖序事件

gs/app.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,11 @@ import (
2929

3030
"github.com/go-spring-projects/go-spring/conf"
3131
"github.com/go-spring-projects/go-spring/gs/arg"
32-
"github.com/go-spring-projects/go-spring/internal/utils"
3332
)
3433

35-
// AppRunner .
36-
type AppRunner interface {
37-
Run(ctx Context)
38-
}
39-
4034
// AppEvent start and stop events
4135
type AppEvent interface {
42-
OnAppStart(ctx Context)
36+
OnAppStart(ctx context.Context)
4337
OnAppStop(ctx context.Context)
4438
}
4539

@@ -87,9 +81,7 @@ func (app *App) run(resourceLocator ResourceLocator) error {
8781
return err
8882
}
8983

90-
var logger = GetLogger("", utils.TypeName(app))
91-
92-
app.onAppRun(app.container)
84+
var logger = GetLogger()
9385

9486
app.onAppStart(app.container)
9587

@@ -105,7 +97,7 @@ func (app *App) run(resourceLocator ResourceLocator) error {
10597

10698
<-app.exitChan
10799

108-
app.onAppStop(context.Background())
100+
app.onAppStop(app.container)
109101

110102
app.container.Close()
111103

@@ -114,32 +106,24 @@ func (app *App) run(resourceLocator ResourceLocator) error {
114106
return nil
115107
}
116108

117-
func (app *App) onAppRun(ctx Context) {
118-
for _, bean := range app.container.Dependencies(true) {
119-
x := bean.Value().Interface()
120-
121-
if ar, ok := x.(AppRunner); ok {
122-
ar.Run(ctx)
123-
}
124-
}
125-
}
126-
127109
func (app *App) onAppStart(ctx Context) {
110+
gsCtx := WithContext(ctx.Context(), ctx)
128111
for _, bean := range app.container.Dependencies(true) {
129112
x := bean.Value().Interface()
130113

131114
if ae, ok := x.(AppEvent); ok {
132-
ae.OnAppStart(ctx)
115+
ae.OnAppStart(gsCtx)
133116
}
134117
}
135118
}
136119

137-
func (app *App) onAppStop(ctx context.Context) {
120+
func (app *App) onAppStop(ctx Context) {
121+
gsCtx := WithContext(context.Background(), ctx)
138122
for _, bean := range app.container.Dependencies(false) {
139123
x := bean.Value().Interface()
140124

141125
if ae, ok := x.(AppEvent); ok {
142-
ae.OnAppStop(ctx)
126+
ae.OnAppStop(gsCtx)
143127
}
144128
}
145129
}
@@ -184,7 +168,7 @@ func (app *App) Shutdown(msg ...string) {
184168
case <-app.exitChan:
185169
// app already closed
186170
default:
187-
var logger = GetLogger("", utils.TypeName(app))
171+
var logger = GetLogger()
188172
logger.Info(fmt.Sprintf("program will exit %s", strings.Join(msg, ", ")))
189173
close(app.exitChan)
190174
}

gs/gs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ type Context interface {
8181

8282
type contextKey struct{}
8383

84-
func WithContext(ctx Context) context.Context {
85-
return context.WithValue(ctx.Context(), contextKey{}, ctx)
84+
func WithContext(parent context.Context, ctx Context) context.Context {
85+
return context.WithValue(parent, contextKey{}, ctx)
8686
}
8787

8888
func FromContext(ctx context.Context) Context {
@@ -392,7 +392,7 @@ func (c *container) refresh(autoClear bool) (err error) {
392392
}
393393

394394
c.state = Refreshing
395-
c.logger = GetLogger("", utils.TypeName(c))
395+
c.logger = GetLogger()
396396

397397
for _, b := range c.beans {
398398
c.registerBean(b)
@@ -828,7 +828,7 @@ func (c *container) wireStruct(v reflect.Value, t reflect.Type, param conf.BindP
828828
tag = parseWireTag(tag).beanName
829829
}
830830

831-
l := GetLogger(tag, utils.TypeName(v))
831+
l := GetLogger(WithLogName(tag))
832832
if nil == l {
833833
return fmt.Errorf("logger field %s not provide: %s", fieldPath, tag)
834834
}

gs/gs_bean.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (d *BeanDefinition) constructor(ctx Context) error {
301301
fnValue := reflect.ValueOf(d.init)
302302
fnValues := []reflect.Value{d.Value()}
303303
if fnValue.Type().NumIn() > 1 {
304-
fnValues = append(fnValues, reflect.ValueOf(WithContext(ctx)))
304+
fnValues = append(fnValues, reflect.ValueOf(WithContext(ctx.Context(), ctx)))
305305
}
306306

307307
out := fnValue.Call(fnValues)
@@ -311,7 +311,7 @@ func (d *BeanDefinition) constructor(ctx Context) error {
311311
}
312312

313313
if f, ok := d.Interface().(BeanInit); ok {
314-
if err := f.OnInit(WithContext(ctx)); err != nil {
314+
if err := f.OnInit(WithContext(ctx.Context(), ctx)); err != nil {
315315
return err
316316
}
317317
}

gs/logger.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,32 @@
1616

1717
package gs
1818

19-
import "github.com/go-spring-projects/go-spring/internal/log"
19+
import (
20+
"github.com/go-spring-projects/go-spring/internal/log"
21+
)
2022

2123
type Logger = log.Logger
2224

2325
func SetLogger(loggerName string, logger *Logger, primary ...bool) {
2426
log.SetLogger(loggerName, logger, primary...)
2527
}
2628

27-
func GetLogger(loggerName string, typeName string) *Logger {
28-
return log.GetLogger(loggerName, typeName)
29+
func GetLogger(getOptions ...GetLogOption) *Logger {
30+
options := &logOptions{}
31+
for _, fn := range getOptions {
32+
fn(options)
33+
}
34+
return log.GetLogger(options.loggerName)
35+
}
36+
37+
type GetLogOption func(*logOptions)
38+
39+
type logOptions struct {
40+
loggerName string
41+
}
42+
43+
func WithLogName(name string) GetLogOption {
44+
return func(options *logOptions) {
45+
options.loggerName = name
46+
}
2947
}

internal/log/logger.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package log
1919
import (
2020
"log/slog"
2121
"os"
22-
"path/filepath"
23-
"strings"
2422
"sync"
23+
24+
"github.com/go-spring-projects/go-spring/internal/utils"
2525
)
2626

2727
type Logger = slog.Logger
@@ -35,19 +35,8 @@ func init() {
3535
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
3636
if slog.SourceKey == attr.Key {
3737
source := attr.Value.Any().(*slog.Source)
38-
idx := strings.LastIndexByte(source.File, '/')
39-
if idx == -1 {
40-
return attr
41-
}
42-
// Find the penultimate separator.
43-
idx = strings.LastIndexByte(source.File[:idx], '/')
44-
if idx == -1 {
45-
return attr
46-
}
47-
48-
source.File = source.File[idx+1:]
38+
source.File = utils.StripTypeName(source.File)
4939
}
50-
5140
return attr
5241
},
5342
}
@@ -68,10 +57,10 @@ func SetLogger(loggerName string, logger *Logger, primary ...bool) {
6857
}
6958
}
7059

71-
func GetLogger(loggerName string, typeName string) *Logger {
60+
func GetLogger(loggerName string) *Logger {
7261
if l, ok := loggers.Load(loggerName); ok {
7362
named := l.(*namedLogger)
74-
return named.logger.With("logger", named.name, "type", filepath.Base(typeName))
63+
return named.logger.With("logger", named.name)
7564
}
7665
return nil
7766
}

internal/utils/type.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,17 @@ func IsBeanReceiver(t reflect.Type) bool {
194194
return IsBeanType(t)
195195
}
196196
}
197+
198+
// StripTypeName returns simpled type name from full pkg path.
199+
func StripTypeName(file string) string {
200+
idx := strings.LastIndexByte(file, '/')
201+
if idx == -1 {
202+
return file
203+
}
204+
// Find the penultimate separator.
205+
idx = strings.LastIndexByte(file[:idx], '/')
206+
if idx == -1 {
207+
return file
208+
}
209+
return file[idx+1:]
210+
}

internal/utils/type_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,11 @@ func TestIsBeanReceiver(t *testing.T) {
564564
assert.False(t, IsBeanReceiver(reflect.TypeOf(map[string]*string{})))
565565
assert.True(t, IsBeanReceiver(reflect.TypeOf(map[string]fmt.Stringer{})))
566566
}
567+
568+
func TestStripTypeNameFromFile(t *testing.T) {
569+
assert.Equal(t, StripTypeName("test.go"), "test.go")
570+
assert.Equal(t, StripTypeName("xx_test.go"), "xx_test.go")
571+
assert.Equal(t, StripTypeName("bar/test.go"), "bar/test.go")
572+
assert.Equal(t, StripTypeName("foo/bar/test.go"), "bar/test.go")
573+
assert.Equal(t, StripTypeName("github.com/foo/bar/test.go"), "bar/test.go")
574+
}

0 commit comments

Comments
 (0)