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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Alma-media
Copy link

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 🤔

@hashicorp-cla
Copy link

hashicorp-cla commented Nov 27, 2020

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.

Base automatically changed from hcl2 to main February 10, 2021 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants