Skip to content

Commit d27cd9b

Browse files
authored
Merge pull request #33 from lvan100/main
add Module & new Testing
2 parents 13189a9 + d4ad3ee commit d27cd9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+503
-1679
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: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +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"
136-
"github.com/go-spring/spring-core/util"
137136
"github.com/spf13/cast"
138137
)
139138

@@ -222,13 +221,13 @@ var _ Properties = (*MutableProperties)(nil)
222221
// but it costs more CPU time when getting properties because it reads property node
223222
// by node. So `conf` uses a tree to strictly verify and a flat map to store.
224223
type MutableProperties struct {
225-
*storage.Storage
224+
*barky.Storage
226225
}
227226

228227
// New creates empty *MutableProperties.
229228
func New() *MutableProperties {
230229
return &MutableProperties{
231-
Storage: storage.NewStorage(),
230+
Storage: barky.NewStorage(),
232231
}
233232
}
234233

@@ -247,47 +246,30 @@ func Load(file string) (*MutableProperties, error) {
247246
if err != nil {
248247
return nil, err
249248
}
250-
return Map(m), nil
249+
p := New()
250+
_ = p.merge(barky.FlattenMap(m), file)
251+
return p, nil
251252
}
252253

253254
// Map creates *MutableProperties from map.
254255
func Map(m map[string]any) *MutableProperties {
255256
p := New()
256-
_ = p.merge(util.FlattenMap(m))
257+
_, file, _, _ := runtime.Caller(1)
258+
_ = p.merge(barky.FlattenMap(m), file)
257259
return p
258260
}
259261

260262
// merge flattens the map and sets all keys and values.
261-
func (p *MutableProperties) merge(m map[string]string) error {
263+
func (p *MutableProperties) merge(m map[string]string, file string) error {
264+
fileID := p.AddFile(file)
262265
for key, val := range m {
263-
if err := p.Set(key, val); err != nil {
266+
if err := p.Set(key, val, fileID); err != nil {
264267
return err
265268
}
266269
}
267270
return nil
268271
}
269272

270-
// Data returns key-value pairs of the properties.
271-
func (p *MutableProperties) Data() map[string]string {
272-
m := make(map[string]string)
273-
maps.Copy(m, p.RawData())
274-
return m
275-
}
276-
277-
// Keys returns keys of the properties.
278-
func (p *MutableProperties) Keys() []string {
279-
return util.OrderedMapKeys(p.RawData())
280-
}
281-
282-
// Get returns key's value, using Def to return a default value.
283-
func (p *MutableProperties) Get(key string, def ...string) string {
284-
val, ok := p.RawData()[key]
285-
if !ok && len(def) > 0 {
286-
return def[0]
287-
}
288-
return val
289-
}
290-
291273
// Resolve resolves string value that contains references to other
292274
// properties, the references are defined by ${key:=def}.
293275
func (p *MutableProperties) Resolve(s string) (string, error) {
@@ -338,5 +320,18 @@ func (p *MutableProperties) Bind(i any, tag ...string) error {
338320

339321
// CopyTo copies properties into another by override.
340322
func (p *MutableProperties) CopyTo(out *MutableProperties) error {
341-
return out.merge(p.RawData())
323+
rawFile := p.RawFile()
324+
newfile := make(map[string]int8)
325+
oldFile := make([]string, len(rawFile))
326+
for k, v := range rawFile {
327+
oldFile[v] = k
328+
newfile[k] = out.AddFile(k)
329+
}
330+
for key, v := range p.RawData() {
331+
fileID := newfile[oldFile[v.File]]
332+
if err := out.Set(key, v.Value, fileID); err != nil {
333+
return err
334+
}
335+
}
336+
return nil
342337
}

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=

conf/storage/path.go

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

0 commit comments

Comments
 (0)