Skip to content

Commit c78b9d7

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

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

pkg/filter/operator.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,12 @@ func (o DatetimeLessOp) ToMgo(field string, value interface{}) (map[string]inter
706706
if err != nil {
707707
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
708708
}
709-
709+
timeType, _ := util.IsTime(value)
710+
if timeType == util.DateOnlyType {
711+
return mapstr.MapStr{
712+
field: map[string]interface{}{common.BKDBLTE: value},
713+
}, nil
714+
}
710715
return mapstr.MapStr{
711716
field: map[string]interface{}{common.BKDBLT: v},
712717
}, nil
@@ -748,6 +753,12 @@ func (o DatetimeLessOrEqualOp) ToMgo(field string, value interface{}) (map[strin
748753
if err != nil {
749754
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
750755
}
756+
timeType, _ := util.IsTime(value)
757+
if timeType == util.DateOnlyType {
758+
return mapstr.MapStr{
759+
field: map[string]interface{}{common.BKDBLTE: value},
760+
}, nil
761+
}
751762

752763
return mapstr.MapStr{
753764
field: map[string]interface{}{common.BKDBLTE: v},
@@ -791,6 +802,12 @@ func (o DatetimeGreaterOp) ToMgo(field string, value interface{}) (map[string]in
791802
if err != nil {
792803
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
793804
}
805+
timeType, _ := util.IsTime(value)
806+
if timeType == util.DateOnlyType {
807+
return mapstr.MapStr{
808+
field: map[string]interface{}{common.BKDBGT: value},
809+
}, nil
810+
}
794811

795812
return mapstr.MapStr{
796813
field: map[string]interface{}{common.BKDBGT: v},
@@ -833,6 +850,12 @@ func (o DatetimeGreaterOrEqualOp) ToMgo(field string, value interface{}) (map[st
833850
if err != nil {
834851
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
835852
}
853+
timeType, _ := util.IsTime(value)
854+
if timeType == util.DateOnlyType {
855+
return mapstr.MapStr{
856+
field: map[string]interface{}{common.BKDBGTE: value},
857+
}, nil
858+
}
836859

837860
return mapstr.MapStr{
838861
field: map[string]interface{}{common.BKDBGTE: v},

src/common/util/struti.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const (
9090
// timeWithLocationType the date time type compatible for values from db which is marshaled with time zone
9191
timeWithLocationType DateTimeFieldType = "time_with_location"
9292
invalidDateTimeType DateTimeFieldType = "invalid"
93+
94+
DateOnlyType DateTimeFieldType = "date_only"
9395
)
9496

9597
// IsTime 是否是时间类型
@@ -102,6 +104,9 @@ func IsTime(sInput interface{}) (DateTimeFieldType, bool) {
102104
if timeWithLocationRegexp.MatchString(val) {
103105
return timeWithLocationType, true
104106
}
107+
if dateRegexp.MatchString(val) {
108+
return DateOnlyType, true
109+
}
105110
return invalidDateTimeType, false
106111
default:
107112
return invalidDateTimeType, false
@@ -134,6 +139,8 @@ func Str2Time(timeStr string, timeType DateTimeFieldType) time.Time {
134139
layout = "2006-01-02 15:04:05"
135140
case timeWithLocationType:
136141
layout = "2006-01-02T15:04:05Z07:00"
142+
case DateOnlyType:
143+
layout = time.DateOnly
137144
default:
138145
return time.Time{}
139146
}

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+
objID any
55+
}
56+
now := time.Now()
57+
tests := []struct {
58+
name string
59+
args args
60+
}{
61+
{"", args{"2024-00-00"}}, //TODO 不符合日期格式,但能通过正则
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.objID); err != nil {
69+
t.Errorf("ValidateDatetimeType() = %v:%v", tt.args, err)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)