From 792b48306a997acc421469b6d24351b2892cd7d1 Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 25 Jan 2024 19:04:29 +0300 Subject: [PATCH] remove key --- key/helpers.go | 32 ----------------------- key/helpers_test.go | 64 --------------------------------------------- key/key.go | 46 -------------------------------- 3 files changed, 142 deletions(-) delete mode 100644 key/helpers.go delete mode 100644 key/helpers_test.go delete mode 100644 key/key.go diff --git a/key/helpers.go b/key/helpers.go deleted file mode 100644 index acd672f..0000000 --- a/key/helpers.go +++ /dev/null @@ -1,32 +0,0 @@ -package key - -import ( - "context" - "strings" - - "gitoa.ru/go-4devs/config" -) - -func LastIndex(sep string, factory config.KeyFactory) func(ctx context.Context, key config.Key) (string, string) { - return func(ctx context.Context, key config.Key) (string, string) { - name := factory(ctx, key) - - idx := strings.LastIndex(name, sep) - if idx == -1 { - return name, "" - } - - return name[0:idx], name[idx+len(sep):] - } -} - -func LastIndexField(sep, def string, factory config.KeyFactory) func(ctx context.Context, key config.Key) (string, string) { - return func(ctx context.Context, key config.Key) (string, string) { - p, k := LastIndex(sep, factory)(ctx, key) - if k == "" { - return p, def - } - - return p, k - } -} diff --git a/key/helpers_test.go b/key/helpers_test.go deleted file mode 100644 index d0fd980..0000000 --- a/key/helpers_test.go +++ /dev/null @@ -1,64 +0,0 @@ -package key_test - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" - "gitoa.ru/go-4devs/config" - "gitoa.ru/go-4devs/config/key" -) - -func TestLastIndex(t *testing.T) { - t.Parallel() - - ctx := context.Background() - - cases := map[string]struct { - sep string - path string - field string - }{ - "/secret/with/field/name": { - sep: "/", - path: "/secret/with/field", - field: "name", - }, - "/secret/database:username": { - sep: ":", - path: "/secret/database", - field: "username", - }, - "database:username": { - sep: ":", - path: "database", - field: "username", - }, - "/secret/database-dsn": { - sep: ":", - path: "/secret/database-dsn", - field: "", - }, - "/secret/database--dsn": { - sep: "--", - path: "/secret/database", - field: "dsn", - }, - "/secret/database:dsn": { - sep: "--", - path: "/secret/database:dsn", - field: "", - }, - } - - for path, data := range cases { - k := config.Key{ - Name: path, - } - - fn := key.LastIndex(data.sep, key.Name) - ns, field := fn(ctx, k) - assert.Equal(t, data.field, field, k) - assert.Equal(t, data.path, ns, k) - } -} diff --git a/key/key.go b/key/key.go deleted file mode 100644 index 4ba9990..0000000 --- a/key/key.go +++ /dev/null @@ -1,46 +0,0 @@ -package key - -import ( - "context" - "strings" - - "gitoa.ru/go-4devs/config" -) - -func NsAppName(sep string) config.KeyFactory { - return func(_ context.Context, key config.Key) string { - return strings.Join([]string{key.Namespace, key.AppName, key.Name}, sep) - } -} - -func AppName(sep string) config.KeyFactory { - return func(_ context.Context, key config.Key) string { - return strings.Join([]string{key.AppName, key.Name}, sep) - } -} - -func PrefixName(prefix string, factory config.KeyFactory) config.KeyFactory { - return func(ctx context.Context, key config.Key) string { - return prefix + factory(ctx, key) - } -} - -func Name(_ context.Context, key config.Key) string { - return key.Name -} - -func AliasName(name string, alias string, def config.KeyFactory) config.KeyFactory { - return func(ctx context.Context, key config.Key) string { - if name == key.Name { - return alias - } - - return def(ctx, key) - } -} - -func ReplaceAll(oldVal, newVal string, parent config.KeyFactory) config.KeyFactory { - return func(ctx context.Context, key config.Key) string { - return strings.ReplaceAll(parent(ctx, key), oldVal, newVal) - } -}