aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/map_type.go
blob: ae9abae040da1039ebaf693eaeda6bf295894d6b (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
package cty

import (
	"fmt"
)

// TypeList instances represent specific list types. Each distinct ElementType
// creates a distinct, non-equal list type.
type typeMap struct {
	typeImplSigil
	ElementTypeT Type
}

// Map creates a map type with the given element Type.
//
// Map types are CollectionType implementations.
func Map(elem Type) Type {
	return Type{
		typeMap{
			ElementTypeT: elem,
		},
	}
}

// Equals returns true if the other Type is a map whose element type is
// equal to that of the receiver.
func (t typeMap) Equals(other Type) bool {
	ot, isMap := other.typeImpl.(typeMap)
	if !isMap {
		return false
	}

	return t.ElementTypeT.Equals(ot.ElementTypeT)
}

func (t typeMap) FriendlyName() string {
	return "map of " + t.ElementTypeT.FriendlyName()
}

func (t typeMap) ElementType() Type {
	return t.ElementTypeT
}

func (t typeMap) GoString() string {
	return fmt.Sprintf("cty.Map(%#v)", t.ElementTypeT)
}

// IsMapType returns true if the given type is a list type, regardless of its
// element type.
func (t Type) IsMapType() bool {
	_, ok := t.typeImpl.(typeMap)
	return ok
}

// MapElementType is a convenience method that checks if the given type is
// a map type, returning a pointer to its element type if so and nil
// otherwise. This is intended to allow convenient conditional branches,
// like so:
//
//     if et := t.MapElementType(); et != nil {
//         // Do something with *et
//     }
func (t Type) MapElementType() *Type {
	if lt, ok := t.typeImpl.(typeMap); ok {
		return &lt.ElementTypeT
	}
	return nil
}