Skip to content

Commit 3d51cea

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

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

pkg/filter/operator.go

Lines changed: 24 additions & 3 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.BKDBLTE: 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,14 +749,20 @@ 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)
750760
}
751761

752762
return mapstr.MapStr{
753-
field: map[string]interface{}{common.BKDBLTE: v},
763+
field: map[string]interface{}{common.BKDBLT: v},
754764
}, nil
765+
755766
}
756767

757768
// Match checks if the first data matches the second data by this operator
@@ -787,13 +798,18 @@ func (o DatetimeGreaterOp) ToMgo(field string, value interface{}) (map[string]in
787798
return nil, errors.New("field is empty")
788799
}
789800

801+
if util.IsDate(value) {
802+
return mapstr.MapStr{
803+
field: map[string]interface{}{common.BKDBLTE: value},
804+
}, nil
805+
}
790806
v, err := util.ConvToTime(value)
791807
if err != nil {
792808
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
793809
}
794810

795811
return mapstr.MapStr{
796-
field: map[string]interface{}{common.BKDBGT: v},
812+
field: map[string]interface{}{common.BKDBLT: v},
797813
}, nil
798814
}
799815

@@ -829,13 +845,18 @@ func (o DatetimeGreaterOrEqualOp) ToMgo(field string, value interface{}) (map[st
829845
return nil, errors.New("field is empty")
830846
}
831847

848+
if util.IsDate(value) {
849+
return mapstr.MapStr{
850+
field: map[string]interface{}{common.BKDBLTE: value},
851+
}, nil
852+
}
832853
v, err := util.ConvToTime(value)
833854
if err != nil {
834855
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
835856
}
836857

837858
return mapstr.MapStr{
838-
field: map[string]interface{}{common.BKDBGTE: v},
859+
field: map[string]interface{}{common.BKDBLT: v},
839860
}, nil
840861
}
841862

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: 24 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,26 @@ 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+
}{
61+
{"", args{"2024-00-00"}}, //should err
62+
{"", args{now.Format(time.DateOnly)}},
63+
{"", args{now.Format(time.DateTime)}},
64+
{"", args{now.Format(time.RFC3339)}},
65+
}
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
if err := ValidateDatetimeType(tt.args.timeStr); err != nil {
69+
t.Errorf("ValidateDatetimeType() = %v:%v", tt.args, err)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)