Skip to content

add template processing builtin#110

Merged
hinshun merged 1 commit intoopenllb:masterfrom
coryb:template
Apr 19, 2020
Merged

add template processing builtin#110
hinshun merged 1 commit intoopenllb:masterfrom
coryb:template

Conversation

@coryb
Copy link
Copy Markdown
Contributor

@coryb coryb commented Apr 19, 2020

adding a basic templating function. can be used like:

string secretPath() {
  template <<-EOM
    {{- if . -}}
      /path/to/ci/secret
    {{- else -}}
      /path/to/user/secret
    {{- end -}}
  EOM string {
    localEnv "CI"
  }
}

The part I don't like about this is the argument data into the template. The PR currently passes the variadic arguments as a []string slice when there is more than one argument to template, otherwise it will pass just a single string argument if only 1 variadic arg. I am not sure there is a more logical approach. If we wanted to pass named arguments to allow for {{ .name }} we would have to have some syntax that supports maps, which we dont have yet.

Copy link
Copy Markdown
Contributor

@hinshun hinshun left a comment

Choose a reason for hiding this comment

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

An alternative is to use with option like a map. For example:

string secretPath() {
  template <<-EOM
    {{- if .CI -}}
      /path/to/ci/secret
    {{- else -}}
      /path/to/user/secret
    {{- end -}}
  EOM with option {
        stringField "CI" string { localEnv "CI"; }
  }
}

Comment thread language/builtin.hlb Outdated
# For template syntax documentation see:
# https://golang.org/pkg/text/template/
#
# @param the text of the template
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@param needs to be followed by text

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@coryb
Copy link
Copy Markdown
Contributor Author

coryb commented Apr 19, 2020

string secretPath() {
  template <<-EOM
    {{- if .CI -}}
      /path/to/ci/secret
    {{- else -}}
      /path/to/user/secret
    {{- end -}}
  EOM with option {
        stringField "CI" string { localEnv "CI"; }
  }
}

I thought about it a while, I think this makes the most sense. It will be less cumbersome when we fix #100 and I think it provides flexibility going forward.

I will update the PR for option::template

@coryb
Copy link
Copy Markdown
Contributor Author

coryb commented Apr 19, 2020

@hinshun updated PR with option::template

Copy link
Copy Markdown
Contributor

@hinshun hinshun left a comment

Choose a reason for hiding this comment

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

Ship it!

@hinshun hinshun merged commit 2c72ec9 into openllb:master Apr 19, 2020
@coryb coryb deleted the template branch December 19, 2020 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants