move arrays.exists to slices.Contains

This commit is contained in:
Yavolte 2025-06-10 17:05:52 +08:00
parent 7097610fad
commit 8de2f0e3a5
5 changed files with 28 additions and 18 deletions

View File

@ -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

View File

@ -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 {

10
rest.go
View File

@ -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)
}
}

View File

@ -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 {
}

View File

@ -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