hashicorp / hashicorp/terraform-plugin-framework
Feature Request (Ready to contribute): Add utility functions for converting between Go native types and basetypes.DynamicValue
- Dominant language
- Go
- Stars
- 384
- Forks
- 107
- Avg merge
- 3m
- Merged PRs (30d)
- 1
Description
**Summary**
I'd like to propose adding two utility functions — ToTFDynamicValue and FromTFDynamicValue — that simplify conversion between Go's native type system and Terraform's basetypes.DynamicValue. These functions significantly reduce boilerplate when writing Terraform providers in Go.
**Problem Statement**
When developing Terraform providers, a common pain point is bridging Go's native types (string, int, map[string]interface{}, etc.) with Terraform's type system (basetypes.DynamicValue). Currently, provider authors must manually handle this conversion, which involves:
- Understanding the internal representation of basetypes.DynamicValue
- Writing repetitive marshalling/unmarshalling logic
- Handling edge cases (nil values, nested structures, type mismatches)
- Maintaining custom conversion code across multiple providers
This creates unnecessary friction and is a source of subtle bugs.
**Proposed Solution**
Add two exported utility functions:
```
// ToTFDynamicValue converts a native Go value (string, int, bool, map, slice, etc.)
// into a basetypes.DynamicValue suitable for use in Terraform provider schemas.
func ToTFDynamicValue(value interface{}) (basetypes.DynamicValue, error)
// FromTFDynamicValue converts a basetypes.DynamicValue back into a native Go value,
// making it easy to work with provider data using standard Go idioms.
func FromTFDynamicValue(value basetypes.DynamicValue) (interface{}, error)
```
**Key design decisions:**
- Functions return error to allow graceful handling of unsupported or malformed types
- Accept/return interface{} for maximum flexibility with Go's type system
- Handles nested structures (maps of maps, slices of maps, etc.)
**Use Cases**
1. Provider developers who need to accept arbitrary/dynamic user input and process it in Go
2. Testing utilities where you need to quickly construct DynamicValue instances from Go literals
3. Data transformation layers within providers that bridge external API responses into Terraform state
**Willingness to Contribute**
I have a working implementation of these functions that I've been using in my own provider development. I'm happy to submit a pull request with:
- The implementation
- Unit tests covering core types, nested structures, nil handling, and error cases
- Documentation and GoDoc comments
**Additional Context**
Go version tested: 1.25.6
terraform-plugin-framework version: v1.17.0
Contributor guide
Assessment
This issue has not been assessed yet.