Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/filter/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,11 @@ func (o DatetimeLessOp) ToMgo(field string, value interface{}) (map[string]inter
return nil, errors.New("field is empty")
}

if util.IsDate(value) {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBLT: value},
}, nil
}
v, err := util.ConvToTime(value)
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
Expand Down Expand Up @@ -744,6 +749,11 @@ func (o DatetimeLessOrEqualOp) ToMgo(field string, value interface{}) (map[strin
return nil, errors.New("field is empty")
}

if util.IsDate(value) {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBLTE: value},
}, nil
}
v, err := util.ConvToTime(value)
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
Expand Down Expand Up @@ -787,6 +797,11 @@ func (o DatetimeGreaterOp) ToMgo(field string, value interface{}) (map[string]in
return nil, errors.New("field is empty")
}

if util.IsDate(value) {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBGT: value},
}, nil
}
v, err := util.ConvToTime(value)
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
Expand Down Expand Up @@ -829,6 +844,11 @@ func (o DatetimeGreaterOrEqualOp) ToMgo(field string, value interface{}) (map[st
return nil, errors.New("field is empty")
}

if util.IsDate(value) {
return mapstr.MapStr{
field: map[string]interface{}{common.BKDBGTE: value},
}, nil
}
v, err := util.ConvToTime(value)
if err != nil {
return nil, fmt.Errorf("convert value to time failed, err: %v", err)
Expand Down
6 changes: 4 additions & 2 deletions src/common/util/struti.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var (
charRegexp = regexp.MustCompile(charPattern)
numCharRegexp = regexp.MustCompile(numCharPattern)
// mailRegexp = regexp.MustCompile(mailPattern)
dateRegexp = regexp.MustCompile(datePattern)
dateTimeRegexp = regexp.MustCompile(dateTimePattern)
timeWithLocationRegexp = regexp.MustCompile(timeWithLocationPattern)
timeZoneRegexp = regexp.MustCompile(timeZonePattern)
Expand Down Expand Up @@ -75,7 +74,10 @@ func IsDate(sInput interface{}) bool {
if len(val) == 0 {
return false
}
return dateRegexp.MatchString(val)
if _, err := time.Parse(time.DateOnly, val); err == nil {
return true
}
return false
default:
return false
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/valid/valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func ValidateDatetimeType(value interface{}) error {
return nil
}

if util.IsDate(value) {
return nil
}

// string type with time format is supported
if _, ok := util.IsTime(value); ok {
return nil
Expand Down
26 changes: 26 additions & 0 deletions src/common/valid/valid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package valid

import (
"testing"
"time"

"configcenter/src/common"
)
Expand Down Expand Up @@ -47,3 +48,28 @@ func TestIsInnerObject(t *testing.T) {
})
}
}

func TestValidateDatetimeType(t *testing.T) {
type args struct {
timeStr any
}
now := time.Now()
tests := []struct {
name string
args args
wantErr bool
}{
{"illegal date", args{"2024-00-00"}, true}, //should err
{"DateOnly", args{now.Format(time.DateOnly)}, false},
{"DateTime", args{now.Format(time.DateTime)}, false},
{"RFC3339", args{now.Format(time.RFC3339)}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateDatetimeType(tt.args.timeStr)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateDatetimeType() = %v:%v", tt.args, err)
}
})
}
}