@@ -121,19 +121,18 @@ package conf
121121import (
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.
224223type MutableProperties struct {
225- * storage .Storage
224+ * barky .Storage
226225}
227226
228227// New creates empty *MutableProperties.
229228func 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.
254255func 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}.
293275func (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.
340322func (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}
0 commit comments