mirror of
https://gitee.com/johng/gf
synced 2026-06-10 11:27:17 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ab0a77364 | |||
| 79a3aa5916 | |||
| 733c5db228 | |||
| 6171c621a7 |
@ -94,11 +94,36 @@ func Config(file...string) *gcfg.Config {
|
||||
}
|
||||
return instances.GetOrSetFuncLock(fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_CONFIG, configFile),
|
||||
func() interface{} {
|
||||
path := cmdenv.Get("gf.gcfg.path", gfile.SelfDir()).String()
|
||||
config := gcfg.New(path, configFile)
|
||||
// 添加基于源码的搜索目录检索地址,常用于开发环境调试,只添加入口文件目录
|
||||
if p := gfile.MainPkgPath(); p != "" && gfile.Exists(p) {
|
||||
config.AddPath(p)
|
||||
pwdPath := gfile.Pwd()
|
||||
envPath := cmdenv.Get("gf.gcfg.path").String()
|
||||
selfPath := gfile.SelfDir()
|
||||
mainPath := gfile.MainPkgPath()
|
||||
|
||||
config := gcfg.New(pwdPath, configFile)
|
||||
// 添加工作目录下的config目录
|
||||
if path := envPath + gfile.Separator + "config"; gfile.Exists(path) {
|
||||
config.AddPath(path)
|
||||
}
|
||||
// 自定义的环境变量/启动参数路径,优先级最高,覆盖默认的工作目录
|
||||
if envPath != "" && gfile.Exists(envPath) {
|
||||
config.SetPath(envPath)
|
||||
if path := envPath + gfile.Separator + "config"; gfile.Exists(path) {
|
||||
config.AddPath(path)
|
||||
}
|
||||
}
|
||||
// 二进制文件执行目录
|
||||
if selfPath != "" && gfile.Exists(selfPath) {
|
||||
config.AddPath(selfPath)
|
||||
if path := selfPath + gfile.Separator + "config"; gfile.Exists(path) {
|
||||
config.AddPath(path)
|
||||
}
|
||||
}
|
||||
// 开发环境源码main包目录
|
||||
if mainPath != "" && gfile.Exists(mainPath) {
|
||||
config.AddPath(mainPath)
|
||||
if path := mainPath + gfile.Separator + "config"; gfile.Exists(path) {
|
||||
config.AddPath(path)
|
||||
}
|
||||
}
|
||||
return config
|
||||
}).(*gcfg.Config)
|
||||
|
||||
@ -17,7 +17,7 @@ import (
|
||||
// 规则:
|
||||
// 1、命令行参数以小写字母格式,使用: gf.包名.变量名 传递;
|
||||
// 2、环境变量参数以大写字母格式,使用: GF_包名_变量名 传递;
|
||||
func Get(key string, def...interface{}) *gvar.Var {
|
||||
func Get(key string, def...interface{}) gvar.VarRead {
|
||||
value := interface{}(nil)
|
||||
if len(def) > 0 {
|
||||
value = def[0]
|
||||
|
||||
@ -67,35 +67,4 @@ type CustomMsg = map[string]interface{}
|
||||
func parseSequenceTag(tag string) (name, rule, msg string) {
|
||||
match, _ := gregex.MatchString(`\s*((\w+)\s*@){0,1}\s*([^#]+)\s*(#\s*(.*)){0,1}\s*`, tag)
|
||||
return strings.TrimSpace(match[2]), strings.TrimSpace(match[3]), strings.TrimSpace(match[5])
|
||||
}
|
||||
|
||||
// 解析sequence tag为标准校验规则及自定义错误
|
||||
func parseSequenceTags(tags []string) (rules map[string]string, msgs map[string]interface{}) {
|
||||
rules = make(map[string]string)
|
||||
msgs = make(map[string]interface{})
|
||||
for _, tag := range tags {
|
||||
name, rule, msg := parseSequenceTag(tag)
|
||||
// 校验规则
|
||||
if len(name) == 0 {
|
||||
continue
|
||||
}
|
||||
rules[name] = rule
|
||||
// 错误提示
|
||||
if len(msg) > 0 {
|
||||
ruleArray := strings.Split(rule, "|")
|
||||
msgArray := strings.Split(msg, "|")
|
||||
for k, v := range ruleArray {
|
||||
if len(msgArray[k]) == 0 {
|
||||
continue
|
||||
}
|
||||
// 关联校验会有":"符号
|
||||
array := strings.Split(v, ":")
|
||||
if _, ok := msgs[name]; !ok {
|
||||
msgs[name] = make(map[string]string)
|
||||
}
|
||||
msgs[name].(map[string]string)[strings.TrimSpace(array[0])] = strings.TrimSpace(msgArray[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -130,7 +130,7 @@ func Check(value interface{}, rules string, msgs interface{}, params...interface
|
||||
msgArray = strings.Split(v, "|")
|
||||
default:
|
||||
for k, v := range gconv.Map(msgs) {
|
||||
data[k] = gconv.String(v)
|
||||
customMsgMap[k] = gconv.String(v)
|
||||
}
|
||||
}
|
||||
ruleItems := strings.Split(strings.TrimSpace(rules), "|")
|
||||
|
||||
@ -400,6 +400,9 @@ func Test_QQ(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Ip(t *testing.T) {
|
||||
if m := gvalid.Check("10.0.0.1", "ip", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("10.0.0.1", "ipv4", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
@ -409,12 +412,21 @@ func Test_Ip(t *testing.T) {
|
||||
if m := gvalid.Check("1920.0.0.0", "ipv4", nil); m == nil {
|
||||
t.Error("ipv4校验失败")
|
||||
}
|
||||
if m := gvalid.Check("1920.0.0.0", "ip", nil); m == nil {
|
||||
t.Error("ipv4校验失败")
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ip", nil); m != nil {
|
||||
t.Error(m)
|
||||
}
|
||||
if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil {
|
||||
t.Error(m)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_IPv4(t *testing.T) {
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gvalid"
|
||||
"testing"
|
||||
)
|
||||
@ -65,3 +66,35 @@ func Test_CheckMapWithNilAndNotRequiredField(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Sequence(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
params := map[string]interface{} {
|
||||
"passport" : "",
|
||||
"password" : "123456",
|
||||
"password2" : "1234567",
|
||||
}
|
||||
rules := []string {
|
||||
"passport@required|length:6,16#账号不能为空|账号长度应当在:min到:max之间",
|
||||
"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等",
|
||||
"password2@required|length:6,16#",
|
||||
}
|
||||
err := gvalid.CheckMap(params, rules)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Map()), 2)
|
||||
gtest.Assert(err.Map()["required"], "账号不能为空")
|
||||
gtest.Assert(err.Map()["length"], "账号长度应当在6到16之间")
|
||||
gtest.Assert(len(err.Maps()), 2)
|
||||
|
||||
gtest.Assert(err.String(), "账号不能为空; 账号长度应当在6到16之间; 两次密码输入不相等")
|
||||
gtest.Assert(err.Strings(), []string{"账号不能为空", "账号长度应当在6到16之间", "两次密码输入不相等"})
|
||||
|
||||
k, m := err.FirstItem()
|
||||
gtest.Assert(k, "passport")
|
||||
gtest.Assert(m, err.Map())
|
||||
|
||||
r, s := err.FirstRule()
|
||||
gtest.Assert(r, "required")
|
||||
gtest.Assert(s, "账号不能为空")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -7,28 +7,66 @@
|
||||
package gvalid_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/util/gvalid"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_CheckStruct(t *testing.T) {
|
||||
type Object struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
rules := map[string]string {
|
||||
"Name" : "required|length:6,16",
|
||||
"Age" : "between:18,30",
|
||||
}
|
||||
msgs := map[string]interface{} {
|
||||
"Name" : map[string]string {
|
||||
"required" : "名称不能为空",
|
||||
"length" : "名称长度为:min到:max个字符",
|
||||
},
|
||||
"Age" : "年龄为18到30周岁",
|
||||
}
|
||||
obj := &Object{"john", 16}
|
||||
if m := gvalid.CheckStruct(obj, rules, msgs); m == nil {
|
||||
t.Error("CheckObject校验失败")
|
||||
}
|
||||
gtest.Case(t, func() {
|
||||
type Object struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
rules := map[string]string {
|
||||
"Name" : "required|length:6,16",
|
||||
"Age" : "between:18,30",
|
||||
}
|
||||
msgs := map[string]interface{} {
|
||||
"Name" : map[string]string {
|
||||
"required" : "名称不能为空",
|
||||
"length" : "名称长度为:min到:max个字符",
|
||||
},
|
||||
"Age" : "年龄为18到30周岁",
|
||||
}
|
||||
obj := &Object{"john", 16}
|
||||
err := gvalid.CheckStruct(obj, rules, msgs)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 2)
|
||||
gtest.Assert(err.Maps()["Name"]["required"], "")
|
||||
gtest.Assert(err.Maps()["Name"]["length"], "名称长度为6到16个字符")
|
||||
gtest.Assert(err.Maps()["Age"]["between"], "年龄为18到30周岁")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
|
||||
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
|
||||
}
|
||||
var login LoginRequest
|
||||
err := gvalid.CheckStruct(login, nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 2)
|
||||
gtest.Assert(err.Maps()["username"]["required"], "用户名不能为空")
|
||||
gtest.Assert(err.Maps()["password"]["required"], "登录密码不能为空")
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
type User struct {
|
||||
Id int `gvalid:"uid@required|min:10#|ID不能为空"`
|
||||
Age int `gvalid:"age@required#年龄不能为空"`
|
||||
Username string `json:"username" gvalid:"username@required#用户名不能为空"`
|
||||
Password string `json:"password" gvalid:"password@required#登录密码不能为空"`
|
||||
}
|
||||
user := &User{
|
||||
Id : 1,
|
||||
Username : "john",
|
||||
Password : "123456",
|
||||
}
|
||||
err := gvalid.CheckStruct(user, nil)
|
||||
gtest.AssertNE(err, nil)
|
||||
gtest.Assert(len(err.Maps()), 1)
|
||||
gtest.Assert(err.Maps()["uid"]["min"], "ID不能为空")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@ -2,12 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//t := gconv.GTime("2010-10-10 00:00:01")
|
||||
fmt.Println(strings.ToUpper(strconv.FormatInt(gtime.Nanosecond(), 36)))
|
||||
t := gconv.GTime("2010-10-10 00:00:01")
|
||||
fmt.Println(t.String())
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
package gf
|
||||
|
||||
const VERSION = "v1.5.14"
|
||||
const VERSION = "v1.5.15"
|
||||
const AUTHORS = "john<john@goframe.org>"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user