mirror of https://github.com/rocboss/paopao-ce
commit
fd665abb8b
@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
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)
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
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 := &Features{
|
||||
suites: suites,
|
||||
kv: kv,
|
||||
features: make(map[string]string),
|
||||
}
|
||||
f.UseDefault()
|
||||
return f
|
||||
}
|
||||
|
||||
func newEmptyFeatures() *Features {
|
||||
return &Features{
|
||||
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) error {
|
||||
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]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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,3 +1,7 @@
|
||||
package types
|
||||
|
||||
// Empty empty alias type
|
||||
type Empty = struct{}
|
||||
|
||||
// Fn empty argument func alias type
|
||||
type Fn = func()
|
||||
|
Loading…
Reference in new issue