Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add FilterDuration and replace FilterTime in InitPostEvent… #544

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Changes from 1 commit
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
48 changes: 32 additions & 16 deletions domain/filter/shorthand.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,29 @@ func AddAnd(lhs, rhs Expr) Expr {
// * 期間中に始まる (time_start >= since AND time_start < until)
// * 期間中に終わる (time_end >= since AND time_end < until)
// * 期間より前に始まり期間より後に終わる (time_start < since AND time_end >= until)
//
// ただしsinceがゼロのとき time_start <= until
// untilがゼロのとき since <= time_end
// 双方がゼロのき nil を返す
func FilterDuration(since, until time.Time) (Expr, error) {
if !since.IsZero() && !until.IsZero() {
if since.After(until) {
return nil, errors.New("invalid time range")
}
if since.IsZero() && until.IsZero() {
return nil, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここはエラーを返したい

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嘘です
エラーはnilでいいけどExprはnilではなくて空の&CmpExpr{}を返すのがよさそう

} else if since.IsZero() {
return &CmpExpr{
Attr: AttrTimeStart,
Relation: LessEq,
Value: until,
}, nil
} else if until.IsZero() {
return &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: since,
}, nil
}

if since.After(until) {
return nil, errors.New("invalid time range")
}

// 期間中に始まる
Expand Down Expand Up @@ -136,21 +154,19 @@ func FilterDuration(since, until time.Time) (Expr, error) {
},
}

startBeforeMin := &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: since,
}
endAfterMax := &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: until,
}
// 期間より前に始まり期間より後に終わる
throughout := &LogicOpExpr{
LogicOp: And,
Lhs: startBeforeMin,
Rhs: endAfterMax,
Lhs: &CmpExpr{
Attr: AttrTimeStart,
Relation: Less,
Value: since,
},
Rhs: &CmpExpr{
Attr: AttrTimeEnd,
Relation: GreterEq,
Value: until,
},
}

return &LogicOpExpr{
Expand Down
Loading