aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/ext/typeexpr/public.go
blob: e3f5eef592952c9e6ff845896d58125e12ef3e18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package typeexpr

import (
	"bytes"
	"fmt"
	"sort"

	"github.com/hashicorp/hcl2/hcl/hclsyntax"

	"github.com/hashicorp/hcl2/hcl"
	"github.com/zclconf/go-cty/cty"
)

// Type attempts to process the given expression as a type expression and, if
// successful, returns the resulting type. If unsuccessful, error diagnostics
// are returned.
func Type(expr hcl.Expression) (cty.Type, hcl.Diagnostics) {
	return getType(expr, false)
}

// TypeConstraint attempts to parse the given expression as a type constraint
// and, if successful, returns the resulting type. If unsuccessful, error
// diagnostics are returned.
//
// A type constraint has the same structure as a type, but it additionally
// allows the keyword "any" to represent cty.DynamicPseudoType, which is often
// used as a wildcard in type checking and type conversion operations.
func TypeConstraint(expr hcl.Expression) (cty.Type, hcl.Diagnostics) {
	return getType(expr, true)
}

// TypeString returns a string rendering of the given type as it would be
// expected to appear in the HCL native syntax.
//
// This is primarily intended for showing types to the user in an application
// that uses typexpr, where the user can be assumed to be familiar with the
// type expression syntax. In applications that do not use typeexpr these
// results may be confusing to the user and so type.FriendlyName may be
// preferable, even though it's less precise.
//
// TypeString produces reasonable results only for types like what would be
// produced by the Type and TypeConstraint functions. In particular, it cannot
// support capsule types.
func TypeString(ty cty.Type) string {
	// Easy cases first
	switch ty {
	case cty.String:
		return "string"
	case cty.Bool:
		return "bool"
	case cty.Number:
		return "number"
	case cty.DynamicPseudoType:
		return "any"
	}

	if ty.IsCapsuleType() {
		panic("TypeString does not support capsule types")
	}

	if ty.IsCollectionType() {
		ety := ty.ElementType()
		etyString := TypeString(ety)
		switch {
		case ty.IsListType():
			return fmt.Sprintf("list(%s)", etyString)
		case ty.IsSetType():
			return fmt.Sprintf("set(%s)", etyString)
		case ty.IsMapType():
			return fmt.Sprintf("map(%s)", etyString)
		default:
			// Should never happen because the above is exhaustive
			panic("unsupported collection type")
		}
	}

	if ty.IsObjectType() {
		var buf bytes.Buffer
		buf.WriteString("object({")
		atys := ty.AttributeTypes()
		names := make([]string, 0, len(atys))
		for name := range atys {
			names = append(names, name)
		}
		sort.Strings(names)
		first := true
		for _, name := range names {
			aty := atys[name]
			if !first {
				buf.WriteByte(',')
			}
			if !hclsyntax.ValidIdentifier(name) {
				// Should never happen for any type produced by this package,
				// but we'll do something reasonable here just so we don't
				// produce garbage if someone gives us a hand-assembled object
				// type that has weird attribute names.
				// Using Go-style quoting here isn't perfect, since it doesn't
				// exactly match HCL syntax, but it's fine for an edge-case.
				buf.WriteString(fmt.Sprintf("%q", name))
			} else {
				buf.WriteString(name)
			}
			buf.WriteByte('=')
			buf.WriteString(TypeString(aty))
			first = false
		}
		buf.WriteString("})")
		return buf.String()
	}

	if ty.IsTupleType() {
		var buf bytes.Buffer
		buf.WriteString("tuple([")
		etys := ty.TupleElementTypes()
		first := true
		for _, ety := range etys {
			if !first {
				buf.WriteByte(',')
			}
			buf.WriteString(TypeString(ety))
			first = false
		}
		buf.WriteString("])")
		return buf.String()
	}

	// Should never happen because we covered all cases above.
	panic(fmt.Errorf("unsupported type %#v", ty))
}