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

Bug: Cannot use keys containing hyphens in TypeSet. #1641

Closed
tpounds opened this issue Apr 23, 2015 · 6 comments · Fixed by #1654
Closed

Bug: Cannot use keys containing hyphens in TypeSet. #1641

tpounds opened this issue Apr 23, 2015 · 6 comments · Fixed by #1654

Comments

@tpounds
Copy link
Contributor

tpounds commented Apr 23, 2015

It appears you cannot use keys containing hyphens inside of a TypeSet. It appears this behavior is a result of some logic introduced in 83c760f to include item hashcodes within the key.

https://github.com/hashicorp/terraform/blob/master/helper/schema/schema.go#L820
https://github.com/hashicorp/terraform/blob/master/helper/schema/schema.go#L835

I suspect the s/-/~/ magic exists because the hash function signature returns a signed integer.

Set: func(v interface{}) int {
   return -1234
},

I've provided a minimum example that can reproduce the behavior.

main.tf:

resource "foo_resource" "myFoo" {
   myTypeSet = {
      key-with-hyphens = "baz"
   }
}

foo_resource.go:

"myTypeSet": &schema.Schema{
    Type:     schema.TypeSet,
    Optional: true,
    Elem: &schema.Resource{
        Schema: map[string]*schema.Schema{
            "key-with-hyphens": &schema.Schema{
                Type:     schema.TypeString,
                Optional: true,
            },
        },
    },
}

terraform apply stdout:

foo_resource.myFoo: Creating...
  myTypeSet.#:                   "0" => "1"
  myTypeSet.0.key~with~hyphens:  "" => ""
@tpounds tpounds changed the title Cannot use keys containing hyphens in TypeSet. [Bug] Cannot use keys containing hyphens in TypeSet. Apr 23, 2015
@tpounds tpounds changed the title [Bug] Cannot use keys containing hyphens in TypeSet. Bug: Cannot use keys containing hyphens in TypeSet. Apr 23, 2015
@tpounds
Copy link
Contributor Author

tpounds commented Apr 23, 2015

FWIW, it looks like the internal helper hashcode String function enforces non-negative integers. This was enforced by commit a5040ec.

https://github.com/hashicorp/terraform/blob/master/helper/hashcode/hashcode.go#L14

If changing existing hash functions is not an option I think enforcing non-negative hashcodes would be a backwards compatible code change. All bets are off for any hashcodes stored in local state.

@mitchellh
Copy link
Contributor

The schema you entered is not valid: it is missing the hash function. I'm assuming this is an oversight. But anyways, I still don't see the problem here? We make sure the hash is positive (if its negative we multiple by -1, effectively). How does this affect things?

@tpounds
Copy link
Contributor Author

tpounds commented Apr 23, 2015

Oops. It's definitely an oversight. I copied the schema from some code and redacted a lot of internal stuff for the bug report. After looking at the code a bit more I couldn't trace how the hashcodes are related and added to the set keys. What I observe is that any key that contains a '-' is eventually replaced by the '~' per lines in schema.go@820/835 and it looks like this behavior was deliberately done by design. Unfortunately, I'm not familiar enough with the code to know what the intended behavior is supposed to be. I'll dig a bit further to see if I can find out what is happening in more detail.

@mitchellh
Copy link
Contributor

Got it, I have a feeling I know what you're getting at now. Thanks for the clarification!

@tpounds
Copy link
Contributor Author

tpounds commented Apr 23, 2015

I've submitted a pull request that mitigates the issue for arbitrary keys containing hyphens. Let me know if you see any issues with it.

@ghost
Copy link

ghost commented May 3, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants