mirror of https://github.com/rocboss/paopao-ce
parent
6738df8913
commit
72136a2266
@ -1,41 +0,0 @@
|
||||
package cfg
|
||||
|
||||
var (
|
||||
_features = newEmptyFeatures()
|
||||
|
||||
// Use alias of Features.Use func
|
||||
Use = _features.Use
|
||||
|
||||
// UseDeafult alias of Features.UseDefault func
|
||||
UseDefault = _features.UseDefault
|
||||
|
||||
// As alias of Features.Cfg func
|
||||
As = _features.Cfg
|
||||
|
||||
// If alias of Features.CfgIf func
|
||||
If = _features.CfgIf
|
||||
|
||||
// In alias of Features.CfgIn func
|
||||
In = _features.CfgIn
|
||||
|
||||
// Be alias of Feaures.CfgBe func
|
||||
Be = _features.CfgBe
|
||||
|
||||
// Not alias of Features.CfgNot func
|
||||
Not = _features.CfgNot
|
||||
)
|
||||
|
||||
// Initialize initialize features in cfg pkg
|
||||
func Initialize(suites map[string][]string, kv map[string]string) {
|
||||
_features = NewFeatures(suites, kv)
|
||||
{
|
||||
// must re-assign variable below
|
||||
Use = _features.Use
|
||||
UseDefault = _features.UseDefault
|
||||
As = _features.Cfg
|
||||
If = _features.CfgIf
|
||||
In = _features.CfgIn
|
||||
Be = _features.CfgBe
|
||||
Not = _features.CfgNot
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCfg(t *testing.T) {
|
||||
suites := map[string][]string{
|
||||
"default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"},
|
||||
"develop": {"Zinc", "MySQL", "AliOSS", "LogFile"},
|
||||
"slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"},
|
||||
}
|
||||
kv := map[string]string{
|
||||
"sms": "SmsJuhe",
|
||||
}
|
||||
|
||||
Initialize(suites, kv)
|
||||
UseDefault()
|
||||
|
||||
if !If("Sms") {
|
||||
t.Error(`want If("Sms") == true but not`)
|
||||
}
|
||||
|
||||
if v, exist := As("Sms"); exist && v != "SmsJuhe" {
|
||||
t.Errorf(`want As("Sms") == "SmsJuhe", true but got: "%s", "%t"`, v, exist)
|
||||
}
|
||||
|
||||
matched := false
|
||||
Be("Alipay", func() {
|
||||
matched = true
|
||||
})
|
||||
if !matched {
|
||||
t.Error(`want Be("Alipay", ...) matched but not`)
|
||||
}
|
||||
|
||||
matched = false
|
||||
Not("LogFile", func() {
|
||||
matched = true
|
||||
})
|
||||
if !matched {
|
||||
t.Error(`want Not("LogFile", ...) matched but not`)
|
||||
}
|
||||
|
||||
var m1, m2, m3, m4 bool
|
||||
In(Actions{
|
||||
"Sms": func() {
|
||||
m1 = true
|
||||
},
|
||||
"Alipay": func() {
|
||||
m2 = true
|
||||
},
|
||||
"Meili": func() {
|
||||
m4 = true
|
||||
},
|
||||
}, func() {
|
||||
m3 = true
|
||||
})
|
||||
if !m1 || !m2 || m3 || m4 {
|
||||
t.Errorf(`In("Sms", "Alipay", "Meili", ...) not correct -> m1: %t m2:%t m3:%t m4:%t`, m1, m2, m3, m4)
|
||||
}
|
||||
|
||||
m1 = false
|
||||
m2 = false
|
||||
m3 = false
|
||||
In(Actions{
|
||||
"LogFile": func() {
|
||||
m1 = true
|
||||
},
|
||||
"Meili": func() {
|
||||
m2 = true
|
||||
},
|
||||
}, func() {
|
||||
m3 = true
|
||||
})
|
||||
if m1 || m2 || !m3 {
|
||||
t.Errorf(`In("Zinc", "MySQL", ...) not correct -> m1: %t m2:%t m3:%t`, m1, m2, m3)
|
||||
}
|
||||
}
|
@ -1,149 +0,0 @@
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/rocboss/paopao-ce/pkg/types"
|
||||
)
|
||||
|
||||
// Features fetures info struct
|
||||
type Features struct {
|
||||
kv map[string]string
|
||||
suites map[string][]string
|
||||
features map[string]string
|
||||
}
|
||||
|
||||
// Actions feature-func map alias type
|
||||
type Actions map[string]types.Fn
|
||||
|
||||
// NewFeatures create new Features instance
|
||||
func NewFeatures(suites map[string][]string, kv map[string]string) *Features {
|
||||
f := newEmptyFeatures()
|
||||
for k, v := range suites {
|
||||
if len(k) > 0 {
|
||||
for i := 0; i < len(v); i++ {
|
||||
// ignore empty string
|
||||
if len(v[i]) == 0 {
|
||||
lastIdx := len(v) - 1
|
||||
v[i] = v[lastIdx]
|
||||
v = v[:lastIdx]
|
||||
i--
|
||||
}
|
||||
}
|
||||
if len(v) > 0 {
|
||||
f.suites[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
for k, v := range kv {
|
||||
if len(k) > 0 && len(v) > 0 {
|
||||
f.kv[k] = v
|
||||
}
|
||||
}
|
||||
f.UseDefault()
|
||||
return f
|
||||
}
|
||||
|
||||
func newEmptyFeatures() *Features {
|
||||
return &Features{
|
||||
suites: make(map[string][]string),
|
||||
kv: make(map[string]string),
|
||||
features: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// UseDefault use default suite for features
|
||||
func (f *Features) UseDefault() {
|
||||
f.Use([]string{"default"}, true)
|
||||
}
|
||||
|
||||
// Use use custom suite for features
|
||||
func (f *Features) Use(suite []string, noDefault bool) {
|
||||
if noDefault && len(f.features) != 0 {
|
||||
f.features = make(map[string]string)
|
||||
}
|
||||
features := f.flatFeatures(suite)
|
||||
for _, feature := range features {
|
||||
if len(feature) == 0 {
|
||||
continue
|
||||
}
|
||||
f.features[feature] = f.kv[feature]
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Features) flatFeatures(suite []string) []string {
|
||||
features := make([]string, 0, len(suite)+10)
|
||||
for s := suite[:]; len(s) > 0; s = s[:len(s)-1] {
|
||||
item := strings.TrimSpace(strings.ToLower(s[0]))
|
||||
if len(item) > 0 {
|
||||
if items, exist := f.suites[item]; exist {
|
||||
s = append(s, items...)
|
||||
}
|
||||
features = append(features, item)
|
||||
}
|
||||
s[0] = s[len(s)-1]
|
||||
}
|
||||
return features
|
||||
}
|
||||
|
||||
// Cfg get value by key if exist
|
||||
func (f *Features) Cfg(key string) (string, bool) {
|
||||
key = strings.ToLower(key)
|
||||
value, exist := f.features[key]
|
||||
return value, exist
|
||||
}
|
||||
|
||||
// CfgIf check expression is true. if expression just have a string like
|
||||
// `Sms` is mean `Sms` whether define in suite feature settings. expression like
|
||||
// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value
|
||||
// is `SmsJuhe`
|
||||
func (f *Features) CfgIf(expression string) bool {
|
||||
kv := strings.Split(expression, "=")
|
||||
key := strings.Trim(strings.ToLower(kv[0]), " ")
|
||||
v, ok := f.features[key]
|
||||
if len(kv) == 2 && ok && strings.Trim(kv[1], " ") == v {
|
||||
return true
|
||||
} else if len(kv) == 1 && ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CfgIn range actions to check item's expression is true then do the handle, defFn will handle
|
||||
// if all items are not matched,
|
||||
func (f *Features) CfgIn(actions Actions, defAct ...types.Fn) {
|
||||
itemMatched := false
|
||||
for expression, handle := range actions {
|
||||
if f.CfgIf(expression) && handle != nil {
|
||||
handle()
|
||||
itemMatched = true
|
||||
}
|
||||
}
|
||||
if !itemMatched && len(defAct) > 0 {
|
||||
for _, handle := range defAct {
|
||||
if handle != nil {
|
||||
handle()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CfgBe check expression is true then do the handle. if expression just have a string like
|
||||
// `Sms` is mean `Sms` whether defined in suite feature settings. expression like
|
||||
// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value
|
||||
// is `SmsJuhe`
|
||||
func (f *Features) CfgBe(expression string, handle types.Fn) {
|
||||
if f.CfgIf(expression) && handle != nil {
|
||||
handle()
|
||||
}
|
||||
}
|
||||
|
||||
// CfgNot check expression is not true then do the handle. if expression just have a string like
|
||||
// `Sms` is mean `Sms` whether defined in suite feature settings. expression like
|
||||
// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value
|
||||
// is `SmsJuhe`
|
||||
func (f *Features) CfgNot(expression string, handle func()) {
|
||||
if !f.CfgIf(expression) {
|
||||
handle()
|
||||
}
|
||||
}
|
@ -1,114 +0,0 @@
|
||||
package cfg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUseDefault(t *testing.T) {
|
||||
suites := map[string][]string{
|
||||
"default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"},
|
||||
"develop": {"Zinc", "MySQL", "AliOSS", "LogFile"},
|
||||
"slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"},
|
||||
}
|
||||
kv := map[string]string{
|
||||
"sms": "SmsJuhe",
|
||||
}
|
||||
f := NewFeatures(suites, kv)
|
||||
for _, data := range []struct {
|
||||
key string
|
||||
expect string
|
||||
exist bool
|
||||
}{
|
||||
{"Sms", "SmsJuhe", true},
|
||||
{"Alipay", "", true},
|
||||
{"Zinc", "", true},
|
||||
{"Redis", "", true},
|
||||
{"Database", "", false},
|
||||
} {
|
||||
if v, ok := f.Cfg(data.key); ok != data.exist || v != data.expect {
|
||||
t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok)
|
||||
}
|
||||
}
|
||||
for exp, res := range map[string]bool{
|
||||
"Sms": true,
|
||||
"Sms = SmsJuhe": true,
|
||||
"SmsJuhe": false,
|
||||
"default": true,
|
||||
} {
|
||||
if ok := f.CfgIf(exp); res != ok {
|
||||
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUse(t *testing.T) {
|
||||
suites := map[string][]string{
|
||||
"default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"},
|
||||
"develop": {"Zinc", "MySQL", "AliOSS", "LogFile"},
|
||||
"slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"},
|
||||
}
|
||||
kv := map[string]string{
|
||||
"sms": "SmsJuhe",
|
||||
}
|
||||
f := NewFeatures(suites, kv)
|
||||
|
||||
f.Use([]string{"develop"}, true)
|
||||
for _, data := range []struct {
|
||||
key string
|
||||
expect string
|
||||
exist bool
|
||||
}{
|
||||
{"Sms", "", false},
|
||||
{"Alipay", "", false},
|
||||
{"Zinc", "", true},
|
||||
{"Redis", "", false},
|
||||
{"Database", "", false},
|
||||
} {
|
||||
if v, ok := f.Cfg(data.key); ok != data.exist || v != data.expect {
|
||||
t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok)
|
||||
}
|
||||
}
|
||||
for exp, res := range map[string]bool{
|
||||
"Sms": false,
|
||||
"Sms = SmsJuhe": false,
|
||||
"SmsJuhe": false,
|
||||
"default": false,
|
||||
"develop": true,
|
||||
} {
|
||||
if ok := f.CfgIf(exp); res != ok {
|
||||
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)
|
||||
}
|
||||
}
|
||||
|
||||
f.UseDefault()
|
||||
f.Use([]string{"slim", "", "demo"}, false)
|
||||
for _, data := range []struct {
|
||||
key string
|
||||
expect string
|
||||
exist bool
|
||||
}{
|
||||
{"Sms", "SmsJuhe", true},
|
||||
{"Alipay", "", true},
|
||||
{"Zinc", "", true},
|
||||
{"Redis", "", true},
|
||||
{"Database", "", false},
|
||||
{"demo", "", true},
|
||||
} {
|
||||
if v, ok := f.Cfg(data.key); ok != data.exist || v != data.expect {
|
||||
t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok)
|
||||
}
|
||||
}
|
||||
for exp, res := range map[string]bool{
|
||||
"Sms": true,
|
||||
"Sms = SmsJuhe": true,
|
||||
"SmsJuhe": false,
|
||||
"default": true,
|
||||
"develop": false,
|
||||
"slim": true,
|
||||
"demo": true,
|
||||
} {
|
||||
if ok := f.CfgIf(exp); res != ok {
|
||||
t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue