作者 郑周

优化字符串计算长度

@@ -2,6 +2,7 @@ package command @@ -2,6 +2,7 @@ package command
2 2
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
5 ) 6 )
6 7
7 type CreateCycleCommand struct { 8 type CreateCycleCommand struct {
@@ -23,7 +24,7 @@ func (in *CreateCycleCommand) Valid(validation *validation.Validation) { @@ -23,7 +24,7 @@ func (in *CreateCycleCommand) Valid(validation *validation.Validation) {
23 validation.SetError("creatorId", "创建人ID无效") 24 validation.SetError("creatorId", "创建人ID无效")
24 return 25 return
25 } 26 }
26 - if len(in.Name) > 40 { 27 + if utf8.RuneCountInString(in.Name) > 40 {
27 validation.SetError("name", "名称最大长度40个字符") 28 validation.SetError("name", "名称最大长度40个字符")
28 return 29 return
29 } 30 }
@@ -2,6 +2,7 @@ package command @@ -2,6 +2,7 @@ package command
2 2
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
5 ) 6 )
6 7
7 type UpdateCycleCommand struct { 8 type UpdateCycleCommand struct {
@@ -15,7 +16,7 @@ type UpdateCycleCommand struct { @@ -15,7 +16,7 @@ type UpdateCycleCommand struct {
15 } 16 }
16 17
17 func (in *UpdateCycleCommand) Valid(validation *validation.Validation) { 18 func (in *UpdateCycleCommand) Valid(validation *validation.Validation) {
18 - if len(in.Name) > 40 { 19 + if utf8.RuneCountInString(in.Name) > 40 {
19 validation.SetError("name", "角色名称最大长度40个字符") 20 validation.SetError("name", "角色名称最大长度40个字符")
20 return 21 return
21 } 22 }
@@ -2,6 +2,7 @@ package command @@ -2,6 +2,7 @@ package command
2 2
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
5 ) 6 )
6 7
7 type CreateProjectCommand struct { 8 type CreateProjectCommand struct {
@@ -24,7 +25,7 @@ func (in *CreateProjectCommand) Valid(validation *validation.Validation) { @@ -24,7 +25,7 @@ func (in *CreateProjectCommand) Valid(validation *validation.Validation) {
24 validation.SetError("creatorId", "创建人ID无效") 25 validation.SetError("creatorId", "创建人ID无效")
25 return 26 return
26 } 27 }
27 - if len(in.Name) > 40 { 28 + if utf8.RuneCountInString(in.Name) > 40 {
28 validation.SetError("name", "项目名称最大长度40个字符") 29 validation.SetError("name", "项目名称最大长度40个字符")
29 return 30 return
30 } 31 }
@@ -2,6 +2,7 @@ package command @@ -2,6 +2,7 @@ package command
2 2
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
5 ) 6 )
6 7
7 type UpdateProjectCommand struct { 8 type UpdateProjectCommand struct {
@@ -36,7 +37,7 @@ type CheckRecipientCommand struct { @@ -36,7 +37,7 @@ type CheckRecipientCommand struct {
36 } 37 }
37 38
38 func (in *UpdateProjectCommand) Valid(validation *validation.Validation) { 39 func (in *UpdateProjectCommand) Valid(validation *validation.Validation) {
39 - if len(in.Name) > 40 { 40 + if utf8.RuneCountInString(in.Name) > 40 {
40 validation.SetError("name", "项目名称最大长度40个字符") 41 validation.SetError("name", "项目名称最大长度40个字符")
41 return 42 return
42 } 43 }
@@ -3,6 +3,7 @@ package command @@ -3,6 +3,7 @@ package command
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  6 + "unicode/utf8"
6 ) 7 )
7 8
8 type CreateRuleCommand struct { 9 type CreateRuleCommand struct {
@@ -25,12 +26,12 @@ func (in *CreateRuleCommand) Valid(validation *validation.Validation) { @@ -25,12 +26,12 @@ func (in *CreateRuleCommand) Valid(validation *validation.Validation) {
25 return 26 return
26 } 27 }
27 28
28 - if len(in.Name) > 40 { 29 + if utf8.RuneCountInString(in.Name) > 40 {
29 validation.SetError("name", "名称最大长度40个字符") 30 validation.SetError("name", "名称最大长度40个字符")
30 return 31 return
31 } 32 }
32 33
33 - if len(in.Remark) > 100 { 34 + if utf8.RuneCountInString(in.Remark) > 100 {
34 validation.SetError("remark", "备注不能超过100个字符") 35 validation.SetError("remark", "备注不能超过100个字符")
35 return 36 return
36 } 37 }
@@ -3,6 +3,7 @@ package command @@ -3,6 +3,7 @@ package command
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  6 + "unicode/utf8"
6 ) 7 )
7 8
8 type UpdateRuleCommand struct { 9 type UpdateRuleCommand struct {
@@ -21,12 +22,12 @@ func (in *UpdateRuleCommand) Valid(validation *validation.Validation) { @@ -21,12 +22,12 @@ func (in *UpdateRuleCommand) Valid(validation *validation.Validation) {
21 return 22 return
22 } 23 }
23 24
24 - if len(in.Name) > 40 { 25 + if utf8.RuneCountInString(in.Name) > 40 {
25 validation.SetError("name", "名称最大长度40个字符") 26 validation.SetError("name", "名称最大长度40个字符")
26 return 27 return
27 } 28 }
28 29
29 - if len(in.Remark) > 100 { 30 + if utf8.RuneCountInString(in.Remark) > 100 {
30 validation.SetError("remark", "备注不能超过100个字符") 31 validation.SetError("remark", "备注不能超过100个字符")
31 return 32 return
32 } 33 }
@@ -2,6 +2,7 @@ package command @@ -2,6 +2,7 @@ package command
2 2
3 import ( 3 import (
4 "github.com/beego/beego/v2/core/validation" 4 "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
5 ) 6 )
6 7
7 type CreateTemplateCommand struct { 8 type CreateTemplateCommand struct {
@@ -21,12 +22,13 @@ func (in *CreateTemplateCommand) Valid(validation *validation.Validation) { @@ -21,12 +22,13 @@ func (in *CreateTemplateCommand) Valid(validation *validation.Validation) {
21 return 22 return
22 } 23 }
23 24
24 - if len(in.Name) > 40 { 25 + // 这个才是真正计算字符串文字长度的 utf8.RuneCountInString(in.Name)
  26 + if utf8.RuneCountInString(in.Name) > 40 {
25 validation.SetError("name", "模板名称最大长度40个字符") 27 validation.SetError("name", "模板名称最大长度40个字符")
26 return 28 return
27 } 29 }
28 30
29 - if len(in.Describe) > 100 { 31 + if utf8.RuneCountInString(in.Describe) > 100 {
30 validation.SetError("describe", "备注最大长度100个字符") 32 validation.SetError("describe", "备注最大长度100个字符")
31 return 33 return
32 } 34 }
@@ -20,15 +20,15 @@ func (in *UpdateTemplateCommand) Valid(validation *validation.Validation) { @@ -20,15 +20,15 @@ func (in *UpdateTemplateCommand) Valid(validation *validation.Validation) {
20 return 20 return
21 } 21 }
22 22
23 - if len(in.Name) > 40 {  
24 - validation.SetError("name", "模板名称最大长度40个字符")  
25 - return  
26 - }  
27 -  
28 - if len(in.Describe) > 100 {  
29 - validation.SetError("describe", "备注最大长度100个字符")  
30 - return  
31 - } 23 + //if len(in.Name) > 40 {
  24 + // validation.SetError("name", "模板名称最大长度40个字符")
  25 + // return
  26 + //}
  27 + //
  28 + //if len(in.Describe) > 100 {
  29 + // validation.SetError("describe", "备注最大长度100个字符")
  30 + // return
  31 + //}
32 32
33 if len(in.LinkNodes) == 0 { 33 if len(in.LinkNodes) == 0 {
34 validation.SetError("linkNodes", "评估模板流程不能为空") 34 validation.SetError("linkNodes", "评估模板流程不能为空")
1 package command 1 package command
2 2
3 -import "github.com/beego/beego/v2/core/validation" 3 +import (
  4 + "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
  6 +)
4 7
5 type CreateRoleCommand struct { 8 type CreateRoleCommand struct {
6 CompanyId int64 `cname:"公司ID" json:"companyId"` 9 CompanyId int64 `cname:"公司ID" json:"companyId"`
@@ -14,7 +17,7 @@ func (in *CreateRoleCommand) Valid(validation *validation.Validation) { @@ -14,7 +17,7 @@ func (in *CreateRoleCommand) Valid(validation *validation.Validation) {
14 return 17 return
15 } 18 }
16 19
17 - if len(in.Name) > 30 { 20 + if utf8.RuneCountInString(in.Name) > 30 {
18 validation.SetError("name", "角色名称最大长度30个字符") 21 validation.SetError("name", "角色名称最大长度30个字符")
19 return 22 return
20 } 23 }
1 package command 1 package command
2 2
3 -import "github.com/beego/beego/v2/core/validation" 3 +import (
  4 + "github.com/beego/beego/v2/core/validation"
  5 + "unicode/utf8"
  6 +)
4 7
5 type UpdateRoleCommand struct { 8 type UpdateRoleCommand struct {
6 CompanyId int64 `cname:"公司ID" json:"companyId"` 9 CompanyId int64 `cname:"公司ID" json:"companyId"`
@@ -14,7 +17,7 @@ func (in *UpdateRoleCommand) Valid(validation *validation.Validation) { @@ -14,7 +17,7 @@ func (in *UpdateRoleCommand) Valid(validation *validation.Validation) {
14 validation.SetError("companyId", "公司ID无效") 17 validation.SetError("companyId", "公司ID无效")
15 } 18 }
16 19
17 - if len(in.Name) > 30 { 20 + if utf8.RuneCountInString(in.Name) > 30 {
18 validation.SetError("name", "角色名称最大长度30个字符") 21 validation.SetError("name", "角色名称最大长度30个字符")
19 return 22 return
20 } 23 }