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

Expose marshaller interfaces for expressions and blocks #607

Open
ropnop opened this issue Apr 21, 2023 · 0 comments
Open

Expose marshaller interfaces for expressions and blocks #607

ropnop opened this issue Apr 21, 2023 · 0 comments

Comments

@ropnop
Copy link

ropnop commented Apr 21, 2023

Hello -

I'd like to propose an enhancement that would allow me to use this library and extend with custom functionality in my own projects.

I would like to define 2 marshaler interfaces in the library that I can implement in my own project:

// ExpressionMarshaler defines an interface for marshaling a field type as an HCL expression.
type ExpressionMarshaler interface {
	MarshalExpression() (*hclwrite.Expression, error)
}

// BlockMarshaler defines an interface for marshalling a type to an *hclwrite.Block
// It can be used to implement custom hcl block writing for structs and types, including
// when used as fields with the block tag
type BlockMarshaler interface {
	MarshalBlock(blockType string) *hclwrite.Block
}

In the EncodeAsBlock and populateBody functions, I'd like to then check if the interface{} passed in implements these respective interfaces and if it does, just call the function.

This would allow other consumers of this library to extend/customize the marshalling behavior when using hclwrite for custom types and structs.

I have a working fork of this and happy to open a PR, but wanted to start a discussion here first if you are okay with this approach.

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

No branches or pull requests

1 participant