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

update to avoid hard coding of the artifactType #30

Merged
merged 4 commits into from
Oct 19, 2020

Conversation

IsabellaDev
Copy link
Contributor

Hi, sorry for the delay, please check if this what you need, let me know if you need me to do any modification. Thanks.

Signed-off-by: Isabella Liu <isabellaliu77@gmail.com>
main.go Outdated
helmTargetArtifactTypeValue := "Helm"
yamlsTargetArtifactTypeValue := "Yamls"
knativeTargetArtifactTypeValue := "Knative"

re := regexp.MustCompile(`artifacttype:\\s.*`)
Copy link
Member

Choose a reason for hiding this comment

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

Can we possibly account for scenarios where mistakenly more than one space is left by using say \s*

Also can we search for [^\s]+ as the string to search for?

Also please chande artifacttype to artifactType. It was a bug from before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, I made some changes, please check if it satisfied the need. I removed one '' from your regexp pattern, it seems only '\s' should meet the requirement to represent the whitespace from my test. Let me know if you need any updates. Thanks.

main.go Outdated
} else if strings.HasSuffix(artifactTypematch, helmTargetArtifactTypeValue) {
artifactType = helmTargetArtifactTypeValue
}
match := []rune(artifactTypematch)
Copy link
Member

Choose a reason for hiding this comment

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

Before this we could possibly check if the match was successful or empty. If empty we can use s hard-coded empty vslue like "".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, you are right, there should be a check to see if the match is successful or not. So if it's empty, you want the artifactType to be set to "", is that correct?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

main.go Outdated
artifactType = helmTargetArtifactTypeValue
}
match := []rune(artifactTypematch)
artifactType := string(match[16:len(match)])
Copy link
Member

Choose a reason for hiding this comment

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

Instead of hardcoding 16, can we ectract out the non spaced string from suffiix? By say using a regex? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, 16 is the length of the former part of the string, it has nothing to do with the suffix. So if you want me to remove all possible whitespace, I can try to make the change. Or maybe you prefer to use regex groups to avoid such hardcoding like 16?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, Using regex groups would be a easier approach.

@ashokponkumar
Copy link
Member

Hi, sorry for the delay, please check if this what you need, let me know if you need me to do any modification. Thanks.

Thanks for the fix. Have suggested msome changes, please have a look.

Signed-off-by: Isabella Liu <isabellaliu77@gmail.com>
main.go Outdated
Comment on lines 184 to 195

helmTargetArtifactTypeValue := "Helm"
yamlsTargetArtifactTypeValue := "Yamls"
knativeTargetArtifactTypeValue := "Knative"

re := regexp.MustCompile(`artifacttype:\\s.*`)
re := regexp.MustCompile(`artifactType:\s.*`)
artifactTypematch := re.FindString(plan)
artifactType := yamlsTargetArtifactTypeValue
if strings.HasSuffix(artifactTypematch, knativeTargetArtifactTypeValue) {
artifactType = knativeTargetArtifactTypeValue
} else if strings.HasSuffix(artifactTypematch, helmTargetArtifactTypeValue) {
artifactType = helmTargetArtifactTypeValue
var artifactType string
if artifactTypematch == "" {
artifactType = ""
} else {
match := []rune(artifactTypematch)
name := string(match[len("artifactType:"):len(match)])
artifactType = strings.Replace(name, " ", "", -1)
}

Copy link
Member

Choose a reason for hiding this comment

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

How about something like

var artifactType
re := regexp.MustCompile(`artifactType:\s*([^\s]*)`)
matches = rgx.FindStringSubmatch(plan)
if len(matches) > 0 {
    artifactType = matches[0]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm,that's a smart solution. But is [^\s]* necessary, since \s* should be able to catch all white spaces behind : I think?

Copy link
Member

Choose a reason for hiding this comment

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

It will help ignore the space at the end. Also now that I think about it a + is better than a * there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made some changes, please take a look. [^\s]* would cause no match if there is any whitespace in the latter part. So I used (.+) instead, and then removed any possible whitespace in it.

Signed-off-by: Isabella Liu <isabellaliu77@gmail.com>
main.go Outdated
artifactType = knativeTargetArtifactTypeValue
} else if strings.HasSuffix(artifactTypematch, helmTargetArtifactTypeValue) {
artifactType = helmTargetArtifactTypeValue
re := regexp.MustCompile(`artifactType:\s*(.+)`)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
re := regexp.MustCompile(`artifactType:\s*(.+)`)
re := regexp.MustCompile(`artifactType:\s*([^\s]+)`)

Won't this end up choosing a contiguous set of non space characters like what we want? The issue with the previous regex I had suggested was the user of * which was incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there would be no whitespace in the words between, [^\s]+ can give the correct answer. But if the string is like:
"artifactType: he l m", it will only take "he", not "helm". If "he" is what you expected, I can change it to '[^\s]+'.
Please let me know, thanks.

main.go Outdated
Comment on lines 188 to 194
if len(artifactTypematch) == 0 {
artifactType = ""
} else {

artifactType = strings.ReplaceAll(artifactTypematch[1], " ", "")
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if len(artifactTypematch) == 0 {
artifactType = ""
} else {
artifactType = strings.ReplaceAll(artifactTypematch[1], " ", "")
}
if len(artifactTypematch) > 1 {
artifactType = artifactTypematch[1]
}

This can leverage the fact that the default value of golang string is "". Also since we are selecting only non-spaced characters we need not replace them. And since we are accessing the first element it is better to check for >1 as length.

Signed-off-by: Isabella Liu <isabellaliu77@gmail.com>
@ashokponkumar ashokponkumar merged commit 5c2e964 into konveyor:master Oct 19, 2020
@ashokponkumar
Copy link
Member

Thanks for the nice work with this PR @isabellaliu77 ! Feel free to submit more PRs for other issues, if you find time.

@ashokponkumar ashokponkumar linked an issue Oct 19, 2020 that may be closed by this pull request
@IsabellaDev
Copy link
Contributor Author

Glad I helped!

@HarikrishnanBalagopal HarikrishnanBalagopal added the kind/bug Categorizes issue or PR as related to a bug. label Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use regex substring to get portion of string
3 participants