@@ -121,19 +121,18 @@ package conf
121
121
import (
122
122
"errors"
123
123
"fmt"
124
- "maps"
125
124
"os"
126
125
"path/filepath"
127
126
"reflect"
127
+ "runtime"
128
128
"strings"
129
129
"time"
130
130
131
+ "github.com/go-spring/barky"
131
132
"github.com/go-spring/spring-core/conf/reader/json"
132
133
"github.com/go-spring/spring-core/conf/reader/prop"
133
134
"github.com/go-spring/spring-core/conf/reader/toml"
134
135
"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"
137
136
"github.com/spf13/cast"
138
137
)
139
138
@@ -222,13 +221,13 @@ var _ Properties = (*MutableProperties)(nil)
222
221
// but it costs more CPU time when getting properties because it reads property node
223
222
// by node. So `conf` uses a tree to strictly verify and a flat map to store.
224
223
type MutableProperties struct {
225
- * storage .Storage
224
+ * barky .Storage
226
225
}
227
226
228
227
// New creates empty *MutableProperties.
229
228
func New () * MutableProperties {
230
229
return & MutableProperties {
231
- Storage : storage .NewStorage (),
230
+ Storage : barky .NewStorage (),
232
231
}
233
232
}
234
233
@@ -247,47 +246,30 @@ func Load(file string) (*MutableProperties, error) {
247
246
if err != nil {
248
247
return nil , err
249
248
}
250
- return Map (m ), nil
249
+ p := New ()
250
+ _ = p .merge (barky .FlattenMap (m ), file )
251
+ return p , nil
251
252
}
252
253
253
254
// Map creates *MutableProperties from map.
254
255
func Map (m map [string ]any ) * MutableProperties {
255
256
p := New ()
256
- _ = p .merge (util .FlattenMap (m ))
257
+ _ , file , _ , _ := runtime .Caller (1 )
258
+ _ = p .merge (barky .FlattenMap (m ), file )
257
259
return p
258
260
}
259
261
260
262
// 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 )
262
265
for key , val := range m {
263
- if err := p .Set (key , val ); err != nil {
266
+ if err := p .Set (key , val , fileID ); err != nil {
264
267
return err
265
268
}
266
269
}
267
270
return nil
268
271
}
269
272
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
-
291
273
// Resolve resolves string value that contains references to other
292
274
// properties, the references are defined by ${key:=def}.
293
275
func (p * MutableProperties ) Resolve (s string ) (string , error ) {
@@ -338,5 +320,18 @@ func (p *MutableProperties) Bind(i any, tag ...string) error {
338
320
339
321
// CopyTo copies properties into another by override.
340
322
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
342
337
}
0 commit comments