/
type_opaque.go
205 lines (179 loc) 路 6.3 KB
/
type_opaque.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model/pretty"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
// OpaqueType represents a type that is named by a string.
type OpaqueType string
// SyntaxNode returns the syntax node for the type. This is always syntax.None.
func (*OpaqueType) SyntaxNode() hclsyntax.Node {
return syntax.None
}
// Traverse attempts to traverse the opaque type with the given traverser. The result type of traverse(opaque(name))
// is dynamic if name is "dynamic"; otherwise the traversal fails.
func (t *OpaqueType) Traverse(traverser hcl.Traverser) (Traversable, hcl.Diagnostics) {
if t == DynamicType {
return DynamicType, nil
}
return DynamicType, hcl.Diagnostics{unsupportedReceiverType(t, traverser.SourceRange())}
}
// Equals returns true if this type has the same identity as the given type.
func (t *OpaqueType) Equals(other Type) bool {
return t.equals(other, nil)
}
func (t *OpaqueType) equals(other Type, seen map[Type]struct{}) bool {
if o, ok := other.(*OpaqueType); ok {
return *o == *t
}
return t == other
}
// AssignableFrom returns true if this type is assignable from the indicated source type. A token(name) is assignable
// from token(name).
func (t *OpaqueType) AssignableFrom(src Type) bool {
return assignableFrom(t, src, func() bool {
return false
})
}
func (t *OpaqueType) conversionFromImpl(
src Type, unifying, checkUnsafe bool, seen map[Type]struct{},
) (ConversionKind, lazyDiagnostics) {
return conversionFrom(t, src, unifying, seen, func() (ConversionKind, lazyDiagnostics) {
if constType, ok := src.(*ConstType); ok {
return t.conversionFrom(constType.Type, unifying, seen)
}
switch {
case t == NumberType:
// src == NumberType is handled by t == src above
contract.Assertf(src != NumberType, "unexpected number-to-number conversion")
cki, _ := IntType.conversionFromImpl(src, unifying, false, seen)
switch cki {
case SafeConversion:
return SafeConversion, nil
case UnsafeConversion:
return UnsafeConversion, nil
case NoConversion:
if checkUnsafe {
if kind, _ := StringType.conversionFromImpl(src, unifying, false, seen); kind.Exists() {
return UnsafeConversion, nil
}
}
}
return NoConversion, nil
case t == IntType:
if checkUnsafe {
if kind, _ := NumberType.conversionFromImpl(src, unifying, true, seen); kind.Exists() {
return UnsafeConversion, nil
}
}
return NoConversion, nil
case t == BoolType:
if checkUnsafe {
if kind, _ := StringType.conversionFromImpl(src, unifying, false, seen); kind.Exists() {
return UnsafeConversion, nil
}
}
return NoConversion, nil
case t == StringType:
ckb, _ := BoolType.conversionFromImpl(src, unifying, false, seen)
ckn, _ := NumberType.conversionFromImpl(src, unifying, false, seen)
if ckb == SafeConversion || ckn == SafeConversion {
return SafeConversion, nil
}
if ckb == UnsafeConversion || ckn == UnsafeConversion {
return UnsafeConversion, nil
}
return NoConversion, nil
default:
return NoConversion, nil
}
})
}
func (t *OpaqueType) conversionFrom(src Type, unifying bool, seen map[Type]struct{}) (ConversionKind, lazyDiagnostics) {
return t.conversionFromImpl(src, unifying, true, seen)
}
// ConversionFrom returns the kind of conversion (if any) that is possible from the source type to this type.
//
// In general, an opaque type is only convertible from itself (in addition to the standard dynamic and union
// conversions). However, there are special rules for the builtin types:
//
// - The dynamic type is safely convertible from any other type, and is unsafely convertible _to_ any other type
// - The string type is safely convertible from bool, number, and int
// - The number type is safely convertible from int and unsafely convertible from string
// - The int type is unsafely convertible from string
// - The bool type is unsafely convertible from string
func (t *OpaqueType) ConversionFrom(src Type) ConversionKind {
kind, _ := t.conversionFrom(src, false, nil)
return kind
}
func (t *OpaqueType) String() string {
switch t {
case NumberType:
return "number"
case IntType:
return "int"
case BoolType:
return "bool"
case StringType:
return "string"
default:
if hclsyntax.ValidIdentifier(string(*t)) {
return string(*t)
}
return fmt.Sprintf("type(%s)", string(*t))
}
}
func NewOpaqueType(name string) *OpaqueType {
t := OpaqueType(name)
return &t
}
func (t *OpaqueType) pretty(seenFormatters map[Type]pretty.Formatter) pretty.Formatter {
return pretty.FromStringer(t)
}
func (t *OpaqueType) Pretty() pretty.Formatter {
seenFormatters := map[Type]pretty.Formatter{}
return t.pretty(seenFormatters)
}
func (t *OpaqueType) string(_ map[Type]struct{}) string {
return t.String()
}
var opaquePrecedence = []Type{StringType, NumberType, IntType, BoolType}
func (t *OpaqueType) unify(other Type) (Type, ConversionKind) {
return unify(t, other, func() (Type, ConversionKind) {
if t == DynamicType || other == DynamicType {
// These should have been handled by unify.
contract.Failf("unexpected type %v in OpaqueType.unify", t)
return DynamicType, SafeConversion
}
for _, goal := range opaquePrecedence {
if t == goal {
kind, _ := goal.conversionFrom(other, true, nil)
return goal, kind
}
if other == goal {
kind, _ := goal.conversionFrom(t, true, nil)
return goal, kind
}
}
// There should be a total order on conversions to and from these types, so there should be a total order
// on unifications with these types.
return DynamicType, SafeConversion
})
}
func (*OpaqueType) isType() {}