package controller import ( "git.cestong.com.cn/cecf/cecf-golib/pkg/errors" "git.cestong.com.cn/cecf/cecf-golib/pkg/http" "git.cestong.com.cn/cecf/cecf-golib/pkg/validate" "git.cestong.com.cn/cecf/config-center-server/pkg/model" "github.com/gin-gonic/gin" log "github.com/go-admin-team/go-admin-core/logger" "github.com/go-playground/validator/v10" "gorm.io/gorm" ) type ValueConstraintController struct { db *gorm.DB val *validator.Validate executeEngineMap map[string]validate.ExecuteEngine } func NewValueConstraintController(db *gorm.DB, val *validator.Validate, eeMap map[string]validate.ExecuteEngine) *ValueConstraintController { return &ValueConstraintController{ db: db, val: val, executeEngineMap: eeMap, } } func (v *ValueConstraintController) AddExecuteEngine(name string, engine validate.ExecuteEngine) *ValueConstraintController { v.executeEngineMap[name] = engine return v } type ListValueConstraintRespVO struct { ConstraintList []ListConstraintItemRespVO `json:"constraintList"` } type ListConstraintItemRespVO struct { ID uint `json:"id"` Name string `json:"name"` Desc string `json:"desc"` Script string `json:"script"` ExecuteEngine string `json:"executeEngine"` } var orderByFieldToColName = map[string]string{ "createTime": "created_at", "id": "id", } func (v *ValueConstraintController) List(c *gin.Context) (interface{}, error) { var pageQueryParams http.PageQueryParams if errParse := c.ShouldBindQuery(&pageQueryParams); errParse != nil { return nil, errParse } if errVal := pageQueryParams.SetDefault(orderByFieldToColName).Validate().Error(); errVal != nil { return nil, errors.Wrap(errors.ErrBadRequest, errVal.Error()) } var cs []model.ValueConstraintModel if errFind := v.db.Limit(pageQueryParams.GetLimit()).Offset(pageQueryParams.GetOffset()). Order(pageQueryParams.OrderByString()).Find(&cs).Error; errFind != nil { return nil, errFind } var resp ListValueConstraintRespVO resp.ConstraintList = make([]ListConstraintItemRespVO, 0, len(cs)) for _, c := range cs { var it ListConstraintItemRespVO it.ID = c.ID it.Name = c.Name it.Script = c.Script it.Desc = c.Desc it.ExecuteEngine = c.ExecuteEngine resp.ConstraintList = append(resp.ConstraintList, it) } return resp, nil } type ValueTestReqVO struct { ConstraintIDList []uint `json:"constraintIdList" validate:"min=1"` Value string `json:"value"` } type ValueTestRespVO struct { Match bool `json:"match"` ErrMsgs []string `json:"errMsgs"` } func (v *ValueConstraintController) ValueTest(c *gin.Context) (interface{}, error) { var req ValueTestReqVO if errParse := c.ShouldBindJSON(&req); errParse != nil { return nil, errors.ErrBadRequest } if errVal := v.val.Struct(req); errVal != nil { return nil, errVal } var cs []model.ValueConstraintModel if err := v.db.Find(&cs, req.ConstraintIDList).Error; err != nil { return nil, err } var resp ValueTestRespVO for _, cm := range cs { ee, find := v.executeEngineMap[cm.ExecuteEngine] if !find { log.Errorf("no ExecuteEngine for type:%s", cm.ExecuteEngine) continue } if errMatch := ee.Validate(cm.Script, req.Value); errMatch != nil { if !errors.Is(errMatch, validate.ErrConstraintNotMatch) { return nil, errMatch } // not match resp.ErrMsgs = append(resp.ErrMsgs, errMatch.Error()) } } resp.Match = len(resp.ErrMsgs) == 0 return resp, nil }