Skip to content

Commit 924ffcf

Browse files
committed
fix:修复通过日期字段查询模型实例 --bug=148529573 【CMDB】模型通过日期字段查询报错
1 parent 5be097d commit 924ffcf

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

pkg/filter/operator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,11 @@ func (o DatetimeLessOp) ToMgo(field string, value interface{}) (map[string]inter
702702
return nil, errors.New("field is empty")
703703
}
704704

705+
if util.IsDate(value) {
706+
return mapstr.MapStr{
707+
field: map[string]interface{}{common.BKDBLT: value},
708+
}, nil
709+
}
705710
v, err := util.ConvToTime(value)
706711
if err != nil {
707712
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
@@ -744,6 +749,11 @@ func (o DatetimeLessOrEqualOp) ToMgo(field string, value interface{}) (map[strin
744749
return nil, errors.New("field is empty")
745750
}
746751

752+
if util.IsDate(value) {
753+
return mapstr.MapStr{
754+
field: map[string]interface{}{common.BKDBLTE: value},
755+
}, nil
756+
}
747757
v, err := util.ConvToTime(value)
748758
if err != nil {
749759
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
@@ -787,6 +797,11 @@ func (o DatetimeGreaterOp) ToMgo(field string, value interface{}) (map[string]in
787797
return nil, errors.New("field is empty")
788798
}
789799

800+
if util.IsDate(value) {
801+
return mapstr.MapStr{
802+
field: map[string]interface{}{common.BKDBGT: value},
803+
}, nil
804+
}
790805
v, err := util.ConvToTime(value)
791806
if err != nil {
792807
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
@@ -829,6 +844,11 @@ func (o DatetimeGreaterOrEqualOp) ToMgo(field string, value interface{}) (map[st
829844
return nil, errors.New("field is empty")
830845
}
831846

847+
if util.IsDate(value) {
848+
return mapstr.MapStr{
849+
field: map[string]interface{}{common.BKDBGTE: value},
850+
}, nil
851+
}
832852
v, err := util.ConvToTime(value)
833853
if err != nil {
834854
return nil, fmt.Errorf("convert value to time failed, err: %v", err)

src/common/util/struti.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ func IsDate(sInput interface{}) bool {
7575
if len(val) == 0 {
7676
return false
7777
}
78-
return dateRegexp.MatchString(val)
78+
if _, err := time.Parse(time.DateOnly, val); err == nil {
79+
return true
80+
}
81+
return false
7982
default:
8083
return false
8184
}

src/common/valid/valid.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func ValidateDatetimeType(value interface{}) error {
9696
return nil
9797
}
9898

99+
if util.IsDate(value) {
100+
return nil
101+
}
102+
99103
// string type with time format is supported
100104
if _, ok := util.IsTime(value); ok {
101105
return nil

src/common/valid/valid_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package valid
1818

1919
import (
2020
"testing"
21+
"time"
2122

2223
"configcenter/src/common"
2324
)
@@ -47,3 +48,28 @@ func TestIsInnerObject(t *testing.T) {
4748
})
4849
}
4950
}
51+
52+
func TestValidateDatetimeType(t *testing.T) {
53+
type args struct {
54+
timeStr any
55+
}
56+
now := time.Now()
57+
tests := []struct {
58+
name string
59+
args args
60+
wantErr bool
61+
}{
62+
{"illegal date", args{"2024-00-00"}, true}, //should err
63+
{"DateOnly", args{now.Format(time.DateOnly)}, false},
64+
{"DateTime", args{now.Format(time.DateTime)}, false},
65+
{"RFC3339", args{now.Format(time.RFC3339)}, false},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
err := ValidateDatetimeType(tt.args.timeStr)
70+
if (err != nil) != tt.wantErr {
71+
t.Errorf("ValidateDatetimeType() = %v:%v", tt.args, err)
72+
}
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)