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

Time Based Queries with Specific Format #11

Closed
jon-whit opened this issue May 16, 2019 · 8 comments · Fixed by #14
Closed

Time Based Queries with Specific Format #11

jon-whit opened this issue May 16, 2019 · 8 comments · Fixed by #14
Labels
enhancement New feature or request

Comments

@jon-whit
Copy link

jon-whit commented May 16, 2019

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
Copy link
Owner

a8m commented May 16, 2019

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
Copy link
Author

jon-whit commented May 16, 2019

@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
Copy link
Owner

a8m commented May 17, 2019

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.

@a8m a8m added the enhancement New feature or request label May 17, 2019
@jon-whit
Copy link
Author

@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 added a commit that referenced this issue May 20, 2019
@a8m
Copy link
Owner

a8m commented May 20, 2019

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

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

@jon-whit
Copy link
Author

@a8m No worries at all! Weekend away dude 👍

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

a8m added a commit that referenced this issue May 20, 2019
a8m added a commit that referenced this issue May 22, 2019
a8m added a commit that referenced this issue May 23, 2019
a8m added a commit that referenced this issue May 23, 2019
@a8m a8m closed this as completed in #14 May 23, 2019
@a8m
Copy link
Owner

a8m commented May 23, 2019

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

@jon-whit
Copy link
Author

Thanks @a8m !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants