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

Add option to fall back on regex parsing to splitAddress1 #281

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

gabypancu
Copy link
Contributor

@gabypancu gabypancu commented Aug 30, 2024

What are you trying to accomplish?

Resolves https://github.com/Shopify/address/issues/2726

When there is no reserved delimiter present in an address line (i.e. any address that was created before the release of additional address fields), we want to attempt to split the additional fields out using regex. The regex patterns will have to be strict in order to minimize incorrect captures.

What approach did you choose and why?

  1. Adds a conservative address1 regex pattern for the Netherlands. On a sample of about 800 address lines, it parses 75% correctly (the rest are not matched) - https://rubular.com/r/3BNDUviIL8hHeS
    2. Adds a splitAddress1WithFallback function to the TS library that uses the regex as a fallback if splitting on the delimiter is not possible
  2. Adds an optional param tryRegexFallback to the existing splitAddress1 function in the TS library. When true, and when the input string does not contain the reserved delimiter, we attempt to parse using regex.

This way, checkout web still only has one function to call for splitting.

The following PRs have been merged into this one:

See PR comments for debate on the following pros and cons
Our options for where to implement this new behaviour:

  1. In the existing splitAddress1 function, controlled by a new optional param
  • Requires the smallest change in checkout-web, but brings higher visibility to this feature
  • Reusability if we choose to use this on surfaces other than checkout-web
  1. In a new function in Worldwide
  • Maintains separation with the existing splitting function, reduces visibility to this feature
  • Reusability if we choose to use this on surfaces other than checkout-web
  1. Directly in checkout-web when splitting fails
  • Keeps this feature private to Shopify
  • Introduces complexity & Worldwide domain into checkout-web
  • Not reusable outside of checkout-web

Checklist

  • I have added a CHANGELOG entry for this change (or determined that it isn't needed)

@gabypancu gabypancu changed the title Parse additional fields Add parseAddress1 function to parse standard addresses Aug 30, 2024
@gabypancu gabypancu marked this pull request as ready for review September 4, 2024 16:04
@gabypancu gabypancu changed the title Add parseAddress1 function to parse standard addresses Add splitAddress1WithFallback function to parse standard addresses Sep 4, 2024
@gabypancu gabypancu changed the title Add splitAddress1WithFallback function to parse standard addresses Add splitAddress1WithFallback function Sep 4, 2024
db/data/regions/NL.yml Outdated Show resolved Hide resolved
@gabypancu gabypancu changed the title Add splitAddress1WithFallback function Add option to fall back on regex parsing to splitAddress1 Sep 6, 2024
db/data/regions/NL.yml Outdated Show resolved Hide resolved
lib/worldwide/region.rb Outdated Show resolved Hide resolved
lang/typescript/src/extended-address/splitAddress1.test.ts Outdated Show resolved Hide resolved
Comment on lines 3 to 28
## 0.7.0-next.0

### Minor Changes

- 1f5d405: Add optional experimentalRegexFallback param to splitAddress1 function to attempt splitting address lines that do not contain the reserved delimiter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you follow these docs to generate the prerelease? https://github.com/changesets/changesets/blob/main/docs/prereleases.md

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it was but I don't see the package published as a prerelease on NPM so might have to change something in the GH Actions to actually do it https://www.npmjs.com/package/@shopify/worldwide?activeTab=versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I skipped the publish step for now - wasn't sure if that should happen after this PR merges

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to read the prerelease again but I though they recommended not merging something in prerelease mode to the main/default branch and rather just have it deploy from a separate branch, then exit prerelease mode before merging.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok yeah changesets does recommend using a second branch when doing prereleases just so the main branch isn't locked up in prerelease mode. Given we don't make that many changes to this library and we won't be in prerelease for long thats probably still fine to just lock up the main branch but I'd rather not if we don't have to so main stays in a releasable state that reflects the latest version.

We can try to run the publish on this branch, we'll need to modify the GH action to also run on this branch though so the job that handles publishing will run.

Copy link
Contributor

@kennygoff kennygoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 If we want to do the prerelease I still recommend trying to publish the prerelease from this branch instead of merging though as the changesets docs recommend.

"mode": "pre",
"tag": "next",
"initialVersions": {
"@shopify/worldwide": "0.6.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "0.7.0"?

Copy link
Contributor Author

@gabypancu gabypancu Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is the initial version before the pre-release. I looked at the tests in changesets, you can see they've set up the initial package versions here, and this is the version that appears in the release plan's initialVesrions https://github.com/changesets/changesets/blob/c867f32d0c25cfb2a6b518bc1f9880f758beb157/packages/assemble-release-plan/src/index.test.ts#L908

@@ -192,6 +192,21 @@ class RegionYmlConsistencyTest < ActiveSupport::TestCase
end
end

test "address1_regex capture groups must belong to a limited set of allowed names" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🍳 👌

Comment on lines 220 to 228
test('creates an address object from string matching one of the defined regexes', () => {
const fieldDefinition: FieldConcatenationRule[] = [
{key: 'streetName'},
{key: 'streetNumber'},
];
const regexPatterns = [
new RegExp('^(?<streetName>[^\\d]+) (?<streetNumber>\\d+)$'),
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you update this test to have 2 regexPatterns, with the first not a match and the second a match?

Copy link
Contributor

@DominiqueFlaaa DominiqueFlaaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, Gaby! Left a couple comments, non-blocking.

@gabypancu gabypancu force-pushed the parse-additional-fields branch 2 times, most recently from 76d8165 to 7829478 Compare September 12, 2024 15:02
@gabypancu
Copy link
Contributor Author

The pre-release is now published as v0.7.0-next.0.

Next steps:

  1. Keep this PR open, merge any additional regex support into this branch
  2. Start using the pre-release version in checkout-web
  3. Once this is tested on checkout and we have support for all relevant countries, exit pre-release mode, publish the new version and merge this PR into main
    a. Don't forget to remove the branch from npm-release.yml

Comment on lines +7 to +27
- bbacde1: Add address1 regex for BE, BR, CL, ES, IL, MX
- b6e1c7f: Add address1 regex for DE
- ee7b7a9: Add optional tryRegexFallback param to splitAddress1 function to attempt splitting address lines that do not contain the reserved delimiter

## 0.7.0-next.2

### Minor Changes

- 3bfb56a: Add address1 regex for DE

## 0.7.0-next.1

### Minor Changes

- Add address1 regex for BE, BR, CL, ES, IL, MX

## 0.7.0-next.0

### Minor Changes

- 1f5d405: Add optional tryRegexFallback param to splitAddress1 function to attempt splitting address lines that do not contain the reserved delimiter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the pre-releases, since we are no longer publishing a pre-release in favour of a minor version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-releases are published and available in npm, so although 0.7.0 includes the same content I think it's worth including this bit of documentation on the pre-release versions. I see it left in in some other shopify repos

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-releases are published and available in npm

Ah thank you for clarifying.

Copy link
Contributor

@DominiqueFlaaa DominiqueFlaaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gabypancu gabypancu merged commit f13cd4c into main Oct 1, 2024
10 checks passed
@gabypancu gabypancu deleted the parse-additional-fields branch October 1, 2024 16:53
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.

4 participants