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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] new functions: startswith and endswith #63

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

alexjurkiewicz
Copy link

Example usage:

startswith(var.subnet_id, "subnet-")
startswith(var.service_name, "mycorp-")
endswith(var.hostname, var.corp_domain)

I'd like to get these into Terraform. The first step is implementing here, I guess.

These functions overlap with regex/regexall, but obligatory XKCD. Regular expressions are difficult to understand, even for simple prefix/suffix tests. You could also implement these functions with substr and length -- but the code is similarly impenetrable and even more verbose.

This PR is currently WIP. I'm looking for maintainer feedback before progressing it further 馃檹 .

  • Implement startswith
  • Test startswith
  • Implement endswith
  • Test endswith

@apparentlymart
Copy link
Collaborator

Hi @alexjurkiewicz! Thanks for working on these.

First I want to note that not all functions in Terraform have to be upstreamed here. I'm happy to accept these here because they are generic, but often functions start their life in Terraform's repository and then HashiCorp folks decide whether to submit them upstream into this repository (which isn't a HashiCorp project).

Looking at what you've done so far here, I have a note that will hopefully make your remaining work a little easier...

Because both of your parameter declarations have an exact type constraint of cty.String and don't set AllowUnknown: true, you can safely convert directly to Go strings and do all of the remaining work using Go's own functions. For example. the Go library has strings.HasPrefix which I think would simplify the prefix-check function considerably:

str := args[0].AsString()
prefix := args[1].AsString()

return cty.BoolVal(strings.HasPrefix(str, prefix)), nil

One of the invariants that cty guarantees is that strings are always encoded in a consistent Unicode normalization form, and so it should be safe to rely on the byte-oriented strings.HasPrefix: the str and prefix values will have already been Unicode-normalized so that the prefix comparison should behave the same way as a direct == would behave if str entirely matched prefix (without any additional characters).

I see you also changed the .travis.yml file here. While I do agree that it seems like time to switch to newer versions of Go for testing, I'd prefer to handle that myself as a separate change rather than having it mixed in to this PR. If there's some reason why that change blocks you completing this PR then let me know and I'll see about getting that done before we merge your PR here.

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.

None yet

2 participants