add GetRegisteredRuleMap function, add internal logging for rule overwrite situation of package gvalid

This commit is contained in:
John Guo
2022-01-14 16:46:25 +08:00
parent 067514b74f
commit fe93d7b332
4 changed files with 44 additions and 3 deletions

View File

@ -181,7 +181,7 @@ func (view *View) ParseContent(ctx context.Context, content string, params ...Pa
})
)
// Using memory lock to ensure concurrent safety for content parsing.
hash := strconv.FormatUint(ghash.DJBHash64([]byte(content)), 10)
hash := strconv.FormatUint(ghash.DJB64([]byte(content)), 10)
gmlock.LockFunc("gview.ParseContent:"+hash, func() {
if view.config.AutoEncode {
tpl, err = tpl.(*htmltpl.Template).Parse(content)

View File

@ -38,7 +38,7 @@ func init() {
macAddrBytes = append(macAddrBytes, []byte(mac)...)
}
b := []byte{'0', '0', '0', '0', '0', '0', '0'}
s := strconv.FormatUint(uint64(ghash.DJBHash(macAddrBytes)), 36)
s := strconv.FormatUint(uint64(ghash.DJB(macAddrBytes)), 36)
copy(b, s)
macAddrStr = string(b)
}
@ -124,7 +124,7 @@ func getRandomStr(n int) []byte {
// getDataHashStr creates and returns hash bytes in 7 bytes with given data bytes.
func getDataHashStr(data []byte) []byte {
b := []byte{'0', '0', '0', '0', '0', '0', '0'}
s := strconv.FormatUint(uint64(ghash.DJBHash(data)), 36)
s := strconv.FormatUint(uint64(ghash.DJB(data)), 36)
copy(b, s)
return b
}

View File

@ -8,8 +8,12 @@ package gvalid
import (
"context"
"fmt"
"reflect"
"runtime"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/internal/intlog"
)
// RuleFunc is the custom function for data validation.
@ -39,6 +43,14 @@ var (
// RegisterRule registers custom validation rule and function for package.
func RegisterRule(rule string, f RuleFunc) {
if customRuleFuncMap[rule] != nil {
intlog.PrintFunc(context.TODO(), func() string {
return fmt.Sprintf(
`rule "%s" is overwrotten by function "%s"`,
rule, runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(),
)
})
}
customRuleFuncMap[rule] = f
}
@ -49,6 +61,18 @@ func RegisterRuleByMap(m map[string]RuleFunc) {
}
}
// GetRegisteredRuleMap returns all the custom registered rules and associated functions.
func GetRegisteredRuleMap() map[string]RuleFunc {
if len(customRuleFuncMap) == 0 {
return nil
}
ruleMap := make(map[string]RuleFunc)
for k, v := range customRuleFuncMap {
ruleMap[k] = v
}
return ruleMap
}
// DeleteRule deletes custom defined validation one or more rules and associated functions from global package.
func DeleteRule(rules ...string) {
for _, rule := range rules {

View File

@ -11,8 +11,10 @@ import (
"errors"
"testing"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/util/gvalid"
)
@ -271,3 +273,18 @@ func TestValidator_RuleFuncMap(t *testing.T) {
t.AssertNil(err)
})
}
func Test_CustomRule_Overwrite(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var rule = "custom-" + guid.S()
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
return gerror.New("1")
})
t.Assert(g.Validator().Rules(rule).Data(1).Run(ctx), "1")
gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
return gerror.New("2")
})
t.Assert(g.Validator().Rules(rule).Data(1).Run(ctx), "2")
})
g.Dump(gvalid.GetRegisteredRuleMap())
}