fix: tag filter with no value revisit

This commit is contained in:
Qiu Jian
2024-03-12 21:43:36 +08:00
parent 6313ce87a3
commit 006174bd2b
3 changed files with 39 additions and 148 deletions
+2 -38
View File
@@ -30,48 +30,12 @@ func splitValues(values []string) (bool, []string) {
}
}
func (ts TTagSet) toFilters() (map[string][]string, map[string][]string) {
filter := tagset2Map(ts)
pos := make(map[string][]string)
neg := make(map[string][]string)
negExist := false
for k, v := range filter {
noval, values := splitValues(v)
if noval {
negExist = true
if len(values) > 0 {
pos[k] = values
}
neg[k] = []string{}
} else {
pos[k] = values
neg[k] = values
}
}
if !negExist {
neg = nil
}
return pos, neg
}
func (tf *STagFilters) AddFilter(ts TTagSet) {
pos, neg := ts.toFilters()
if pos != nil {
tf.Filters = append(tf.Filters, pos)
}
if neg != nil {
tf.NoFilters = append(tf.NoFilters, neg)
}
tf.Filters = append(tf.Filters, tagset2Map(ts))
}
func (tf *STagFilters) AddNoFilter(ts TTagSet) {
pos, neg := ts.toFilters()
if pos != nil {
tf.NoFilters = append(tf.NoFilters, pos)
}
if neg != nil {
tf.Filters = append(tf.Filters, neg)
}
tf.NoFilters = append(tf.NoFilters, tagset2Map(ts))
}
func (tf *STagFilters) AddFilters(tsl TTagSetList) {
-89
View File
@@ -1,89 +0,0 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tagutils
import (
"reflect"
"testing"
"yunion.io/x/jsonutils"
)
func TestTagSetToFilters(t *testing.T) {
cases := []struct {
ts TTagSet
pos map[string][]string
neg map[string][]string
}{
{
ts: TTagSet{
STag{
Key: "project",
Value: "a",
},
},
pos: map[string][]string{
"project": {
"a",
},
},
neg: nil,
},
{
ts: TTagSet{
STag{
Key: "project",
Value: NoValue,
},
},
pos: map[string][]string{},
neg: map[string][]string{
"project": {},
},
},
{
ts: TTagSet{
STag{
Key: "env",
Value: "c",
},
STag{
Key: "project",
Value: NoValue,
},
},
pos: map[string][]string{
"env": {
"c",
},
},
neg: map[string][]string{
"env": {
"c",
},
"project": {},
},
},
}
for i, c := range cases {
gotPos, gotNeg := c.ts.toFilters()
if !reflect.DeepEqual(c.pos, gotPos) {
t.Errorf("[%d] filters want: %s got: %s", i, jsonutils.Marshal(c.pos), jsonutils.Marshal(gotPos))
}
if !reflect.DeepEqual(c.neg, gotNeg) {
t.Errorf("[%d] nofilters want %s got %s", i, jsonutils.Marshal(c.neg), jsonutils.Marshal(gotNeg))
}
}
}