diff --git a/condition.go b/condition.go index 2395490..9b78223 100644 --- a/condition.go +++ b/condition.go @@ -4,9 +4,9 @@ import ( "context" "encoding/json" "net/http" + "slices" "strings" - "git.nobla.cn/golang/kos/util/arrays" "git.nobla.cn/golang/rest/types" ) @@ -31,7 +31,7 @@ func BuildConditions(ctx context.Context, r *http.Request, query *Query, schemas return } } - if arrays.Exists(r.Method, []string{http.MethodPut, http.MethodPost}) { + if slices.Contains(allowMethods, r.Method) { conditions := make([]*types.Condition, 0) if err = json.NewDecoder(r.Body).Decode(&conditions); err != nil { return diff --git a/model.go b/model.go index 82f92cf..4abd4db 100644 --- a/model.go +++ b/model.go @@ -5,13 +5,6 @@ import ( "encoding/csv" "encoding/json" "fmt" - "git.nobla.cn/golang/kos/util/arrays" - "git.nobla.cn/golang/kos/util/pool" - "git.nobla.cn/golang/rest/inflector" - "git.nobla.cn/golang/rest/types" - "gorm.io/gorm" - "gorm.io/gorm/clause" - "gorm.io/gorm/schema" "io" "mime/multipart" "net/http" @@ -19,9 +12,17 @@ import ( "os" "path" "reflect" + "slices" "strconv" "strings" "time" + + "git.nobla.cn/golang/kos/util/pool" + "git.nobla.cn/golang/rest/inflector" + "git.nobla.cn/golang/rest/types" + "gorm.io/gorm" + "gorm.io/gorm/clause" + "gorm.io/gorm/schema" ) type Model struct { @@ -325,7 +326,7 @@ func (m *Model) Search(w http.ResponseWriter, r *http.Request) { return } scenario = types.ScenarioList - if arrays.Exists(r.FormValue("scenario"), allowScenario) { + if slices.Contains(allowScenario, r.FormValue("scenario")) { scenario = r.FormValue("scenario") } if listSchemas, err = m.schemaLookup(childCtx, m.getDB(), domainName, m.naming.ModuleName, m.naming.TableName, scenario); err != nil { diff --git a/rest.go b/rest.go index 96affda..ad52819 100644 --- a/rest.go +++ b/rest.go @@ -6,11 +6,11 @@ import ( "fmt" "net/http" "reflect" + "slices" "strconv" "strings" "time" - "git.nobla.cn/golang/kos/util/arrays" "git.nobla.cn/golang/kos/util/reflection" "git.nobla.cn/golang/rest/inflector" "git.nobla.cn/golang/rest/types" @@ -167,12 +167,12 @@ func fieldRule(field *schema.Field) types.Rule { if ls > 1 { bs := strings.Split(vs[1], ",") for _, i := range bs { - if arrays.Exists(i, []string{types.ScenarioCreate, types.ScenarioUpdate}) { + if slices.Contains(requiredScenario, i) { r.Required = append(r.Required, i) } } } else { - r.Required = []string{types.ScenarioCreate, types.ScenarioUpdate} + r.Required = requiredScenario } case "unique": r.Unique = true @@ -257,7 +257,7 @@ func fieldAttribute(field *schema.Field) types.Attribute { case "icon": attr.Icon = sv case "match": - if arrays.Exists(sv, matchEnums) { + if slices.Contains(matchEnums, sv) { attr.Match = sv } case "endofnow", "end_of_now": @@ -281,7 +281,7 @@ func fieldAttribute(field *schema.Field) types.Attribute { case "readonly": bs := strings.Split(sv, ",") for _, i := range bs { - if arrays.Exists(i, []string{types.ScenarioCreate, types.ScenarioUpdate}) { + if slices.Contains(readonlyScenario, i) { attr.Readonly = append(attr.Readonly, i) } } diff --git a/types.go b/types.go index 903f188..4d50a79 100644 --- a/types.go +++ b/types.go @@ -3,8 +3,10 @@ package rest import ( "context" "encoding/json" - "gorm.io/gorm" "net/http" + + "git.nobla.cn/golang/rest/types" + "gorm.io/gorm" ) const ( @@ -12,6 +14,13 @@ const ( defaultDomain = "localhost" ) +var ( + readonlyScenario = []string{types.ScenarioCreate, types.ScenarioUpdate} + requiredScenario = []string{types.ScenarioCreate, types.ScenarioUpdate} + + allowMethods = []string{http.MethodPut, http.MethodPost} +) + type ( httpWriter struct { } diff --git a/utils.go b/utils.go index 4a5fc30..262be11 100644 --- a/utils.go +++ b/utils.go @@ -1,8 +1,8 @@ package rest import ( - "git.nobla.cn/golang/kos/util/arrays" "reflect" + "slices" "strings" ) @@ -13,7 +13,7 @@ func hasToken(hack string, need string) bool { char := []byte{',', ';'} for _, c := range char { if strings.IndexByte(need, c) > -1 { - return arrays.Exists(hack, strings.Split(need, string(c))) + return slices.Contains(strings.Split(need, string(c)), hack) } } return false