mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #8305 from zexi/hotfix/remove-utf8-stringutils
stringutils2: add RemoveUtf8Strings func
This commit is contained in:
@@ -107,12 +107,7 @@ func (manager *SDomainizedResourceBaseManager) ListItemFilter(
|
||||
) (*sqlchemy.SQuery, error) {
|
||||
if len(query.ProjectDomainIds) > 0 {
|
||||
// make sure ids are not utf8 string
|
||||
idList := make([]string, 0)
|
||||
for _, pid := range query.ProjectDomainIds {
|
||||
if !stringutils2.IsUtf8(pid) {
|
||||
idList = append(idList, pid)
|
||||
}
|
||||
}
|
||||
idList := stringutils2.RemoveUtf8Strings(query.ProjectDomainIds)
|
||||
tenants := TenantCacheManager.GetDomainQuery().SubQuery()
|
||||
subq := tenants.Query(tenants.Field("id")).Filter(sqlchemy.OR(
|
||||
sqlchemy.In(tenants.Field("id"), idList),
|
||||
|
||||
@@ -102,12 +102,7 @@ func (manager *SProjectizedResourceBaseManager) ListItemFilter(
|
||||
}
|
||||
if len(query.ProjectIds) > 0 {
|
||||
// make sure ids are not utf8 string
|
||||
idList := make([]string, 0)
|
||||
for _, pid := range query.ProjectIds {
|
||||
if !stringutils2.IsUtf8(pid) {
|
||||
idList = append(idList, pid)
|
||||
}
|
||||
}
|
||||
idList := stringutils2.RemoveUtf8Strings(query.ProjectIds)
|
||||
tenants := TenantCacheManager.GetTenantQuery().SubQuery()
|
||||
subq := tenants.Query(tenants.Field("id")).Filter(sqlchemy.OR(
|
||||
sqlchemy.In(tenants.Field("id"), idList),
|
||||
|
||||
@@ -518,7 +518,7 @@ func _managedResourceFilterByAccount(managerIdFieldName string, q *sqlchemy.SQue
|
||||
if len(cloudaccountArr) > 0 {
|
||||
cpq := CloudaccountManager.Query().SubQuery()
|
||||
subcpq := cpq.Query(cpq.Field("id")).Filter(sqlchemy.OR(
|
||||
sqlchemy.In(cpq.Field("id"), cloudaccountArr),
|
||||
sqlchemy.In(cpq.Field("id"), stringutils2.RemoveUtf8Strings(cloudaccountArr)),
|
||||
sqlchemy.In(cpq.Field("name"), cloudaccountArr),
|
||||
)).SubQuery()
|
||||
subq := CloudproviderManager.Query("id").In("cloudaccount_id", subcpq).SubQuery()
|
||||
|
||||
@@ -133,12 +133,7 @@ func (manager *SIdentityBaseResourceManager) ListItemFilter(
|
||||
// override manager.SDomainizedResourceBaseManager.ListItemFilter()
|
||||
if len(query.ProjectDomainIds) > 0 {
|
||||
// make sure ids are not utf8 string
|
||||
idList := make([]string, 0)
|
||||
for _, pid := range query.ProjectDomainIds {
|
||||
if !stringutils2.IsUtf8(pid) {
|
||||
idList = append(idList, pid)
|
||||
}
|
||||
}
|
||||
idList := stringutils2.RemoveUtf8Strings(query.ProjectDomainIds)
|
||||
domains := DomainManager.Query().SubQuery()
|
||||
subq := domains.Query(domains.Field("id")).Filter(sqlchemy.OR(
|
||||
sqlchemy.In(domains.Field("id"), idList),
|
||||
|
||||
@@ -23,6 +23,16 @@ func IsUtf8(str string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func RemoveUtf8Strings(idOrNames []string) []string {
|
||||
ids := make([]string, 0)
|
||||
for _, idOrName := range idOrNames {
|
||||
if !IsUtf8(idOrName) {
|
||||
ids = append(ids, idOrName)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func IsPrintableAscii(b byte) bool {
|
||||
if b >= 32 && b <= 126 {
|
||||
return true
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package stringutils2
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -34,6 +35,31 @@ func TestIsUtf8(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveUtf8Strings(t *testing.T) {
|
||||
cases := []struct {
|
||||
in []string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
in: []string{},
|
||||
want: []string{},
|
||||
},
|
||||
{
|
||||
in: []string{"en", "中文"},
|
||||
want: []string{"en"},
|
||||
},
|
||||
{
|
||||
in: []string{"中文"},
|
||||
want: []string{},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := RemoveUtf8Strings(c.in); !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("RemoveUtf8Strings %s got %v want %v", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPrintableAscii(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
|
||||
Reference in New Issue
Block a user