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

Helper Functions for custom annotations #691

Merged
merged 3 commits into from
Sep 17, 2019
Merged

Conversation

ampant
Copy link
Contributor

@ampant ampant commented Sep 11, 2019

Proposed changes

Describe the use case and detail of the change. If this PR addresses an issue on GitHub, make sure to include a link to that issue here in this description (not in the title of the PR).

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto master
  • I will ensure my PR is targeting the master branch and pulling from my branch from my own fork

@ampant ampant added the enhancement Pull requests for new features/feature enhancements label Sep 12, 2019
@ampant ampant self-assigned this Sep 12, 2019
Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

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

@ampant please see my comments. some small fixes are required.

)

// SplitInput splits the input from "," and returns an array of strings
func SplitInput(s string, delim string) []string {
Copy link
Contributor

Choose a reason for hiding this comment

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

there is no need for these functions to be public - they are not used in other packages

return strings.TrimSpace(s)
}

// HelperFunctions to parse the annotations
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest removing this comment

@@ -86,6 +86,37 @@ If you'd like to use custom annotations with Mergeable Ingress resources, please

* Minions do not inherent custom annotations of the master.

### Helper Functions

Helper functions can be used in custom templates to transform the value of custom annotations.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Helper functions can be used in custom templates to transform the value of custom annotations.
Helper functions can be used in the Ingress template to parse the values of custom annotations.


Helper functions can be used in custom templates to transform the value of custom annotations.

| Function | Description |
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest adding two additional columns to better document the functions.

Suggested change
| Function | Description |
| Function | Input Arguments| Return Arguments | Description |

Additionally, see the following suggestions:

| splitinput | s, sep string | []string | Splits the string s into a slice of strings separated by the sep. | triminput | s string | string | Trims the trailing and leading whitespace from the string s. |

| `splitinput` | Splits the arguments at specified delimiter and returns an array of strings |
| `triminput` | Trims the trailing and leading whitespaces from the arguments |

For example, the following custom annotation custom.nginx.org/allowed-ips represents a comma separated list of IP addresses as a string.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
For example, the following custom annotation custom.nginx.org/allowed-ips represents a comma separated list of IP addresses as a string.
Consider the following custom annotation `custom.nginx.org/allowed-ips`, which expects a comma-separated list of IP addresses:

}

func TestTrimHelperFunctions(t *testing.T) {
const tpl = `{{triminput (index .)}}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const tpl = `{{triminput (index .)}}`
const tpl = `{{triminput .}}`

tmpl, err := template.New("testTemplate").Funcs(helperFunctions).Parse(tpl)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

consider adding a blank line here to improve readability. Same applies for TestTrimHelperFunctions

slice := "foo,bar"
expected := "foo bar "
err = tmpl.Execute(&buf, slice)
t.Log(buf.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to print the buffer? Same applies for TestTrimHelperFunctions

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 other tests are following same pattern to print buf if it fails. So did same here. Let me know if it shoud be removed

Copy link
Contributor

Choose a reason for hiding this comment

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

Consider removing it here.
The other tests do not check the generated config against the expected output -- they only check that there is no error returned. That's mostly why t.Log(buf.String()), so that the output could be checked visually. In contrast, this test checks the output against the expected output, so no printing is necessary.

err = tmpl.Execute(&buf, slice)
t.Log(buf.String())
if err != nil {
t.Fatalf("Failed to write template %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

failed to execute the template?

}
var buf bytes.Buffer

slice := "foo,bar"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think input would suit better for the name of this variable. Same applies for TestTrimHelperFunctions

Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. Looks good. Please see a couple of additional suggestions.

slice := "foo,bar"
expected := "foo bar "
err = tmpl.Execute(&buf, slice)
t.Log(buf.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider removing it here.
The other tests do not check the generated config against the expected output -- they only check that there is no error returned. That's mostly why t.Log(buf.String()), so that the output could be checked visually. In contrast, this test checks the output against the expected output, so no printing is necessary.

| `triminput` | Trims the trailing and leading whitespaces from the arguments |
| Function | Input Arguments| Return Arguments | Description |
| -------- | -------------- | ---------------- | ----------- |
| `split` | s, sep string | []string | Splits the string s into a slice of strings separated by the sep. |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
| `split` | s, sep string | []string | Splits the string s into a slice of strings separated by the sep. |
| `split` | `s, sep string` | `[]string` | Splits the string `s` into a slice of strings separated by the `sep`. |

| Function | Input Arguments| Return Arguments | Description |
| -------- | -------------- | ---------------- | ----------- |
| `split` | s, sep string | []string | Splits the string s into a slice of strings separated by the sep. |
| `trim` | s string | string | Trims the trailing and leading whitespace from the string s. |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
| `trim` | s string | string | Trims the trailing and leading whitespace from the string s. |
| `trim` | `s string` | `string` | Trims the trailing and leading whitespace from the string `s`. |

// SplitInput splits the input from "," and returns an array of strings
func SplitInput(s string, delim string) []string {
// splitinput splits the input from "," and returns an array of strings
func splitinput(s string, delim string) []string {
Copy link
Contributor

@pleshakov pleshakov Sep 13, 2019

Choose a reason for hiding this comment

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

For multiword names, the convention is to use mixed caps. Like "splitInput". See https://golang.org/doc/effective_go.html#mixed-caps
At the same time, as mentioned here #691 (comment), I suggest renaming to split. This will make the name simpler, as the suffix input seems unnecessary.
Same applies for triminput.

var helperFunctions = template.FuncMap{
"splitinput": SplitInput, //returns array of strings
"triminput": TrimInput, //returns string with trimmed leading and trailing spaces
"split": splitinput, //returns array of strings
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest removing these comments completely. The comment for split s already outdated. Having the docs in the doc is enough.

// SplitInput splits the input from "," and returns an array of strings
func SplitInput(s string, delim string) []string {
// splitinput splits the input from "," and returns an array of strings
func splitinput(s string, delim string) []string {
Copy link
Contributor

Choose a reason for hiding this comment

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

The documentation for the function is not correct. I suggest removing it complety - the function is private and also strait-forward.
Same applies for triminput.

Copy link
Contributor

@pleshakov pleshakov left a comment

Choose a reason for hiding this comment

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

👍

@ampant ampant merged commit 6b1130d into master Sep 17, 2019
@ampant ampant deleted the feature/helperfunctions branch September 18, 2019 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Pull requests for new features/feature enhancements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants