value_constraint.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package controller
  2. import (
  3. "git.cestong.com.cn/cecf/cecf-golib/pkg/errors"
  4. "git.cestong.com.cn/cecf/cecf-golib/pkg/http"
  5. "git.cestong.com.cn/cecf/cecf-golib/pkg/validate"
  6. "git.cestong.com.cn/cecf/config-center-server/pkg/model"
  7. "github.com/gin-gonic/gin"
  8. log "github.com/go-admin-team/go-admin-core/logger"
  9. "github.com/go-playground/validator/v10"
  10. "gorm.io/gorm"
  11. )
  12. type ValueConstraintController struct {
  13. db *gorm.DB
  14. val *validator.Validate
  15. executeEngineMap map[string]validate.ExecuteEngine
  16. }
  17. func NewValueConstraintController(db *gorm.DB, val *validator.Validate, eeMap map[string]validate.ExecuteEngine) *ValueConstraintController {
  18. return &ValueConstraintController{
  19. db: db,
  20. val: val,
  21. executeEngineMap: eeMap,
  22. }
  23. }
  24. func (v *ValueConstraintController) AddExecuteEngine(name string, engine validate.ExecuteEngine) *ValueConstraintController {
  25. v.executeEngineMap[name] = engine
  26. return v
  27. }
  28. type ListValueConstraintRespVO struct {
  29. ConstraintList []ListConstraintItemRespVO `json:"constraintList"`
  30. }
  31. type ListConstraintItemRespVO struct {
  32. ID uint `json:"id"`
  33. Name string `json:"name"`
  34. Desc string `json:"desc"`
  35. Script string `json:"script"`
  36. ExecuteEngine string `json:"executeEngine"`
  37. }
  38. var orderByFieldToColName = map[string]string{
  39. "createTime": "created_at",
  40. "id": "id",
  41. }
  42. func (v *ValueConstraintController) List(c *gin.Context) (interface{}, error) {
  43. var pageQueryParams http.PageQueryParams
  44. if errParse := c.ShouldBindQuery(&pageQueryParams); errParse != nil {
  45. return nil, errParse
  46. }
  47. if errVal := pageQueryParams.SetDefault(orderByFieldToColName).Validate().Error(); errVal != nil {
  48. return nil, errors.Wrap(errors.ErrBadRequest, errVal.Error())
  49. }
  50. var cs []model.ValueConstraintModel
  51. if errFind := v.db.Limit(pageQueryParams.GetLimit()).Offset(pageQueryParams.GetOffset()).
  52. Order(pageQueryParams.OrderByString()).Find(&cs).Error; errFind != nil {
  53. return nil, errFind
  54. }
  55. var resp ListValueConstraintRespVO
  56. resp.ConstraintList = make([]ListConstraintItemRespVO, 0, len(cs))
  57. for _, c := range cs {
  58. var it ListConstraintItemRespVO
  59. it.ID = c.ID
  60. it.Name = c.Name
  61. it.Script = c.Script
  62. it.Desc = c.Desc
  63. it.ExecuteEngine = c.ExecuteEngine
  64. resp.ConstraintList = append(resp.ConstraintList, it)
  65. }
  66. return resp, nil
  67. }
  68. type ValueTestReqVO struct {
  69. ConstraintIDList []uint `json:"constraintIdList" validate:"min=1"`
  70. Value string `json:"value"`
  71. }
  72. type ValueTestRespVO struct {
  73. Match bool `json:"match"`
  74. ErrMsgs []string `json:"errMsgs"`
  75. }
  76. func (v *ValueConstraintController) ValueTest(c *gin.Context) (interface{}, error) {
  77. var req ValueTestReqVO
  78. if errParse := c.ShouldBindJSON(&req); errParse != nil {
  79. return nil, errors.ErrBadRequest
  80. }
  81. if errVal := v.val.Struct(req); errVal != nil {
  82. return nil, errVal
  83. }
  84. var cs []model.ValueConstraintModel
  85. if err := v.db.Find(&cs, req.ConstraintIDList).Error; err != nil {
  86. return nil, err
  87. }
  88. var resp ValueTestRespVO
  89. for _, cm := range cs {
  90. ee, find := v.executeEngineMap[cm.ExecuteEngine]
  91. if !find {
  92. log.Errorf("no ExecuteEngine for type:%s", cm.ExecuteEngine)
  93. continue
  94. }
  95. if errMatch := ee.Validate(cm.Script, req.Value); errMatch != nil {
  96. if !errors.Is(errMatch, validate.ErrConstraintNotMatch) {
  97. return nil, errMatch
  98. }
  99. // not match
  100. resp.ErrMsgs = append(resp.ErrMsgs, errMatch.Error())
  101. }
  102. }
  103. resp.Match = len(resp.ErrMsgs) == 0
  104. return resp, nil
  105. }