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

How to convert object to go's map #77

Closed
faruqisan opened this issue Nov 11, 2020 · 2 comments
Closed

How to convert object to go's map #77

faruqisan opened this issue Nov 11, 2020 · 2 comments

Comments

@faruqisan
Copy link

Hi, I'm trying to parse cty.Object to go's map

if variable.Default.Type().IsObjectType() {
        defaultValue, err = formatDefaultObjectType(variable.Default)
	if err != nil {
		log.Fatal("[terraform][formatDefaultObjectType] ", err)
	}
}

func formatDefaultObjectType(v cty.Value) (string, error) {
	var (
		result string
		err    error
	)

	raw := make(map[string]interface{})

	err = gocty.FromCtyValue(v, &raw)
	if err != nil {
		return "", err
	}

	result = fmt.Sprintf("%+v", raw)

	return result, nil
}

got this error

map or object value is required

I already pass the map to FromCtyValue, and also try to change map key type to interface{} and the error still same.

@apparentlymart
Copy link
Collaborator

Hi @faruqisan,

If by "object" you mean a value whose type is an object type, gocty can currently decode object values only into struct types with cty: tags on the fields, so that the field types can give gocty the hints about what types you expect for each of the attributes. The existing issue #17 is talking about this, although when I wrote that I was imagining converting an object into a map of an explicit element type, rather than into interface{}; gocty can't currently decode values into interface{} because the mapping from cty to Go type system is not one-to-one and interface{} doesn't give enough information about what type you want the result to have.

With all of that said, in your example here it seems like your underlying goal is just to print out a string representation of the value. If so, there are some other ways to get that done without first converting into a Go map:

  • You can use a %#v format directly on the value to produce a pseudo-Go-syntax compact representation, although it is not particularly readable for complex object types because it's presented all on one line.
  • The related other module github.com/zclconf/go-cty-debug has a function ValueString which is similar to the default %#v formatting but it uses newlines and indentation to hopefully make the result more readable when it's a complex data structure.

@faruqisan
Copy link
Author

Thanks, @apparentlymart ,

I saw in this docs that conversion from object to map is a safe operation that’s why I tried this.

Like what you said, my goal is just to print out a value in a string form, and I will try to use %#v or trying github.com/zclconf/go-cty-debug

I will close this issue

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

2 participants