This is an automated email from the ASF dual-hosted git repository.

tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new e97cd40  添加创建业务场景未对alias字段校验,将对methods校验修改为method (#1016)
e97cd40 is described below

commit e97cd40079e6b3f3b62e304ae5a612ed198a1448
Author: 朱程成 <[email protected]>
AuthorDate: Mon May 31 14:11:06 2021 +0800

    添加创建业务场景未对alias字段校验,将对methods校验修改为method (#1016)
    
    * 添加创建业务场景未对alias字段校验,将对methods校验修改为method
    
    * 添加创建业务场景未对alias字段校验,将对methods校验修改为method
    
    * 添加创建业务场景未对alias字段校验,将对methods校验修改为method
    
    * 添加创建业务场景未对alias字段校验,将对methods校验修改为method
---
 server/service/gov/kie/kie_distributor.go | 14 ++++++++++++++
 server/service/gov/kie/validate.go        | 18 +++++++++++-------
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/server/service/gov/kie/kie_distributor.go 
b/server/service/gov/kie/kie_distributor.go
index 4533a1d..4193546 100644
--- a/server/service/gov/kie/kie_distributor.go
+++ b/server/service/gov/kie/kie_distributor.go
@@ -51,6 +51,10 @@ const (
        KeyApp          = "app"
        KeyEnvironment  = "environment"
        EnvAll          = "all"
+       Alias           = "alias"
+       Method          = "method"
+       Matches         = "matches"
+       Rules           = "rules"
 )
 
 var PolicyNames = []string{"retry", "rateLimiting", "circuitBreaker", 
"bulkhead"}
@@ -76,6 +80,7 @@ func (d *Distributor) Create(kind, project string, spec 
[]byte) ([]byte, error)
        if err != nil {
                return nil, err
        }
+       setAliasIfEmpty(p.Spec, p.Name)
        yamlByte, err := yaml.Marshal(p.Spec)
        if err != nil {
                return nil, err
@@ -106,6 +111,7 @@ func (d *Distributor) Update(kind, id, project string, spec 
[]byte) error {
        if err != nil {
                return err
        }
+       setAliasIfEmpty(p.Spec, p.Name)
        yamlByte, err := yaml.Marshal(p.Spec)
        if err != nil {
                return err
@@ -211,6 +217,14 @@ func (d *Distributor) Display(project, app, env string) 
([]byte, error) {
        return b, nil
 }
 
+func setAliasIfEmpty(val interface{}, name string) {
+       spec := val.(map[string]interface{})
+       alias := spec["alias"].(string)
+       if alias == "" {
+               spec["alias"] = name
+       }
+}
+
 func (d *Distributor) List(kind, project, app, env string) ([]byte, error) {
        list, _, err := d.listDataByKind(kind, project, app, env)
        if err != nil {
diff --git a/server/service/gov/kie/validate.go 
b/server/service/gov/kie/validate.go
index 2423d2f..768abf7 100644
--- a/server/service/gov/kie/validate.go
+++ b/server/service/gov/kie/validate.go
@@ -62,10 +62,14 @@ func matchValidate(val interface{}) error {
        if !ok {
                return &ErrIllegalItem{"can not cast to map", val}
        }
-       if spec["matches"] == nil {
+       if spec[Matches] == nil {
                return nil
        }
-       matches, ok := spec["matches"].([]interface{})
+       alias, ok := spec[Alias].(string)
+       if !ok {
+               return &ErrIllegalItem{"alias must be string", alias}
+       }
+       matches, ok := spec[Matches].([]interface{})
        if !ok {
                return &ErrIllegalItem{"don't have matches", spec}
        }
@@ -77,12 +81,12 @@ func matchValidate(val interface{}) error {
                if match["name"] == nil {
                        return &ErrIllegalItem{"match's name can not be null", 
match}
                }
-               if match["apiPath"] == nil && match["headers"] == nil && 
match["methods"] == nil {
+               if match["apiPath"] == nil && match["headers"] == nil && 
match[Method] == nil {
                        return &ErrIllegalItem{"match must have a match item 
[apiPath/headers/methods]", match}
                }
                //apiPath & headers do not check
-               if match["methods"] != nil {
-                       methods, ok := match["methods"].([]interface{})
+               if match[Method] != nil {
+                       methods, ok := match[Method].([]interface{})
                        if !ok {
                                return &ErrIllegalItem{"methods must be a 
list", match}
                        }
@@ -121,8 +125,8 @@ func policyValidate(val interface{}) error {
        if !ok {
                return &ErrIllegalItem{"policy can not cast to map", val}
        }
-       if spec["rules"] != nil {
-               rules, ok := spec["rules"].(map[string]interface{})
+       if spec[Rules] != nil {
+               rules, ok := spec[Rules].(map[string]interface{})
                if !ok {
                        return &ErrIllegalItem{"policy's rules can not cast to 
map", spec}
                }

Reply via email to