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

Clean up Regexes #5255

Closed
wants to merge 1 commit into from
Closed

Clean up Regexes #5255

wants to merge 1 commit into from

Conversation

chuangw6
Copy link
Member

@chuangw6 chuangw6 commented Aug 2, 2022

Changes

Prior to this commit, the regexes in substitution.go file were hard to
understand and hard to reuse because of the complexity of the regexes.
For example, we have to consider

  • all the three notations (dot notation, bracket with single quote,
    and bracket with double quote)
  • the operator suffix including star [*] operator and
    int indexing that was not considerd before.
    Also the old regex coupled each notation part with the operator
    part, which leads to extra step to be done in the extract-related functions
    i.e. have to trim the [*] every time.

This PR

  • cleans up the regexes and make it as much reable and reuseble as possible
  • makes the method extractVariablesFromString be capable of extracting
    both variable name and the operator (indexing operator or star operator)
    examples:
    • $(params.myString) - name: "myString", operator: ""
    • $(params['myArray'][2]) - name: "myArray", operator: "1"
    • $(params.myObject[*]) - name: "myObject", operator: "*"

Enabling extractVariablesFromString to extract both name and operator
is particularly useful for array indexing validation. There will be more
PRs to come to clean up considering the complexity of the all regexes.
Also results-related regexes in resultref.go file need cleanup too.

/kind cleanup

Submitter Checklist

As the author of this PR, please check off the items in this checklist:

  • Has Docs included if any changes are user facing
  • Has Tests included if any functionality added or changed
  • Follows the commit message standard
  • Meets the Tekton contributor standards (including
    functionality, content, code)
  • Has a kind label. You can add one by adding a comment on this PR that contains /kind <type>. Valid types are bug, cleanup, design, documentation, feature, flake, misc, question, tep
  • Release notes block below has been updated with any user facing changes (API changes, bug fixes, changes requiring upgrade notices or deprecation warnings)
  • Release notes contains the string "action required" if the change requires additional action from users switching to the new release

Release Notes

NONE

@tekton-robot tekton-robot added release-note-none Denotes a PR that doesnt merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Aug 2, 2022
@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 2, 2022
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 63.9% 1.7

@abayer
Copy link
Contributor

abayer commented Aug 2, 2022

/approve

Nice! Getting out of obfuscated regex hell is a good thing, to say the least.

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 2, 2022
@chuangw6
Copy link
Member Author

chuangw6 commented Aug 2, 2022

/test pull-tekton-pipeline-alpha-integration-tests

1 similar comment
@chuangw6
Copy link
Member Author

chuangw6 commented Aug 2, 2022

/test pull-tekton-pipeline-alpha-integration-tests

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 63.9% 1.7

@chuangw6
Copy link
Member Author

chuangw6 commented Aug 3, 2022

/retest

1 similar comment
@chuangw6
Copy link
Member Author

chuangw6 commented Aug 3, 2022

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 63.9% 1.7

@lbernick
Copy link
Member

lbernick commented Aug 3, 2022

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 3, 2022
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 63.9% 1.7

@chuangw6
Copy link
Member Author

chuangw6 commented Aug 3, 2022

/test pull-tekton-pipeline-alpha-integration-tests

1 similar comment
@chuangw6
Copy link
Member Author

chuangw6 commented Aug 3, 2022

/test pull-tekton-pipeline-alpha-integration-tests

@abayer
Copy link
Contributor

abayer commented Aug 4, 2022

/retest

@abayer
Copy link
Contributor

abayer commented Aug 4, 2022

Ok, pull-tekton-pipeline-alpha-integration-tests keeps failing on the same example tests, which makes me think there is a legit issue.

@chuangw6
Copy link
Member Author

chuangw6 commented Aug 5, 2022

Ok, pull-tekton-pipeline-alpha-integration-tests keeps failing on the same example tests, which makes me think there is a legit issue.

Thanks @abayer for helping retest. #5222 that only updates doc files failed same 3 tests in alpha integration test. Let me retry.

@chuangw6
Copy link
Member Author

chuangw6 commented Aug 5, 2022

/test pull-tekton-pipeline-alpha-integration-tests

@chuangw6
Copy link
Member Author

chuangw6 commented Aug 8, 2022

/retest

1 similar comment
@chuangw6
Copy link
Member Author

chuangw6 commented Aug 8, 2022

/retest

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 59.4% -2.8

@chuangw6
Copy link
Member Author

/test pull-tekton-pipeline-integration-tests

@chuangw6
Copy link
Member Author

Hi @abayer,
I've solved the issue and also added the test. Please take a look. Thanks.

@abayer
Copy link
Contributor

abayer commented Aug 15, 2022

/lgtm

Yay!

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 15, 2022
@chuangw6 chuangw6 closed this Aug 15, 2022
@chuangw6 chuangw6 reopened this Aug 15, 2022
@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please ask for approval from abayer after the PR has been reviewed.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 59.4% -2.8

@abayer
Copy link
Contributor

abayer commented Aug 15, 2022

/retest

2 similar comments
@abayer
Copy link
Contributor

abayer commented Aug 15, 2022

/retest

@chuangw6
Copy link
Member Author

/retest

Prior to this commit, the regexes in substitution.go file were hard to
understand and hard to reuse because of the complexity of the regexes.
For example, we have to consider
  - all the three notations (dot notation, bracket with single quote,
  and bracket with double quote)
  - the operator suffix including star `[*]` operator and
  int indexing that was not considerd before.
Also the old regex coupled each notation part with the operator
part, which leads to extra step to be done in the extract-related functions
i.e. have to trim the [*] every time.

In this PR, we
  - clean up the regexes and make it as much reable and reuseble as possible
  - make the method `extractVariablesFromString` be capable of extracting
  both variable name and the operator (indexing operator or star operator)
  examples:
    - $(params.myString)       - name: "myString", operator: ""
    - $(params['myArray'][2])  - name: "myArray", operator: "1"
    - $(params.myObject[*])    - name: "myObject", operator: "*"

Enabling `extractVariablesFromString` to extract both name and operator
is particularly useful for array indexing validation. There will be more
PRs to come to clean up considering the complexity of the all regexes.
Also results-related regexes in resultref.go file need cleanup too.
@tekton-robot tekton-robot removed the lgtm Indicates that a PR is ready to be merged. label Aug 15, 2022
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/substitution/substitution.go 62.2% 59.4% -2.8

@abayer
Copy link
Contributor

abayer commented Aug 15, 2022

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 15, 2022
@abayer
Copy link
Contributor

abayer commented Aug 15, 2022

/retest

@chuangw6
Copy link
Member Author

@lbernick Since this PR has been changed a bit, please take a look again.
Thanks!

@chuangw6 chuangw6 requested a review from lbernick August 15, 2022 17:34
@@ -228,7 +221,7 @@ func (arrayOrString *ArrayOrString) applyOrCorrect(stringReplacements map[string

// if the stringVal is a string literal or a string that mixed with var references
// just do the normal string replacement
if !exactVariableSubstitutionRegex.MatchString(stringVal) {
if !isolatedResultVariableSubstitutionRegex.MatchString(stringVal) && !substitution.IsIsolatedArrayOrObjectParamRef(stringVal) {
Copy link
Member

Choose a reason for hiding this comment

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

hm it seems like the substitution package is responsible for determining whether a reference is an isolated reference or not. Can the regex being used be moved to that package instead (and put into a more useful helper function instead of exporting the regex)? With the existing code, each package is only partially responsible for determining how the param reference should be parsed, and I'd like to see better separation of concerns.

var VariableSubstitutionRegex = regexp.MustCompile(variableSubstitutionFormat)
var exactVariableSubstitutionRegex = regexp.MustCompile(exactVariableSubstitutionFormat)
// ResultVariableSubstitutionRegex is a regex to find all result matching substitutions
var ResultVariableSubstitutionRegex = regexp.MustCompile(resultVariableSubstitutionFormat)
Copy link
Member

Choose a reason for hiding this comment

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

does this need to be exported? based on its usage in the TaskRun reconciler, it looks like it would be more useful as part of a validation helper function.


// parseIndex parses integer from a string.
// If the string is not numeric, the second returned value will be false.
func parseIndex(s string) (int, bool) {
Copy link
Member

@JeromeJu JeromeJu Aug 16, 2022

Choose a reason for hiding this comment

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

nit: Wondering if we should stick with strconv.Atoi here? This func seems to be trimming the error message which we might be interested to boolean.

Copy link
Member

Choose a reason for hiding this comment

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

And also this func doesn't seem to be used elsewhere at the moment?

@tekton-robot tekton-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 16, 2022
@tekton-robot
Copy link
Collaborator

@chuangw6: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@chuangw6 chuangw6 marked this pull request as draft September 27, 2022 20:00
@tekton-robot tekton-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 27, 2022
@chuangw6 chuangw6 closed this Sep 27, 2022
@chuangw6
Copy link
Member Author

Hey @lbernick @JeromeJu @abayer
Thank you so much for taking your time to review this PR. I saw this as non-urgent and was distracted by other tasks, so I didn't work on this after then.

Just temporally mark this as closed. Will pick this up later and get it ready for review. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note-none Denotes a PR that doesnt merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants