Browse Source

remove key

pull/4/head
andrey 3 months ago
parent
commit
792b48306a
  1. 32
      key/helpers.go
  2. 64
      key/helpers_test.go
  3. 46
      key/key.go

32
key/helpers.go

@ -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
}
}

64
key/helpers_test.go

@ -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)
}
}

46
key/key.go

@ -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)
}
}
Loading…
Cancel
Save