Skip to content

Commit

Permalink
refactor: validating date range
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrMatsko committed Jan 23, 2025
1 parent 7123776 commit d314efb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
10 changes: 2 additions & 8 deletions api/handler/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ func getEventsList(writer http.ResponseWriter, request *http.Request) {
page := middleware.GetPage(request)
fromStr := middleware.GetFromStr(request)
toStr := middleware.GetToStr(request)
var err error

fromStr, err = validateFromStr(fromStr)
if err != nil {
render.Render(writer, request, api.ErrorInvalidRequest(err)) //nolint
return
}

toStr, err = validateToStr(toStr)
validator := DateRangeValidator{AllowInf: true}
fromStr, toStr, err := validator.ValidateDateRangeStrings(fromStr, toStr)
if err != nil {
render.Render(writer, request, api.ErrorInvalidRequest(err)) //nolint
return
Expand Down
11 changes: 2 additions & 9 deletions api/handler/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,8 @@ func getTriggerNoisiness(writer http.ResponseWriter, request *http.Request) {
toStr := middleware.GetToStr(request)
sort := middleware.GetSortOrder(request)

var err error

fromStr, err = validateFromStr(fromStr)
if err != nil {
render.Render(writer, request, api.ErrorInvalidRequest(err)) //nolint
return
}

toStr, err = validateToStr(toStr)
validator := DateRangeValidator{AllowInf: true}
fromStr, toStr, err := validator.ValidateDateRangeStrings(fromStr, toStr)
if err != nil {
render.Render(writer, request, api.ErrorInvalidRequest(err)) //nolint
return
Expand Down
60 changes: 44 additions & 16 deletions api/handler/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,54 @@ import (
"github.com/go-graphite/carbonapi/date"
)

func validateFromStr(fromStr string) (string, error) {
if fromStr != "-inf" {
from := date.DateParamToEpoch(fromStr, "UTC", 0, time.UTC)
if from == 0 {
return "", fmt.Errorf("can not parse from: %s", fromStr)
}
fromStr = strconv.FormatInt(from, 10)
// DateRangeValidator for query parameters from and to.
type DateRangeValidator struct {
AllowInf bool
}

// ValidateDateRangeStrings validates and converts both from and to query params.
// Returns converted from and to, or error if there was any.
// If AllowInf is true "-inf" is allowed for from, "+inf" for to.
func (d DateRangeValidator) ValidateDateRangeStrings(fromStr, toStr string) (string, string, error) {
fromStr, err := d.validateFromStr(fromStr)
if err != nil {
return "", "", err
}

toStr, err = d.validateToStr(toStr)
if err != nil {
return "", "", err
}

return fromStr, toStr, nil
}

// validateFromStr by trying to parse date with carbonapi/date package. Also converts to proper format.
// If AllowInf is true, then "-inf" is also allowed.
func (d DateRangeValidator) validateFromStr(fromStr string) (string, error) {
if d.AllowInf && fromStr == "-inf" {
return fromStr, nil
}

from := date.DateParamToEpoch(fromStr, "UTC", 0, time.UTC)
if from == 0 {
return "", fmt.Errorf("can not parse from: %s", fromStr)
}

return fromStr, nil
return strconv.FormatInt(from, 10), nil
}

func validateToStr(toStr string) (string, error) {
if toStr != "+inf" {
to := date.DateParamToEpoch(toStr, "UTC", 0, time.UTC)
if to == 0 {
return "", fmt.Errorf("can not parse to: %v", to)
}
toStr = strconv.FormatInt(to, 10)
// validateToStr by trying to parse date with carbonapi/date package. Also converts to proper format.
// If AllowInf is true, then "+inf" is also allowed.
func (d DateRangeValidator) validateToStr(toStr string) (string, error) {
if d.AllowInf && toStr == "+inf" {
return toStr, nil
}

to := date.DateParamToEpoch(toStr, "UTC", 0, time.UTC)
if to == 0 {
return "", fmt.Errorf("can not parse to: %v", to)
}

return toStr, nil
return strconv.FormatInt(to, 10), nil
}

0 comments on commit d314efb

Please sign in to comment.