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

Options framework to allow more control over parsing #89

Merged
merged 10 commits into from Sep 30, 2020

Conversation

troyspencer
Copy link
Contributor

This PR creates a framework for creating and applying arbitrary options without breaking changes. It follows the pattern used in github.com/gorilla/handlers. Options are created by implementing the ParserOption interface. They are then passed in a spread to existing parsing functions. The options are passed down into the parser constructor, where they are then applied on the parser. Through the excellent architecture already present in this package, the changes to accomplish this are straightforward.

The first options to implement the ParserOption interface are PreferMonthFirst and RetryAmbiguousDateWithSwap.

PreferMonthFirst exposes parser.preferMonthFirst, and the code handling the false case was added by #88.

RetryAmbiguousDateWithSwap adds a new parser field, parser.retryAmbiguousDateWithSwap, allowing parsing not to fail when the month is not actually ambiguous because if it were swapped with the day it would be outside the range of valid month values. It does this by checking if the month was ambiguous; if so, then trying to parse, and rebuilding the parser with the opposite preferMonthFirst value and disabling retryAmbiguousDateWithSwap. The code of the logic behind the option was added by #88, and this PR places its implementation in a much tighter scope while also exposing it as a ParserOption.

Created PreferMonthFirst option, which exposes preferMonthFirst in a backwards compatible manner with optional spread functions that operate on the parser when it's being constructed
Original retry logic by KamalGalrani
Adapted into a deferred function at the source enabled by an option, by me
Update RetryAmbiguousDateWithSwap detection logic after testing
@troyspencer
Copy link
Contributor Author

CI is failing due to unrelated issue #90.

@troyspencer troyspencer mentioned this pull request Aug 13, 2019
@codecov-io
Copy link

Codecov Report

Merging #89 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #89   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines         899    929   +30     
=====================================
+ Hits          899    929   +30
Impacted Files Coverage Δ
parseany.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0fb0a47...a56081e. Read the comment docs.

@troyspencer
Copy link
Contributor Author

This now also fixes #90 by vendoring github.com/apcera/termtables

@troyspencer
Copy link
Contributor Author

@araddon Any thoughts?

@skius
Copy link

skius commented Aug 24, 2019

Dates with dots as separators (like 30.1.2014) don't work with PreferMonthFirst(false):

preferMonthFirst := dateparse.PreferMonthFirst(false)
t, err := dateparse.ParseAny("30.1.2014", preferMonthFirst) // => parsing time "30.1.2014": month out of range
if err != nil {
	fmt.Println(err)
}

t, err = dateparse.ParseAny("30/1/2014", preferMonthFirst) // => 2014-01-30 00:00:00 +0000 UTC
if err != nil {
	fmt.Println(err)
}
fmt.Println(t)

@troyspencer
Copy link
Contributor Author

That's due to an inconsistency in how this package labels dates as ambiguous. This flag is only set and returned as the ambiguous date error in the case from the test. If the dot case set ambiguousMMDD, then the option would activate.

@skius
Copy link

skius commented Aug 24, 2019

Sounds like an issue in general. Do you want to open an issue? I will if you don't want to.

@troyspencer
Copy link
Contributor Author

Sure, you can open one, and to elaborate a bit more, it's the check for preferMonthFirst, and alternate logic if it's false which will allow this test (and any similar case with different overall format) to pass; setting ambiguousMMDD will allow the retry with opposite preference option to activate.

@djaglowski
Copy link

I'd love to use this package, but I can't without this feature. Anything holding it up?

@codecov-commenter
Copy link

codecov-commenter commented Jun 11, 2020

Codecov Report

Merging #89 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #89   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          899       929   +30     
=========================================
+ Hits           899       929   +30     
Impacted Files Coverage Δ
parseany.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d820a61...539e9f1. Read the comment docs.

@troyspencer
Copy link
Contributor Author

I'd love to use this package, but I can't without this feature. Anything holding it up?

Not from me. I just removed the vendoring that was made unnecessary by #101.

@uditrana
Copy link

@araddon is there a reason this isn't pulled into master?

@araddon
Copy link
Owner

araddon commented Sep 30, 2020

Thank you!

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

Successfully merging this pull request may close these issues.

None yet

7 participants