zclconf / go-cty

A type system for dynamic values in Go applications
MIT License
338 stars 70 forks source link

Parse string into cty type #162

Closed chriskolenko closed 1 year ago

chriskolenko commented 1 year ago

Is there any way to parse a string into a cty type?

Here's the string I have

list(object({
    domain_name         = string
    origin_id           = string
    origin_path         = optional(string)
    connection_attempts = optional(number)
    connection_timeout  = optional(number)
}))
apparentlymart commented 1 year ago

Hi @chriskolenko,

This library does have a function for parsing a standardized representation of types as JSON -- UnmarshalType -- but the syntax you've shared here seems like Terraform's type constraint syntax, which is outside of the scope of this library.

I would suggest instead studying the Terraform codebase to see how Terraform parses type constraint expressions like these. It should be possible in principle to replicate what Terraform does to arrive at a cty.Type value to pass to the "want" argument of convert.Convert, which is what Terraform ultimately does with the type constraints in its language.