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

fixed Contains stdlib function #46

Merged
merged 1 commit into from
Mar 3, 2020
Merged

fixed Contains stdlib function #46

merged 1 commit into from
Mar 3, 2020

Conversation

jbardin
Copy link
Contributor

@jbardin jbardin commented Feb 24, 2020

Contains was transferred from Terraform, but the Index function which it
was calling has the inverse meaning than the Terraform function.

@codecov
Copy link

codecov bot commented Feb 24, 2020

Codecov Report

Merging #46 into master will increase coverage by 0.13%.
The diff coverage is 72.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #46      +/-   ##
==========================================
+ Coverage   69.92%   70.06%   +0.13%     
==========================================
  Files          78       78              
  Lines        6987     7001      +14     
==========================================
+ Hits         4886     4905      +19     
+ Misses       1668     1659       -9     
- Partials      433      437       +4
Impacted Files Coverage Δ
cty/function/stdlib/collection.go 19.55% <72.22%> (+2.33%) ⬆️
cty/convert/compare_types.go 100% <0%> (+2.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 089490b...fca3b0d. Read the comment docs.

Copy link
Collaborator

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

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

This looks good to me! Just one small thing inline...

}

if args[0].LengthInt() == 0 {
return cty.NilVal, errors.New("cannot search an empty list or set")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure what the equivalent Terraform function was doing here, but it feels more useful to me for this case to return false so that callers don't have to treat an empty collection as a special case. 🤔

I'd prefer to do that here in cty even if that does mean that Terraform needs to keep its own separate implementation for now to retain the previous behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this was specifically a carry-over from terraform. I think we can even accept this directly in terraform, since there's no working case that would be relying on the error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like this check was only ever in Terraform because it was using the Index function internally, so by writing out the implementation directly here we can "fix the glitch"! 🙃

_, v := it.Element()
// if we don't find a match, but have an unknown element, we need
// to return unknown
if !v.IsKnown() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This special case doesn't really hurt, and I'm okay with keeping it, but just wanted to note that eq := args[1].Equals(v) followed by eq.IsKnown() would've been sufficient to cach this, because Equals is an "operation method" rather than an "integration method", as defined in the cty concepts doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I will clean this up since I want to make the change above too.

Contains was transferred from Terraform, but the Index function which it
was calling has the inverse meaning than the Terraform function.
@apparentlymart apparentlymart merged commit 3a0df52 into zclconf:master Mar 3, 2020
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