a8m / rql

Resource Query Language for REST
MIT License
343 stars 42 forks source link

Time Based Queries with Specific Format #11

Closed jon-whit closed 5 years ago

jon-whit commented 5 years ago

First off, thanks for this awesome library! Some awesome work has been put into this project, and I'm going to be able to leverage a lot of it. 👍


I'd like to be able to filter on time range based results with a specific time format. For example, I'm storing time in YYYY-MM-DD HH:MM format. If the following filter is provided, I'd like it to error.

{
    "filter": {
        "startTime": {
            $gt: "2006-01-02T15:04:05Z07:00"
    }
}

It looks like time validation in the library right now just validates based on RFC3339 time format, so the filter expression above would work. What would be your recommendation to achieve this?

a8m commented 5 years ago

We can either add it to the rql.Config, or add it as an optional struct tag to give more granularity. For example:

The config option:

var Parser = rql.MustNew(rql.Config{
    // User if the resource we want to query.
    Model: User{},
    // TimeLayout is the layout for time fields when calling time.Parse.
    Layout: time.UnixDate,
})

Struct tag option:

type User struct {
    Name      string    `rql:"filter"`
    Address   string    `rql:"filter"`
    CreatedAt time.Time `rql:"filter,sort,layout=UnixDate"`
    UpdatedAt time.Time `rql:"filter,sort,layout=RFC822"`
    DeletedAt time.Time `rql:"filter,sort"` // RFC3339.
}

What do you think?

Update: struct option example.

jon-whit commented 5 years ago

@a8m I like the idea of it being in the layout tag of the model so long as you can specify custom time formats (formatted using Go's reference time) such as 2006-01-02 15:04 etc..

What are your thoughts?

a8m commented 5 years ago

Sounds good @jon-whit.

You can add any valid layout in the struct tag, for example:

type User struct {
         // ...
    CreatedAt time.Time `rql:"filter,sort,layout=2006-01-02 15:04"`
}

rql will do the validation on the initialization (checks that time.Parse(layout, layout) doesn't return an error), and will use this one for the time parsing.

jon-whit commented 5 years ago

@a8m Yeah, that'd be rad 👍. Do you want to add this support? If you don't think you'll be able to get around to it soon, I could give it a go. What would you like?

a8m commented 5 years ago

@jon-whit - sorry for the delay, weekend mode.

I've added a WIP PR, I'll try to land it tomorrow.

jon-whit commented 5 years ago

@a8m No worries at all! Weekend away dude :+1:

Thanks for this support. That's some awesome stuff right there.. This will be super helpful!

a8m commented 5 years ago

@jon-whit - A new version released with this feature (v1.1.0). Let me know if it works for you.

jon-whit commented 5 years ago

Thanks @a8m !