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

Use a new vars template implementation #19156

Closed
wants to merge 6 commits into from

Conversation

lunny
Copy link
Member

@lunny lunny commented Mar 21, 2022

This PR reimplemented Expand in modules/templates/vars and replace all places.

@lunny lunny added the type/enhancement An improvement of existing functionality label Mar 21, 2022
@lunny lunny added this to the 1.17.0 milestone Mar 21, 2022
@codecov-commenter

This comment was marked as outdated.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 21, 2022
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 21, 2022
wxiaoguang
wxiaoguang previously approved these changes Mar 21, 2022
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 21, 2022
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Mar 21, 2022

PR:

(now it has a separate PR 19284)

Changes:

  1. Add com to deny list
  2. Improve vars.Expand
    1. it always returns the rendered string (even if there is an error)
    2. the key processing is allocation-free, and bytes are written in string batches, faster.
    3. support escaped chars, and now we have the ability to extend the syntax in future by the reserved char #
  3. In some cases, always show rendered text to end users, avoid unnecessary 500 erros
  4. Fix some errors in golangci.yml

Maybe the title of this PR could be changed accordingly: Refactor legacy com(common) package and improve the simple string template Expand

@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Mar 21, 2022
@wxiaoguang wxiaoguang removed the lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. label Mar 21, 2022
@lunny
Copy link
Member Author

lunny commented Mar 21, 2022

PR:

Changes:

  1. Add com to deny list
    This could be another PR.

  2. Improve vars.Expand

    1. it always returns the rendered string (even if there is an error)
      I prefer a strict mode.
    2. the key processing is allocation-free, and bytes are written in string batches, faster.
      I have changed my code, now it's also allocation-free for key processing.
    3. support escaped chars, and now we have the ability to extend the syntax in future by the reserved char #
      Why we need that? The key should not contain {.
  3. In some cases, always show rendered text to end users, avoid unnecessary 500 erros
    Hiding the error means we catch the error more later.

  4. Fix some errors in golangci.yml
    This could be another PR.

Maybe the title of this PR could be changed accordingly: Refactor legacy com(common) package and improve the simple string template Expand
I will remove all changes about com to keep this PR clean.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Mar 21, 2022

PR:

Changes:

  1. Add com to deny list
    This could be another PR.

Why can not the refactoring of the legacy com in one PR?

  1. Improve vars.Expand

    1. it always returns the rendered string (even if there is an error)
      I prefer a strict mode.

Strict mode doesn't work here because reason 3

  1. the key processing is allocation-free, and bytes are written in string batches, faster.
    I have changed my code, now it's also allocation-free for key processing.
  2. support escaped chars, and now we have the ability to extend the syntax in future by the reserved char #
    Why we need that? The key should not contain {.

How about there is some {xxx} or {} in README template during rendering? Can you guarantee that the content of README never contain non-var keys?

And The key should not contain {, but users may want to render a separate { or {}.

  1. In some cases, always show rendered text to end users, avoid unnecessary 500 erros
    Hiding the error means we catch the error more later.

I never hide errors, I always output them. But in some cases they should not be shown to users in the web, for example, issue URL rendering or README rendering.

  1. Fix some errors in golangci.yml
    This could be another PR.

Hmm .... so many PRs, then no PRs.

Maybe the title of this PR could be changed accordingly: Refactor legacy com(common) package and improve the simple string template Expand
I will remove all changes about com to keep this PR clean.

IIRC other maintainers have talked about we should avoid force-pushing when review starts.

modules/context/repo.go Outdated Show resolved Hide resolved
modules/context/repo.go Outdated Show resolved Hide resolved
routers/web/repo/issue.go Outdated Show resolved Hide resolved
routers/web/goget.go Outdated Show resolved Hide resolved
modules/templates/vars/vars.go Show resolved Hide resolved
}
}

if len(match) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

  1. Why is that an error case? I strongly disagree that not finding the corresponding variable should return an error.
  2. Even if I count that as an error case, it is an unnecessary one as that will already be tested down below with the v, ok := match[template[keyStartPos+1:i]] implicitly.
  3. Even more so, it is implemented incorrectly as it disregards the positional arguments.

modules/templates/vars/vars.go Show resolved Hide resolved
return "", err
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Currently, there is an edge case missing, as an unclosed parentheses at the end (i.e. test {template end as template) would not throw a syntax error.

fail: true,
},
{
template: "expand }, {b} and {c}",
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned above, the other case is currently missing.

}

// Expand replaces all variables like {var} to match
func Expand(template string, match map[string]string, subs ...string) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

You could save all the extra trouble with the positional arguments by using something like

Suggested change
func Expand(template string, match map[string]string, subs ...string) (string, error) {
// All positional arguments will be converted into corresponding map entries: match["x"] = subs[x])
func Expand(template string, match map[string]string, subs ...string) (string, error) {
for index, substitution := range subs {
match[strconv.Itoa(index)] = substitution
}

@lunny lunny removed this from the 1.17.0 milestone Apr 2, 2022
@lunny
Copy link
Member Author

lunny commented Apr 2, 2022

replaced by #19284

@lunny lunny closed this Apr 2, 2022
@lunny lunny deleted the lunny/vars_expand branch April 2, 2022 00:47
@go-gitea go-gitea locked and limited conversation to collaborators Apr 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants