hcl icon indicating copy to clipboard operation
hcl copied to clipboard

allow interface/[]interface types to be encoded if interface is struct type

Open Alma-media opened this issue 5 years ago • 1 comments

We use HCL encoder to build dynamic configs for our services, unfortunately, there is no ability to use different types in the block. Let's say we have App config with additional options and couple of possible types:

type App struct {
	. . .
	Option []interface{} `hcl:"option,block"`
}

type HTTPOptions struct {
	Listener string `hcl:"listener,label"`
	Address  string `hcl:"address"`
	Secure   bool   `hcl:"secure"`
}

type MQTTOptions struct {
	Listener string   `hcl:"listener,label"`
	Address  string   `hcl:"address"`
	Topics   []string `hcl:"topics"`
}

We would like to be able to provide all of them:

var config = App{
	Options: []interface{}{
		HTTPOptions{
			Listener: "http",
			Address:  ":8080",
		},
		MQTTOptions{
			Listener: "mqtt",
			Address:  ":1883",
			Topics:   []string{"foo", "bar"},
		},
	},
} 

Desired output:

option "http" {
  address = ":8080"
  secure  = false
}
option "mqtt" {
  address = ":1883"
  topics  = ["foo", "bar"]
}

Or maybe someone could suggest more elegant way :thinking:

Alma-media avatar Nov 27 '20 16:11 Alma-media

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes


Alex Sch seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

hashicorp-cla avatar Nov 27 '20 16:11 hashicorp-cla