Skip to content

Commit

Permalink
[BREAK] in Store API, unexport struct ptr removed, instead of Store
Browse files Browse the repository at this point in the history
… interface.

These apis changed to:

- `Clone() (newStore Store)`
- `Dup() (newStore Store)`
- `WithPrefix(prefix ...string) (newStore Store)`
- `WithPrefixReplaced(newPrefix ...string) (newStore Store)`
  • Loading branch information
hedzr committed Nov 1, 2024
1 parent 1b4adfe commit 6a053ca
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
27 changes: 14 additions & 13 deletions dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
"strings"
"time"

"github.com/hedzr/store/radix"
Expand All @@ -10,7 +11,7 @@ import (
// NewDummyStore returns an empty store with dummy abilities implemented.
func NewDummyStore() *dummyS { return &dummyS{} }

type dummyS struct{}
type dummyS struct{ p string }

var _ radix.TypedGetters[any] = (*dummyS)(nil) // assertion helper

Expand Down Expand Up @@ -311,15 +312,15 @@ func (s *dummyS) GetM(path string, opt ...radix.MOpt[any]) (ret map[string]any,
func (s *dummyS) MustM(path string, opt ...radix.MOpt[any]) (ret map[string]any) { return } //nolint:revive
func (s *dummyS) GetSectionFrom(path string, holder any, opts ...radix.MOpt[any]) (err error) { return } //nolint:revive

func (s *dummyS) Dump() (text string) { return } //nolint:revive
func (s *dummyS) Clone() (newStore *storeS) { return } //nolint:revive
func (s *dummyS) Dup() (newStore *storeS) { return } //nolint:revive
func (s *dummyS) Walk(path string, cb func(path, fragment string, node radix.Node[any])) {} //nolint:revive
func (s *dummyS) WithPrefix(prefix ...string) (newStore *storeS) { return } //nolint:revive
func (s *dummyS) WithPrefixReplaced(prefix ...string) (newStore *storeS) { return } //nolint:revive
func (s *dummyS) SetPrefix(prefix ...string) {} //nolint:revive
func (s *dummyS) Prefix() string { return "" } //nolint:revive
func (s *dummyS) Delimiter() rune { return 0 } //nolint:revive
func (s *dummyS) SetDelimiter(delimiter rune) {} //nolint:revive
func (s *dummyS) Load(ctx context.Context, opts ...LoadOpt) (wr Writeable, err error) { return } //nolint:revive
func (s *dummyS) WithinLoading(fn func()) { fn() } //nolint:revive
func (s *dummyS) Dump() (text string) { return } //nolint:revive
func (s *dummyS) Clone() (newStore Store) { return } //nolint:revive
func (s *dummyS) Dup() (newStore Store) { return } //nolint:revive
func (s *dummyS) Walk(path string, cb func(path, fragment string, node radix.Node[any])) {} //nolint:revive
func (s *dummyS) WithPrefix(prefix ...string) (newStore Store) { return s } //nolint:revive
func (s *dummyS) WithPrefixReplaced(prefix ...string) (newStore Store) { return s } //nolint:revive
func (s *dummyS) SetPrefix(prefix ...string) { s.p = strings.Join(prefix, ".") } //nolint:revive
func (s *dummyS) Prefix() string { return s.p } //nolint:revive
func (s *dummyS) Delimiter() rune { return '.' } //nolint:revive
func (s *dummyS) SetDelimiter(delimiter rune) {} //nolint:revive
func (s *dummyS) Load(ctx context.Context, opts ...LoadOpt) (wr Writeable, err error) { return } //nolint:revive
func (s *dummyS) WithinLoading(fn func()) { fn() } //nolint:revive
27 changes: 22 additions & 5 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hedzr/evendeep"
logz "github.com/hedzr/logg/slog"

"github.com/hedzr/store/radix"
)

Expand Down Expand Up @@ -144,7 +145,11 @@ func (s *storeS) loadMapByValueType(ec errors.Error, position, k string, v any,
}
break
}
s.WithPrefixReplaced(position).setKV(k, v, creating, onSet)
if cc, ok := s.WithPrefixReplaced(position).(interface {
setKV(path string, data any, createOrModify bool, onSet lmOnSet) (node radix.Node[any], oldData any)
}); ok {
cc.setKV(k, v, creating, onSet)
}
case []map[any]any:
if s.flattenSlice {
buf := make([]byte, 0, len(k)+16)
Expand All @@ -157,7 +162,11 @@ func (s *storeS) loadMapByValueType(ec errors.Error, position, k string, v any,
}
break
}
s.WithPrefixReplaced(position).setKV(k, v, creating, onSet)
if cc, ok := s.WithPrefixReplaced(position).(interface {
setKV(path string, data any, createOrModify bool, onSet lmOnSet) (node radix.Node[any], oldData any)
}); ok {
cc.setKV(k, v, creating, onSet)
}
case []any:
if s.flattenSlice {
buf := make([]byte, 0, len(k)+16)
Expand All @@ -174,9 +183,17 @@ func (s *storeS) loadMapByValueType(ec errors.Error, position, k string, v any,
}
break
}
s.WithPrefixReplaced(position).setKV(k, v, creating, onSet)
if cc, ok := s.WithPrefixReplaced(position).(interface {
setKV(path string, data any, createOrModify bool, onSet lmOnSet) (node radix.Node[any], oldData any)
}); ok {
cc.setKV(k, v, creating, onSet)
}
default:
s.WithPrefixReplaced(position).setKV(k, v, creating, onSet)
if cc, ok := s.WithPrefixReplaced(position).(interface {
setKV(path string, data any, createOrModify bool, onSet lmOnSet) (node radix.Node[any], oldData any)
}); ok {
cc.setKV(k, v, creating, onSet)
}
}
}

Expand Down Expand Up @@ -337,7 +354,7 @@ func WithCodec(codec Codec) LoadOpt {
// location that the external settings will be merged at.
func WithStorePrefix(prefix string) LoadOpt {
return func(s *loadS) {
s.storeS = s.storeS.WithPrefixReplaced(prefix)
s.storeS = s.storeS.WithPrefixReplaced(prefix).(*storeS)
}
}

Expand Down
37 changes: 29 additions & 8 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ type Store interface {
Has(path string) (found bool)

// Locate provides an advanced interface for locating a path.
//
// RETURNs:
// node: the matched node for retrieving node data later
// branch: true means a branch node matched (generally partialMatched is true)
// patialMatched: true means only a part of the node key was matched.
// found: any (fully or partially) found.
//
// When querying "app.logging.f" on a tree holding "app.logging.file",
// Locate will return
// found = true, partialMatched = true,
// branch = false, and
// node is pointed to "app.logging.file".
//
// These high order apis (Has, Get(Xxx), Set(Xxx), Must(Xxx)) covers the
// Locate's results and provides a dotted-key-path-based behaviors.
// Which means, Has("app.logging.f") gets false and
// Has("app.logging.file") is true.
Locate(path string) (node radix.Node[any], branch, partialMatched, found bool)

radix.TypedGetters[any] // getters
Expand All @@ -96,7 +113,7 @@ type Store interface {
Dump() (text string)

// Clone makes a clone copy for this store
Clone() (newStore *storeS)
Clone() (newStore Store)

// Dup is a native Clone tool.
//
Expand All @@ -107,7 +124,7 @@ type Store interface {
// remote connection such as what want to do by consul provider.
//
// At this scene, the parent store still holds the cleanup closers.
Dup() (newStore *storeS)
Dup() (newStore Store)

// Walk does iterate the whole Store.
//
Expand Down Expand Up @@ -136,9 +153,9 @@ type Store interface {
//
// It simplify biz-logic codes sometimes.
//
// A [Delimiter] will be inserted at jointing prefix and key. Also at
// jointing old and new prefix.
WithPrefix(prefix ...string) (newStore *storeS) // todo need a balance on returning *storeS or Store, for WithPrefix
// The arg 'prefix' can be an array, which will be joint
// with the [Delimiter].
WithPrefix(prefix ...string) (newStore Store)

// WithPrefixReplaced is similar with WithPrefix, but it replaces old
// prefix with new one instead of appending it.
Expand All @@ -148,17 +165,21 @@ type Store interface {
// ns := s1.WithPrefixReplaced("app.server")
// println(ns.MustGet("type")) # print conf["app.server.type"]
//
// A [Delimiter] will be inserted at jointing prefix and key.
// The arg 'prefix' can be an array, which will be joint
// with the [Delimiter].
//
// todo need a balance on returning *storeS or Store, for WithPrefixReplaced.
WithPrefixReplaced(newPrefix ...string) (newStore *storeS)
WithPrefixReplaced(newPrefix ...string) (newStore Store)

// SetPrefix updates the prefix in current storeS.
//
// The arg 'prefix' can be an array, which will be joint
// with the [Delimiter].
SetPrefix(newPrefix ...string)

Prefix() string // return current prefix string
Delimiter() rune // return current delimiter, generally it's dot ('.')
SetDelimiter(delimiter rune) // setter. Change it in runtime doesn't update old delimiter inside tree nodes.
SetDelimiter(delimiter rune) // setter. Change it at runtime doesn't update old delimiter inside tree nodes.

// Load loads k-v pairs from external provider(s) with specified codec decoder(s).
//
Expand Down
8 changes: 4 additions & 4 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (s *storeS) Dump() (text string) {
return s.Trie.Dump()
}

func (s *storeS) Clone() (newStore *storeS) { return s.Dup() } // make a clone for this store
func (s *storeS) Clone() (newStore Store) { return s.Dup() } // make a clone for this store

// Dup is a native Clone tool.
//
Expand All @@ -399,7 +399,7 @@ func (s *storeS) Clone() (newStore *storeS) { return s.Dup() } // make a clone f
// remote connection such as what want to do by consul provider.
//
// At this scene, the parent store still holds the cleanup closers.
func (s *storeS) Dup() (newStore *storeS) {
func (s *storeS) Dup() (newStore Store) {
return s.dupS(s.Trie.Dup())
}

Expand All @@ -423,7 +423,7 @@ func (s *storeS) Dup() (newStore *storeS) {
//
// A [Delimiter] will be inserted at jointing prefix and key. Also at
// jointing old and new prefix.
func (s *storeS) WithPrefix(prefix ...string) (newStore *storeS) {
func (s *storeS) WithPrefix(prefix ...string) (newStore Store) {
return s.dupS(s.Trie.WithPrefix(prefix...))
}

Expand All @@ -436,7 +436,7 @@ func (s *storeS) WithPrefix(prefix ...string) (newStore *storeS) {
// println(ns.MustGet("type")) # print conf["app.server.type"]
//
// A [Delimiter] will be inserted at jointing prefix and key.
func (s *storeS) WithPrefixReplaced(newPrefix ...string) (newStore *storeS) {
func (s *storeS) WithPrefixReplaced(newPrefix ...string) (newStore Store) {
return s.dupS(s.Trie.WithPrefixReplaced(newPrefix...))
}

Expand Down

0 comments on commit 6a053ca

Please sign in to comment.