Code Monkey home page Code Monkey logo

Comments (6)

apparentlymart avatar apparentlymart commented on July 30, 2024

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.

monegim avatar monegim commented on July 30, 2024

Hi @apparentlymart

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.

apparentlymart avatar apparentlymart commented on July 30, 2024

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.

monegim avatar monegim commented on July 30, 2024

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.

apparentlymart avatar apparentlymart commented on July 30, 2024

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.

monegim avatar monegim commented on July 30, 2024

You are right. Thank you for your response. I am going to close this issue.

from go-cty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.