Comments (6)
Hi @monegim,
It's an intentional design decision that all strings in cty
-- which includes map keys and the attribute names of an object type -- are always stored in normalized form. This is one of the many invariants that comes from cty
's focus on standardization of representations and a focus on configuration-language-related use-cases. I don't wish to complicate things further by introducing the possibility that some values may not have been normalized, since that would then require robust calling applications to always handle both cases.
I'd be interested to learn more about your underlying goal though, in case I can propose an alternative way to achieve it without modifying cty
. Thanks!
from go-cty.
Thank you for getting back to me. I am trying to solve the How to convert (write) hcl files as tfvars? question.
This is a part of tfvars
file I wanted to generate from a hcl
file:
firewall = {
"16abbc94-4048" = {
action = "allow"
domain = "example.com"
filter_expr = "((ip.src == 1.1.1.1))"
is_enabled = true
name = "Simple"
}
Reading a hcl
file, converting to cty.Value
and then printing as the tfvars
file. The code for outputting this is as follows:
func Hcl2CtyValue(hclFile *hcl.File, spec hcldec.Spec) cty.Value {
value, diags := hcldec.Decode(hclFile.Body, spec, nil)
if diags.HasErrors() {
log.Fatal(diags.Error())
}
return value
}
func HCLWriter(obj cty.Value) []byte {
f := hclwrite.NewEmptyFile()
body := f.Body()
for it := obj.ElementIterator(); it.Next(); {
k, v := it.Element()
name := k.AsString()
// Purify
// Iterate through Ids
reconstructedValueMap := make(map[string]cty.Value, 0)
for resources := v.ElementIterator(); resources.Next(); {
id, resource := resources.Element()
idString := id.AsString()
reconstructedValueMap[fmt.Sprintf("%q", idString)] = resource
}
body.SetAttributeValue(name, cty.ObjectVal(reconstructedValueMap))
}
return f.Bytes()
}
but it would generate this:
firewall = {
"\"16abbc94-4048\"" = {
action = "allow"
domain = "example.com"
filter_expr = "((ip.src == 1.1.1.1))"
is_enabled = true
name = "Simple"
}
As you can see, normalizing would add \"
to the key. I cannot access to the cty.Value
I can remove the NormalizeString
func.
And this is the module for using the tfvars
as a variable file:
resource "firewall" "this" {
for_each = {
for k,v in var.firewall : k => v
if var.enable_firewall
}
domain = each.value.domain
name = each.value.name
filter_expr = each.value.filter_expr
is_enabled = each.value.is_enabled
action = each.value.action
}
from go-cty.
Hi @monegim,
In the code you shared you are constructing the key using fmt.Sprintf("%q", idString)
, which I believe is what adds these extra quotes.
cty
string normalization does not add quotes; it only ensures that the unicode characters in the given string are encoded in a predictable way so that rune-based string manipulation will always behave the same way for a given string.
I expect that if you change that line to the following then you will no longer produce these extra quotes:
reconstructedValueMap[idString] = resource
from go-cty.
Removing fmt.Sprintf("%q", idString)
does not print " "
at all. Something like this would be generated:
firewall = {
# Note double quotes are not printed:
16abbc94-4048 = {
action = "allow"
domain = "example.com"
filter_expr = "((ip.src == 1.1.1.1))"
is_enabled = true
name = "Simple"
}
from go-cty.
You are describing a formatting decision made by hclwrite
, which is separate from any behavior in this repository. There's nothing you can do using the cty
API to influence how hclwrite
decides to render a value, because cty
knows nothing about HCL.
It doesn't make any practical difference whether or not that attribute name is written in quotes. Terraform (really: HCL) understands it the same either way.
from go-cty.
You are right. Thank you for your response. I am going to close this issue.
from go-cty.
Related Issues (20)
- 1.11.0 causes errors with //go:generate packer-sdc mapstructure-to-hcl2 HOT 1
- hashicorp/packer-plugin-sdk incompatible with zclconf/go-cty v1.11.0 HOT 4
- TestFormatDate fails with upcoming Go 1.20 release HOT 5
- Add support for decoding into structs with a custom tag HOT 1
- Nested go struct to cty value fails HOT 1
- Proposal: JSON serialization of `cty.Path` HOT 3
- cty/json: configure (non-)HTML-escaping serialization HOT 3
- stdlib: SetProductFunc doesn't seem to handle refinements quite right HOT 2
- How to convert cty.Value of unknown type into `interface{}` HOT 2
- cty.StringVal always doubles $ in `${}` output HOT 2
- Parse string into cty type HOT 1
- cty.StringVal always doubles $ in ${} output Pt2 HOT 1
- adding jsonencode() block into a generated terraform file using golang cty HOT 2
- Encoding values with custom function (terraform provider) HOT 1
- Marshal() generate invalid JSON HOT 2
- panic with `AsString()` on result of json encoding of to_number conversion of null value HOT 4
- Some valid `Path` values cannot be applied to their source `Value` HOT 2
- Add a deepmerge function HOT 1
- Some method to avoid the lexicographical order when parsing tokens HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-cty.