-
Notifications
You must be signed in to change notification settings - Fork 18
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
Conversation
Signed-off-by: Isabella Liu <isabellaliu77@gmail.com>
main.go
Outdated
helmTargetArtifactTypeValue := "Helm" | ||
yamlsTargetArtifactTypeValue := "Yamls" | ||
knativeTargetArtifactTypeValue := "Knative" | ||
|
||
re := regexp.MustCompile(`artifacttype:\\s.*`) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 "".
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)]) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Thanks for the fix. Have suggested msome changes, please have a look. |
Signed-off-by: Isabella Liu <isabellaliu77@gmail.com>
main.go
Outdated
|
||
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) | ||
} | ||
|
There was a problem hiding this comment.
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]
}
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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*(.+)`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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
if len(artifactTypematch) == 0 { | ||
artifactType = "" | ||
} else { | ||
|
||
artifactType = strings.ReplaceAll(artifactTypematch[1], " ", "") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
Thanks for the nice work with this PR @isabellaliu77 ! Feel free to submit more PRs for other issues, if you find time. |
Glad I helped! |
Hi, sorry for the delay, please check if this what you need, let me know if you need me to do any modification. Thanks.