hashicorp / hcl

HCL is the HashiCorp configuration language.
Mozilla Public License 2.0
5.27k stars 592 forks source link

blocks and labels representation #301

Open gdzx opened 6 years ago

gdzx commented 6 years ago

Hi !

Is it possible to write a schema so that the following representations are equivalent? (I looked at the way it is handled in Terraform, but I'm using another language so I would like to stick with something like hcldec).

# terraform-like provisioning
provision "file" {
    src = "foo"
    dst = "/bar"
}

provision "exec" {
    cmd = "/bar"
}

The label is used to internally tag the object with the type key, thus keeping the declaration order:

{
    "provision": [
        {"type": "file", "src": "foo", "dst": "/bar"},
        {"type": "exec", "cmd": "/bar"}
    ]
}

It would be a kind of enum-like block list.

Thanks !

apparentlymart commented 6 years ago

Hi @gdouezangrard!

I think you're talking about the hcldec command line tool. Is that right?

That's still rather experimental, and limited by the capabilities of the underlying hcldec package. There is the beginning of some mechanism in that package to treat the block label as just another property inside an object but it's not yet fully working because the development so far has, to be honest, been driven primarily by Terraform's needs. (Terraform will use hcldec internally, but only for the "dynamic" portions of the configuration whose schema is driven by provider plugins, and not for fixed structures like the provisioner blocks which I think you are modelling this after.)

This is something that I'd like to support but my bandwidth is currently taken up by integrating HCL2 into Terraform and so I won't be able to work on it for a while.

Here is how it might look in the spec language for the hcldec tool, once implemented:

object {
  blocks "provision" {
    object {
      block_label "type" {
      }
      attr "src" {
      }
      attr "dst" {
      }
      attr "cmd" {
      }
  }
}

Another limitation of hcldec for this use-case is the ability to select a different nested schema depending on the label, so you could represent the fact that provision "file" has a different schema than provision "exec". So far that sort of thing has generally been handled in the logic of a calling application rather than directly in the hcldec system, and would require some more thought to figure out the best way to represent that in a declarative specification language.

gdzx commented 6 years ago

Thank you for your response.

Indeed I was talking about the hcldec command, to easily use and validate HCL with any JSON parsing library.

I'll look at the progress from time to time, but if after integrating it into Terraform you have time to implement this kind of nested schema, that would be great :smiley:.

Thanks for your time !

olorin37 commented 1 year ago

Hei,

Few years has passed. Is it plausible this case will be handled by hcldec? What alternative I have if I would like to have such configuration blocks? only using gohcl?