From fe93d7b332031d79dda5a2a0303895f051805b2f Mon Sep 17 00:00:00 2001 From: John Guo Date: Fri, 14 Jan 2022 16:46:25 +0800 Subject: [PATCH] add GetRegisteredRuleMap function, add internal logging for rule overwrite situation of package gvalid --- os/gview/gview_parse.go | 2 +- util/guid/guid.go | 4 ++-- util/gvalid/gvalid_custom_rule.go | 24 +++++++++++++++++++ .../gvalid_z_unit_feature_custom_rule_test.go | 17 +++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index 418bb3fdf..ea89b5503 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -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) diff --git a/util/guid/guid.go b/util/guid/guid.go index 2259abb81..cea74ab19 100644 --- a/util/guid/guid.go +++ b/util/guid/guid.go @@ -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 } diff --git a/util/gvalid/gvalid_custom_rule.go b/util/gvalid/gvalid_custom_rule.go index 73dc0e648..2521eb4bf 100644 --- a/util/gvalid/gvalid_custom_rule.go +++ b/util/gvalid/gvalid_custom_rule.go @@ -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 { diff --git a/util/gvalid/gvalid_z_unit_feature_custom_rule_test.go b/util/gvalid/gvalid_z_unit_feature_custom_rule_test.go index 360804602..8aaf05eee 100644 --- a/util/gvalid/gvalid_z_unit_feature_custom_rule_test.go +++ b/util/gvalid/gvalid_z_unit_feature_custom_rule_test.go @@ -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()) +}