Skip to content

schema cty types incompatible with hclwrite #1020

Open
@melinath

Description

@melinath

terraform-plugin-sdk has a dependency on github.com/hashicorp/go-cty/cty which is an out-of-date fork of github.com/zclconf/go-cty.

Expected Behavior

Data should be easy to convert from cty -> hcl using hclwrite.

Actual Behavior

schema is defined using an out-of-date forked version of go-cty, so the only way to export with hclwrite is to first convert the schema types manually from github.com/hashicorp/go-cty/cty -> github.com/zclconf/go-cty

Steps to Reproduce

import (
	"encoding/json"

	hashicorpcty "github.com/hashicorp/go-cty/cty"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/zclconf/go-cty/cty"
	ctyjson "github.com/zclconf/go-cty/cty/json"
)

func mapToCtyValWithSchema(m map[string]interface{}, s map[string]*schema.Schema) cty.Value {
	b, err := json.Marshal(&m)
	if err != nil {
		panic(fmt.Errorf("error marshaling map as JSON: %v", err))
	}
	ty := schema.InternalMap(s).CoreConfigSchema().ImpliedType()
	ret, err := ctyjson.Unmarshal(b, ty)
	if err != nil {
		panic(fmt.Errorf("error unmarshaling JSON as cty.Value: %v", err))
	}
	return ret
}

This will fail to compile because ctyjson requires a cty.Type; using ctyjson is necessary because hclwrite requires cty values as input.

References

hashicorp/hcl#546

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions