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" "context"
"encoding/json" "encoding/json"
"net/http" "net/http"
"slices"
"strings" "strings"
"git.nobla.cn/golang/kos/util/arrays"
"git.nobla.cn/golang/rest/types" "git.nobla.cn/golang/rest/types"
) )
@ -31,7 +31,7 @@ func BuildConditions(ctx context.Context, r *http.Request, query *Query, schemas
return return
} }
} }
if arrays.Exists(r.Method, []string{http.MethodPut, http.MethodPost}) { if slices.Contains(allowMethods, r.Method) {
conditions := make([]*types.Condition, 0) conditions := make([]*types.Condition, 0)
if err = json.NewDecoder(r.Body).Decode(&conditions); err != nil { if err = json.NewDecoder(r.Body).Decode(&conditions); err != nil {
return return

View File

@ -5,13 +5,6 @@ import (
"encoding/csv" "encoding/csv"
"encoding/json" "encoding/json"
"fmt" "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" "io"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
@ -19,9 +12,17 @@ import (
"os" "os"
"path" "path"
"reflect" "reflect"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "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 { type Model struct {
@ -325,7 +326,7 @@ func (m *Model) Search(w http.ResponseWriter, r *http.Request) {
return return
} }
scenario = types.ScenarioList scenario = types.ScenarioList
if arrays.Exists(r.FormValue("scenario"), allowScenario) { if slices.Contains(allowScenario, r.FormValue("scenario")) {
scenario = 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 { 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" "fmt"
"net/http" "net/http"
"reflect" "reflect"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"git.nobla.cn/golang/kos/util/arrays"
"git.nobla.cn/golang/kos/util/reflection" "git.nobla.cn/golang/kos/util/reflection"
"git.nobla.cn/golang/rest/inflector" "git.nobla.cn/golang/rest/inflector"
"git.nobla.cn/golang/rest/types" "git.nobla.cn/golang/rest/types"
@ -167,12 +167,12 @@ func fieldRule(field *schema.Field) types.Rule {
if ls > 1 { if ls > 1 {
bs := strings.Split(vs[1], ",") bs := strings.Split(vs[1], ",")
for _, i := range bs { for _, i := range bs {
if arrays.Exists(i, []string{types.ScenarioCreate, types.ScenarioUpdate}) { if slices.Contains(requiredScenario, i) {
r.Required = append(r.Required, i) r.Required = append(r.Required, i)
} }
} }
} else { } else {
r.Required = []string{types.ScenarioCreate, types.ScenarioUpdate} r.Required = requiredScenario
} }
case "unique": case "unique":
r.Unique = true r.Unique = true
@ -257,7 +257,7 @@ func fieldAttribute(field *schema.Field) types.Attribute {
case "icon": case "icon":
attr.Icon = sv attr.Icon = sv
case "match": case "match":
if arrays.Exists(sv, matchEnums) { if slices.Contains(matchEnums, sv) {
attr.Match = sv attr.Match = sv
} }
case "endofnow", "end_of_now": case "endofnow", "end_of_now":
@ -281,7 +281,7 @@ func fieldAttribute(field *schema.Field) types.Attribute {
case "readonly": case "readonly":
bs := strings.Split(sv, ",") bs := strings.Split(sv, ",")
for _, i := range bs { for _, i := range bs {
if arrays.Exists(i, []string{types.ScenarioCreate, types.ScenarioUpdate}) { if slices.Contains(readonlyScenario, i) {
attr.Readonly = append(attr.Readonly, i) attr.Readonly = append(attr.Readonly, i)
} }
} }

View File

@ -3,8 +3,10 @@ package rest
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"gorm.io/gorm"
"net/http" "net/http"
"git.nobla.cn/golang/rest/types"
"gorm.io/gorm"
) )
const ( const (
@ -12,6 +14,13 @@ const (
defaultDomain = "localhost" defaultDomain = "localhost"
) )
var (
readonlyScenario = []string{types.ScenarioCreate, types.ScenarioUpdate}
requiredScenario = []string{types.ScenarioCreate, types.ScenarioUpdate}
allowMethods = []string{http.MethodPut, http.MethodPost}
)
type ( type (
httpWriter struct { httpWriter struct {
} }

View File

@ -1,8 +1,8 @@
package rest package rest
import ( import (
"git.nobla.cn/golang/kos/util/arrays"
"reflect" "reflect"
"slices"
"strings" "strings"
) )
@ -13,7 +13,7 @@ func hasToken(hack string, need string) bool {
char := []byte{',', ';'} char := []byte{',', ';'}
for _, c := range char { for _, c := range char {
if strings.IndexByte(need, c) > -1 { 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 return false