Idiomatic way of converting Golang complex structs to Terraform storage format

I’m currently working on improving Terraform provider for my project (https://github.com/flexkube/libflexkube/pull/66/files) and I struggle to find a good pattern for converting my projects structs into Terraform scheme, so I can store it and present user a nice diff if some condition changes. I’ve tried looking into other providers source, but I couldn’t find any good examples of how to do that.

It would be great to be able to use Go struct tags or something to handle that, so I could avoid so much of the boilerplate code.

Could you point me to some examples/libraries/helpers, which can simplify such process?

1 Like

At the moment, all HashiCorp-maintained providers do this by hand. We do not have official tooling that supports using struct tags to turn structs into schema, or modeling schema as a struct at all. This is something I’m vaguely interested in, but there are some complications to that that make it harder than it appears at first blush. For example, defining validation functions on attributes is tricky if they’re just properties on a struct.

this could be of interest maybe

1 Like

Hi! This is very good news.
Just a question. Does this follow the SDKv2 format or the new framework structure?
I have done something similar, but from scratch, without using any library, except for the standard json Go library.

Hi, thanks qesbalcha for posting this. I’m the developer of this library.

This follows the SDKv2 format. It basically translates Go structs into OpenAPI definitions (to get dependencies between structs) and then to Terraform schema.

It’s still in early stages and isn’t production ready yet.

1 Like

Is it opensource? Can you share?