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

Type::Tiny::Enum - change ::XS encoding to quote strings #59

Merged
merged 1 commit into from Aug 18, 2020

Conversation

aeruder
Copy link
Contributor

@aeruder aeruder commented Aug 11, 2020

If we have something like Enum[qw(a b)] we would communicate with
Type::Tiny::XS via a string like:

'Enum[a, b]'

We've already been hitting issues with this (see perl rt #129729) on hyphens
but now also hitting it on spaces (see perl rt #133141). This commit
changes the encoding for enums for Type::Tiny::XS to:

'EnumQ["a", "b"]'

Since we use B::perlstring() to encode this, we should also handle any
arbitrary string.

Why change Enum -> EnumQ? We've introduced a weird API change,
Type::Tiny::XS < 0.20 won't know how to parse an Enum[] with quoted
strings, but if we pass it an EnumQ[] it'll just silently move past
it (and not be able to optimize it). Then we can release a
Type::Tiny::XS that supports EnumQ and get all the SPEED back.

@aeruder
Copy link
Contributor Author

aeruder commented Aug 11, 2020

This along with a corresponding change over in the p5-type-tiny-xs repo is one proposed fix to the Enum encoding issues.

Also could fix 133141 by just adding another character to the "can't xsify" regex (along with the existing hyphen) or put something together where the interface between Type::Tiny and Type::Tiny::XS isn't via strings

@codecov
Copy link

codecov bot commented Aug 11, 2020

Codecov Report

Merging #59 into master will decrease coverage by 0.23%.
The diff coverage is 71.42%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
- Coverage   88.80%   88.57%   -0.24%     
==========================================
  Files          36       36              
  Lines        3583     3597      +14     
  Branches      984      991       +7     
==========================================
+ Hits         3182     3186       +4     
- Misses         62       64       +2     
- Partials      339      347       +8     
Impacted Files Coverage Δ
lib/Type/Tiny/Enum.pm 75.90% <71.42%> (-9.61%) ⬇️

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 3a06a45...9072674. Read the comment docs.

@coveralls
Copy link

coveralls commented Aug 11, 2020

Coverage Status

Coverage decreased (-0.05%) to 98.552% when pulling 9072674 on aeruder:xs-enum-encoding-bump into acd6114 on tobyink:master.

@tobyink
Copy link
Owner

tobyink commented Aug 12, 2020

I admire your commitment to sticking with the established API, but feel like at this point it might be easier to just do:

my $xsub = Type::Tiny::XS::_parameterize_Enum_for( $opts{unique_values} );

Yes, _parameterize_Enum_for isn't part of Type::Tiny::XS's officially supported API, but meh, Type::Tiny and Type::Tiny::XS can be friends with benefits.

@aeruder
Copy link
Contributor Author

aeruder commented Aug 12, 2020 via email

@aeruder
Copy link
Contributor Author

aeruder commented Aug 12, 2020

Yea, that's a pretty involved change too that might trigger changing the interface to Type::Tiny::XS entirely.

For Tuple[Enum[...]] to inline, we need to register the callback with Type::Tiny::XS (so that Type::Tiny::XS::is_known works) and it still needs to return something parse-able by Type::Parser (so we'll still want the B::perlstring encoding). The first part specifically means we'd probably also have to drag the AUTO::TC%d named subname logic into Type::Tiny::Enum for the inline_check to work out AND also attempt to register the sub to Type::Tiny::XS::_know. This is all assuming the intent here is not require changes in both Type::Tiny and Type::Tiny::XS for this.

Another option I could pursue is not renaming the encoding to EnumQ[], leaving it Enum[] (now with perlstring encoding) and then add some lame check for " in the contents to understand if Type::Tiny::XS is being called from a old Type::Tiny or new Type::Tiny. I'm pretty sure that would work out, but might unintentionally break someone using " in a string in an old Type::Tiny Enum (although that would have exploded on that person if they then tried to wrap that Enum in a Tuple[] and triggered the Type::Parser error).

@tobyink
Copy link
Owner

tobyink commented Aug 12, 2020

Hmm, if Type::Tiny::XS encounters Enum[ and the following character is a quote character (either " or ') then treat the strings as all quoted, and if the following character is not a quote character, treat the strings as barewords and use split.

Meanwhile Type::Tiny::Enum can do a version check on Type::Tiny::XS and pass the strings quoted if it's version 0.020+. If it's older Type::Tiny::XS, bail out from using XS if the strings don't all match /^\w+$/, and pass the strings unquoted if they do all match /^\w+$/.

@aeruder
Copy link
Contributor Author

aeruder commented Aug 12, 2020

That sounds reasonable to me and pretty sure it'll work. I'll update the pull request in a bit.

@aeruder
Copy link
Contributor Author

aeruder commented Aug 15, 2020

Here we go (along with the Type::Tiny::XS pull request which I've updated too). Pretty much what you suggested, version-check Type::Tiny::XS, if we can use the new encoding, otherwise use the old one (but avoid it for these broken space-including strings). This makes the assumption that the version of ::XS that includes the fix will be 0.020.

If we have something like Enum[qw(a b)] we would communicate with
Type::Tiny::XS via a string like:

   'Enum[a, b]'

We've already been hitting issues with this (see perl rt #129729) on hyphens
but now also hitting it on spaces (see perl rt #133141).  This commit
changes the encoding for enums for Type::Tiny::XS to:

   'Enum["a", "b"]'

Since we use B::perlstring() to encode this, we should also handle any
arbitrary string.  When we're on an old Type::Tiny::XS though, we'll
continue to use the old encoding, we'll just avoid stringification for
even more things.
@aeruder
Copy link
Contributor Author

aeruder commented Aug 17, 2020

This appears to be passing all tests now (with old Type::Tiny::XS and new Type::Tiny::XS [manually patched to 0.020 since the version check in here for the new API is 0.020+]). Was able to distinkt-dist it as well.

@aeruder
Copy link
Contributor Author

aeruder commented Aug 17, 2020

Built a local Type-Tiny-1.010004 and Type-Tiny-XS-v0.020 and have confirmed that this gets at least the first half of our internal testsuite at ziprecruiter through the Type::Tiny upgrade. 🎉

@tobyink
Copy link
Owner

tobyink commented Aug 17, 2020

Released Type::Tiny::XS 0.020. Going to wait a day or so for CPAN testers to do their stuff with that before releasing a new Type::Tiny, though I don't anticipate any problems with it.

@tobyink tobyink merged commit 2323726 into tobyink:master Aug 18, 2020
clrpackages pushed a commit to clearlinux-pkgs/perl-Type-Tiny that referenced this pull request Aug 27, 2020
… version 1.010004

1.010004	2020-08-18

 [ Bug Fixes ]
 - Fix XSifying Enum[] where the strings contain certain non-word
   characters.
   Andrew Ruder++
   <tobyink/p5-type-tiny-xs#12>
   <tobyink/p5-type-tiny#59>
 - Type::Params compile_named using both the head and named_to_list options
   would cause compilation error.
   Fixes RT#132419.
   Hauke D++
   Sandor Patocs++
   <https://rt.cpan.org/Ticket/Display.html?id=132419>
 - Workaround RT#121957 by avoiding attempting to XSify Enum type

(NEWS truncated at 15 lines)
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Sep 7, 2020
1.010006	2020-09-04

 [ Bug Fixes ]
 - Eliminate recusion warnings when Type::Parser needs to parse complex
   types.
   Fixes RT#121957.
   Diab Jerius++
   <https://rt.cpan.org/Ticket/Display.html?id=121957>

 [ Other ]
 - Better handling of coercions for pre-declared types in Type::Library.
   The type objects created before the type was properly defined will now
   lazily attempt to find coercions from the properly defined type once it
   becomes available.

1.010005	2020-08-26

 - Improvements to $AvoidCallbacks support for
   Type::Tiny::{Class,Role,Duck,Enum,Union,Intersection}, and LaxNum, Ref,
   RegexpRef, FileHandle, Object, Overload, and Tied types from
   Types::Standard.

1.010004	2020-08-18

 [ Bug Fixes ]
 - Fix XSifying Enum[] where the strings contain certain non-word
   characters.
   Andrew Ruder++
   <tobyink/p5-type-tiny-xs#12>
   <tobyink/p5-type-tiny#59>
 - Type::Params compile_named using both the head and named_to_list options
   would cause compilation error.
   Fixes RT#132419.
   Hauke D++
   Sandor Patocs++
   <https://rt.cpan.org/Ticket/Display.html?id=132419>
 - Workaround RT#121957 by avoiding attempting to XSify Enum type
   constraints with more than 50 possible strings.
   Fixes RT#121957.
   Diab Jerius++
   <https://rt.cpan.org/Ticket/Display.html?id=121957>

 [ Documentation ]
 - Link to HTTPS version of Type::Tiny web page.

1.010003	2020-08-08	The Crazy 88

 [ Bug Fixes ]
 - ClassName type constraint should treat empty @isa as if no @isa were
   defined, like Type::Tiny::XS.
   Fixes RT#132583.
   Szymon Nieznański++
   <https://rt.cpan.org/Ticket/Display.html?id=132583>
 - Fix for Type::Tiny->can called as a class method.
   Meredith Howard++
   <tobyink/p5-type-tiny#57>
 - Fix predeclared types in Type::Library.
   Meredith Howard++
   <tobyink/p5-type-tiny#58>

 [ Documentation ]
 - Document some edge cases for Types::Standard Int.
   <https://rt.cpan.org/Ticket/Display.html?id=132754>
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

3 participants