Skip to content

Commit b300b75

Browse files
lvan100lianghuan
authored andcommitted
feat(core): add Module
1 parent 13189a9 commit b300b75

File tree

35 files changed

+304
-1062
lines changed

35 files changed

+304
-1062
lines changed

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ Go-Spring provides multiple ways to register Beans:
211211
- **`gs.Object(obj)`** - Registers an existing object as a Bean
212212
- **`gs.Provide(ctor, args...)`** - Uses a constructor to generate and register a Bean
213213
- **`gs.Register(bd)`** - Registers a complete Bean definition (suitable for low-level encapsulation or advanced usage)
214-
- **`gs.GroupRegister(fn)`** - Batch registers multiple Beans (commonly used for module initialization and other
215-
scenarios)
216214

217215
Example:
218216

@@ -221,14 +219,6 @@ gs.Object(&Service{}) // Register a struct instance
221219
gs.Provide(NewService) // Register using a constructor
222220
gs.Provide(NewRepo, gs.ValueArg("db")) // Constructor with parameters
223221
gs.Register(gs.NewBean(NewService)) // Complete definition registration
224-
225-
// Batch register multiple Beans
226-
gs.GroupRegister(func (p conf.Properties) []*gs.BeanDefinition {
227-
return []*gs.BeanDefinition{
228-
gs.NewBean(NewUserService),
229-
gs.NewBean(NewOrderService),
230-
}
231-
})
232222
```
233223

234224
### 2️⃣ Injection Methods
@@ -317,11 +307,10 @@ feature toggles, and gray release scenarios.
317307
### 🎯 Common Condition Types
318308

319309
- **`OnProperty("key")`**: Activates when the specified configuration key exists
320-
- **`OnMissingProperty("key")`**: Activates when the specified configuration key does not exist
321310
- **`OnBean[Type]("name")`**: Activates when a Bean of the specified type/name exists
322311
- **`OnMissingBean[Type]("name")`**: Activates when a Bean of the specified type/name does not exist
323312
- **`OnSingleBean[Type]("name")`**: Activates when a Bean of the specified type/name is the only instance
324-
- **`OnFunc(func(ctx CondContext) bool)`**: Uses custom condition logic to determine activation
313+
- **`OnFunc(func(ctx ConditionContext) bool)`**: Uses custom condition logic to determine activation
325314

326315
Example:
327316

README_CN.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ Go-Spring 提供多种方式注册 Bean:
189189
- **`gs.Object(obj)`** - 将已有对象注册为 Bean
190190
- **`gs.Provide(ctor, args...)`** - 使用构造函数生成并注册 Bean
191191
- **`gs.Register(bd)`** - 注册完整 Bean 定义(适合底层封装或高级用法)
192-
- **`gs.GroupRegister(fn)`** - 批量注册多个 Bean(常用于模块初始化等场景)
193192

194193
示例:
195194

@@ -198,14 +197,6 @@ gs.Object(&Service{}) // 注册结构体实例
198197
gs.Provide(NewService) // 使用构造函数注册
199198
gs.Provide(NewRepo, gs.ValueArg("db")) // 构造函数带参数
200199
gs.Register(gs.NewBean(NewService)) // 完整定义注册
201-
202-
// 批量注册多个 Bean
203-
gs.GroupRegister(func (p conf.Properties) []*gs.BeanDefinition {
204-
return []*gs.BeanDefinition{
205-
gs.NewBean(NewUserService),
206-
gs.NewBean(NewOrderService),
207-
}
208-
})
209200
```
210201

211202
### 2️⃣ 注入方式
@@ -291,11 +282,10 @@ Go-Spring 借鉴 Spring 的 `@Conditional` 思想,实现了灵活强大的条
291282
### 🎯 常用条件类型
292283

293284
- **`OnProperty("key")`**:当指定配置 key 存在时激活
294-
- **`OnMissingProperty("key")`**:当指定配置 key 不存在时激活
295285
- **`OnBean[Type]("name")`**:当指定类型/名称的 Bean 存在时激活
296286
- **`OnMissingBean[Type]("name")`**:当指定类型/名称的 Bean 不存在时激活
297287
- **`OnSingleBean[Type]("name")`**:当指定类型/名称的 Bean 是唯一实例时激活
298-
- **`OnFunc(func(ctx CondContext) bool)`**:使用自定义条件逻辑判断是否激活
288+
- **`OnFunc(func(ctx ConditionContext) bool)`**:使用自定义条件逻辑判断是否激活
299289

300290
示例:
301291

conf/bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func getSlice(p Properties, et reflect.Type, param BindParam) (Properties, error
301301
r := New()
302302
for i, s := range arrVal {
303303
k := fmt.Sprintf("%s[%d]", param.Key, i)
304-
_ = r.Set(k, s) // always no error
304+
_ = r.Set(k, s, 0) // always no error
305305
}
306306
return r, nil
307307
}

conf/bind_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -537,20 +537,22 @@ func TestProperties_Bind(t *testing.T) {
537537
p, err := conf.Load("./testdata/config/app.yaml")
538538
assert.That(t, err).Nil()
539539

540-
err = p.Set("extra.intsV0", "")
540+
fileID := p.AddFile("bind_test.go")
541+
542+
err = p.Set("extra.intsV0", "", fileID)
541543
assert.That(t, err).Nil()
542-
err = p.Set("extra.intsV2", "1,2,3")
544+
err = p.Set("extra.intsV2", "1,2,3", fileID)
543545
assert.That(t, err).Nil()
544-
err = p.Set("prefix.extra.intsV2", "1,2,3")
546+
err = p.Set("prefix.extra.intsV2", "1,2,3", fileID)
545547
assert.That(t, err).Nil()
546548

547-
err = p.Set("extra.mapV2.a", "1")
549+
err = p.Set("extra.mapV2.a", "1", fileID)
548550
assert.That(t, err).Nil()
549-
err = p.Set("extra.mapV2.b", "2")
551+
err = p.Set("extra.mapV2.b", "2", fileID)
550552
assert.That(t, err).Nil()
551-
err = p.Set("prefix.extra.mapV2.a", "1")
553+
err = p.Set("prefix.extra.mapV2.a", "1", fileID)
552554
assert.That(t, err).Nil()
553-
err = p.Set("prefix.extra.mapV2.b", "2")
555+
err = p.Set("prefix.extra.mapV2.b", "2", fileID)
554556
assert.That(t, err).Nil()
555557

556558
var c DBConfig

conf/conf.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,18 @@ package conf
121121
import (
122122
"errors"
123123
"fmt"
124-
"maps"
125124
"os"
126125
"path/filepath"
127126
"reflect"
127+
"runtime"
128128
"strings"
129129
"time"
130130

131+
"github.com/go-spring/barky"
131132
"github.com/go-spring/spring-core/conf/reader/json"
132133
"github.com/go-spring/spring-core/conf/reader/prop"
133134
"github.com/go-spring/spring-core/conf/reader/toml"
134135
"github.com/go-spring/spring-core/conf/reader/yaml"
135-
"github.com/go-spring/spring-core/conf/storage"
136136
"github.com/go-spring/spring-core/util"
137137
"github.com/spf13/cast"
138138
)
@@ -222,13 +222,13 @@ var _ Properties = (*MutableProperties)(nil)
222222
// but it costs more CPU time when getting properties because it reads property node
223223
// by node. So `conf` uses a tree to strictly verify and a flat map to store.
224224
type MutableProperties struct {
225-
*storage.Storage
225+
*barky.Storage
226226
}
227227

228228
// New creates empty *MutableProperties.
229229
func New() *MutableProperties {
230230
return &MutableProperties{
231-
Storage: storage.NewStorage(),
231+
Storage: barky.NewStorage(),
232232
}
233233
}
234234

@@ -247,20 +247,24 @@ func Load(file string) (*MutableProperties, error) {
247247
if err != nil {
248248
return nil, err
249249
}
250-
return Map(m), nil
250+
p := New()
251+
_ = p.merge(util.FlattenMap(m), file)
252+
return p, nil
251253
}
252254

253255
// Map creates *MutableProperties from map.
254256
func Map(m map[string]any) *MutableProperties {
255257
p := New()
256-
_ = p.merge(util.FlattenMap(m))
258+
_, file, _, _ := runtime.Caller(1)
259+
_ = p.merge(util.FlattenMap(m), file)
257260
return p
258261
}
259262

260263
// merge flattens the map and sets all keys and values.
261-
func (p *MutableProperties) merge(m map[string]string) error {
264+
func (p *MutableProperties) merge(m map[string]string, file string) error {
265+
fileID := p.AddFile(file)
262266
for key, val := range m {
263-
if err := p.Set(key, val); err != nil {
267+
if err := p.Set(key, val, fileID); err != nil {
264268
return err
265269
}
266270
}
@@ -270,7 +274,9 @@ func (p *MutableProperties) merge(m map[string]string) error {
270274
// Data returns key-value pairs of the properties.
271275
func (p *MutableProperties) Data() map[string]string {
272276
m := make(map[string]string)
273-
maps.Copy(m, p.RawData())
277+
for k, v := range p.RawData() {
278+
m[k] = v.Value
279+
}
274280
return m
275281
}
276282

@@ -281,11 +287,11 @@ func (p *MutableProperties) Keys() []string {
281287

282288
// Get returns key's value, using Def to return a default value.
283289
func (p *MutableProperties) Get(key string, def ...string) string {
284-
val, ok := p.RawData()[key]
290+
v, ok := p.RawData()[key]
285291
if !ok && len(def) > 0 {
286292
return def[0]
287293
}
288-
return val
294+
return v.Value
289295
}
290296

291297
// Resolve resolves string value that contains references to other
@@ -338,5 +344,18 @@ func (p *MutableProperties) Bind(i any, tag ...string) error {
338344

339345
// CopyTo copies properties into another by override.
340346
func (p *MutableProperties) CopyTo(out *MutableProperties) error {
341-
return out.merge(p.RawData())
347+
rawFile := p.RawFile()
348+
newfile := make(map[string]int8)
349+
oldFile := make([]string, len(rawFile))
350+
for k, v := range rawFile {
351+
oldFile[v] = k
352+
newfile[k] = out.AddFile(k)
353+
}
354+
for key, v := range p.RawData() {
355+
fileID := newfile[oldFile[v.File]]
356+
if err := out.Set(key, v.Value, fileID); err != nil {
357+
return err
358+
}
359+
}
360+
return nil
342361
}

conf/storage/path.go

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)