hashicorp / hashicorp/hcl

Expose marshaller interfaces for expressions and blocks

Open
#607 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

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](https://github.com/hashicorp/hcl/blob/main/gohcl/encode.go#L63) and [populateBody](https://github.com/hashicorp/hcl/blob/main/gohcl/encode.go#L51) 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.