github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/fun/CustomizeFun.go (about)

     1  package fun
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/validate"
     6  	"strconv"
     7  )
     8  
     9  type CustomizeEntity1 struct {
    10  	Name string `match:"customize=judge1Name"`
    11  }
    12  
    13  type CustomizeEntity2 struct {
    14  	Name string `match:"customize=judge2Name"`
    15  }
    16  
    17  type CustomizeEntity3 struct {
    18  	Name string `match:"customize=judge3Name"`
    19  	Age  int
    20  }
    21  
    22  type CustomizeEntity4 struct {
    23  	Name string `match:"customize=judge4Name"`
    24  	Age  int
    25  }
    26  
    27  type CustomizeEntity5 struct {
    28  	Name string `match:"customize=judge5Name"`
    29  	Age  int
    30  }
    31  
    32  type CustomizeEntity6 struct {
    33  	Name1 string `match:"customize=judge6Name_1"`
    34  	Name2 string `match:"customize=judge6Name_2"`
    35  	Name3 string `match:"customize=judge6Name_3"`
    36  	Age   int
    37  }
    38  
    39  type CustomizeEntity7 struct {
    40  	Name *string `match:"customize=judge7Name"`
    41  	Flag *bool `match:"customize=judge7Flag"`
    42  	Flag2 *bool `match:"customize=judge7Flag2"`
    43  	Age  int
    44  }
    45  
    46  func JudgeString1(name string) bool {
    47  	if name == "zhou" || name == "宋江" {
    48  		return true
    49  	}
    50  
    51  	return false
    52  }
    53  
    54  func JudgeString2(name string) (bool, string) {
    55  	if name == "zhou" || name == "宋江" {
    56  		return true, ""
    57  	}
    58  
    59  	return false, "没有命中可用的值'zhou'和'宋江'"
    60  }
    61  
    62  func JudgeString3(customize CustomizeEntity3, name string) (bool, string) {
    63  	if name == "zhou" || name == "宋江" {
    64  		if customize.Age > 12 {
    65  			return true, ""
    66  		} else {
    67  			return false, "用户[" + name + "]" + "没有满足年龄age > 12," + "当前年龄为:" + fmt.Sprintf("%v", customize.Age)
    68  		}
    69  
    70  	} else {
    71  		return false, "没有命中可用的值'zhou'和'宋江'"
    72  	}
    73  }
    74  func JudgeString4(customize CustomizeEntity4, name string) (string, bool) {
    75  	if name == "zhou" || name == "宋江" {
    76  		if customize.Age > 12 {
    77  			return "", true
    78  		} else {
    79  			return "用户[" + name + "]" + "没有满足年龄age > 12," + "当前年龄为:" + fmt.Sprintf("%v", customize.Age), false
    80  		}
    81  
    82  	} else {
    83  		return "没有命中可用的值'zhou'和'宋江'", false
    84  	}
    85  }
    86  
    87  func JudgeString5(customize CustomizeEntity5) (string, bool) {
    88  	var name = customize.Name
    89  	if name == "zhou" || name == "宋江" {
    90  		if customize.Age > 12 {
    91  			return "", true
    92  		} else {
    93  			return "用户[" + name + "]" + "没有满足年龄age > 12," + "当前年龄为:" + fmt.Sprintf("%v", customize.Age), false
    94  		}
    95  
    96  	} else {
    97  		return "没有命中可用的值'zhou'和'宋江'", false
    98  	}
    99  }
   100  
   101  func JudgeString6_1(customize CustomizeEntity6, parameterMap map[string]interface{}) (string, bool) {
   102  	nameV := parameterMap["name"]
   103  	name := fmt.Sprintf("%v", nameV)
   104  
   105  	ageV := parameterMap["age"]
   106  	age, _ := strconv.Atoi(fmt.Sprintf("%v", ageV))
   107  	if name == "zhou" || name == "宋江" {
   108  		if age > 12 {
   109  			return "", true
   110  		} else {
   111  			return "用户[" + name + "]" + "没有满足年龄age > 12," + "当前年龄为:" + fmt.Sprintf("%v", age), false
   112  		}
   113  
   114  	} else {
   115  		return "没有命中可用的值'zhou'和'宋江'", false
   116  	}
   117  }
   118  
   119  func JudgeString6_2(customize CustomizeEntity6, name string, parameterMap map[string]interface{}) (string, bool) {
   120  	ageV := parameterMap["age"]
   121  	age, _ := strconv.Atoi(fmt.Sprintf("%v", ageV))
   122  	if name == "zhou" || name == "宋江" {
   123  		if age > 12 {
   124  			return "", true
   125  		} else {
   126  			return "用户[" + name + "]" + "没有满足年龄age > 12," + "当前年龄为:" + fmt.Sprintf("%v", age), false
   127  		}
   128  	} else {
   129  		return "没有命中可用的值'zhou'和'宋江'", false
   130  	}
   131  }
   132  
   133  func JudgeString6_3(name string, parameterMap map[string]interface{}) (string, bool) {
   134  	ageV := parameterMap["age"]
   135  	age, _ := strconv.Atoi(fmt.Sprintf("%v", ageV))
   136  	if name == "zhou" || name == "宋江" {
   137  		if age > 12 {
   138  			return "", true
   139  		} else {
   140  			return "用户[" + name + "]" + "没有满足年龄age > 12," + "当前年龄为:" + fmt.Sprintf("%v", age), false
   141  		}
   142  	} else {
   143  		return "没有命中可用的值'zhou'和'宋江'", false
   144  	}
   145  }
   146  
   147  func JudgeString7(customize CustomizeEntity7) (string, bool) {
   148  	var name = customize.Name
   149  	if name == nil {
   150  		return "用户不可为空", false
   151  	}
   152  	return "", true
   153  }
   154  
   155  func JudgeString7Flag(customize CustomizeEntity7) (string, bool) {
   156  	var flag = customize.Flag
   157  	if flag == nil {
   158  		return "flag不可为空", false
   159  	}
   160  	return "", true
   161  }
   162  
   163  func JudgeString7Flag2(flag *bool) (string, bool) {
   164  	if flag == nil {
   165  		return "flag不可为空", false
   166  	}
   167  	return "", true
   168  }
   169  
   170  func init() {
   171  	validate.RegisterCustomize("judge1Name", JudgeString1)
   172  	validate.RegisterCustomize("judge2Name", JudgeString2)
   173  	validate.RegisterCustomize("judge3Name", JudgeString3)
   174  	validate.RegisterCustomize("judge4Name", JudgeString4)
   175  	validate.RegisterCustomize("judge5Name", JudgeString5)
   176  	validate.RegisterCustomize("judge6Name_1", JudgeString6_1)
   177  	validate.RegisterCustomize("judge6Name_2", JudgeString6_2)
   178  	validate.RegisterCustomize("judge6Name_3", JudgeString6_3)
   179  	validate.RegisterCustomize("judge7Name", JudgeString7)
   180  	validate.RegisterCustomize("judge7Flag", JudgeString7Flag)
   181  	validate.RegisterCustomize("judge7Flag2", JudgeString7Flag2)
   182  }