Debian Codegraph @

cty/convert/mismatch_msg.go

raw symbols SCIP
1 package convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/"
5 "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/"
6 "sortscip-go gomod github.com/golang/go/src go1.18 sort/"
7
8 "github.com/zclconf/go-cty/ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/"
9 )
10
11 // MismatchMessage is a helper to return an English-language description of
12 // the differences between got and want, phrased as a reason why got does
13 // not conform to want.
14 //
15 // This function does not itself attempt conversion, and so it should generally
16 // be used only after a conversion has failed, to report the conversion failure
17 // to an English-speaking user. The result will be confusing got is actually
18 // conforming to or convertable to want.
19 //
20 // The shorthand helper function Convert uses this function internally to
21 // produce its error messages, so callers of that function do not need to
22 // also use MismatchMessage.
23 //
24 // This function is similar to Type.TestConformance, but it is tailored to
25 // describing conversion failures and so the messages it generates relate
26 // specifically to the conversion rules implemented in this package.
27 func MismatchMessagescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/MismatchMessage().(got, want ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) string {
28 switch {
29
30 case got.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().() && want.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().():
31 // If both types are object types then we may be able to say something
32 // about their respective attributes.
33 return mismatchMessageObjectsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/mismatchMessageObjects().(got, want)
34
35 case got.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() && want.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.:
36 // If conversion from tuple to list failed then it's because we couldn't
37 // find a common type to convert all of the tuple elements to.
38 return "all list elements must have the same type"
39
40 case got.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() && want.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() && want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.:
41 // If conversion from tuple to set failed then it's because we couldn't
42 // find a common type to convert all of the tuple elements to.
43 return "all set elements must have the same type"
44
45 case got.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().() && want.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() && want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().() == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.:
46 // If conversion from object to map failed then it's because we couldn't
47 // find a common type to convert all of the object attributes to.
48 return "all map elements must have the same type"
49
50 case (got.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() || got.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().()) && want.IsCollectionTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCollectionType().():
51 return mismatchMessageCollectionsFromStructuralscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/mismatchMessageCollectionsFromStructural().(got, want)
52
53 case got.IsCollectionTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCollectionType().() && want.IsCollectionTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCollectionType().():
54 return mismatchMessageCollectionsFromCollectionsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/mismatchMessageCollectionsFromCollections().(got, want)
55
56 default:
57 // If we have nothing better to say, we'll just state what was required.
58 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
59 }
60 }
61
62 func mismatchMessageObjectsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/mismatchMessageObjects().(got, want ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) string {
63 // Per our conversion rules, "got" is allowed to be a superset of "want",
64 // and so we'll produce error messages here under that assumption.
65 gotAtys := got.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().()
66 wantAtys := want.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().()
67
68 // If we find missing attributes then we'll report those in preference,
69 // but if not then we will report a maximum of one non-conforming
70 // attribute, just to keep our messages relatively terse.
71 // We'll also prefer to report a recursive type error from an _unsafe_
72 // conversion over a safe one, because these are subjectively more
73 // "serious".
74 var missingAttrs []string
75 var unsafeMismatchAttr string
76 var safeMismatchAttr string
77
78 for name, wantAty := range wantAtys {
79 gotAty, exists := gotAtys[name]
80 if !exists {
81 if !want.AttributeOptionalscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeOptional().(name) {
82 missingAttrs = append(missingAttrs, name)
83 }
84 continue
85 }
86
87 if gotAty.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(wantAty) {
88 continue // exact match, so no problem
89 }
90
91 // We'll now try to convert these attributes in isolation and
92 // see if we have a nested conversion error to report.
93 // We'll try an unsafe conversion first, and then fall back on
94 // safe if unsafe is possible.
95
96 // If we already have an unsafe mismatch attr error then we won't bother
97 // hunting for another one.
98 if unsafeMismatchAttr != "" {
99 continue
100 }
101 if conv := GetConversionUnsafescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/GetConversionUnsafe().(gotAty, wantAty); conv == nil {
102 unsafeMismatchAttr = fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("attribute %q: %s", name, MismatchMessagescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/MismatchMessage().(gotAty, wantAty))
103 }
104
105 // If we already have a safe mismatch attr error then we won't bother
106 // hunting for another one.
107 if safeMismatchAttr != "" {
108 continue
109 }
110 if conv := GetConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/GetConversion().(gotAty, wantAty); conv == nil {
111 safeMismatchAttr = fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("attribute %q: %s", name, MismatchMessagescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/MismatchMessage().(gotAty, wantAty))
112 }
113 }
114
115 // We should now have collected at least one problem. If we have more than
116 // one then we'll use our preference order to decide what is most important
117 // to report.
118 switch {
119
120 case len(missingAttrs) != 0:
121 sortscip-go gomod github.com/golang/go/src go1.18 sort/.Stringsscip-go gomod github.com/golang/go/src go1.18 sort/Strings().(missingAttrs)
122 switch len(missingAttrs) {
123 case 1:
124 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("attribute %q is required", missingAttrs[0])
125 case 2:
126 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("attributes %q and %q are required", missingAttrs[0], missingAttrs[1])
127 default:
128 sortscip-go gomod github.com/golang/go/src go1.18 sort/.Stringsscip-go gomod github.com/golang/go/src go1.18 sort/Strings().(missingAttrs)
129 var buf bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#
130 for _, name := range missingAttrs[:len(missingAttrs)-1] {
131 fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Fprintf().(&buf, "%q, ", name)
132 }
133 fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Fprintf().(&buf, "and %q", missingAttrs[len(missingAttrs)-1])
134 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("attributes %s are required", buf.Bytesscip-go gomod github.com/golang/go/src go1.18 bytes/Buffer#Bytes().())
135 }
136
137 case unsafeMismatchAttr != "":
138 return unsafeMismatchAttr
139
140 case safeMismatchAttr != "":
141 return safeMismatchAttr
142
143 default:
144 // We should never get here, but if we do then we'll return
145 // just a generic message.
146 return "incorrect object attributes"
147 }
148 }
149
150 func mismatchMessageCollectionsFromStructuralscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/mismatchMessageCollectionsFromStructural().(got, want ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) string {
151 // First some straightforward cases where the kind is just altogether wrong.
152 switch {
153 case want.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && !got.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().():
154 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
155 case want.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() && !got.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().():
156 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
157 case want.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() && !got.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().():
158 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
159 }
160
161 // If the kinds are matched well enough then we'll move on to checking
162 // individual elements.
163 wantEty := want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().()
164 switch {
165 case got.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().():
166 for i, gotEty := range got.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().() {
167 if gotEty.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(wantEty) {
168 continue // exact match, so no problem
169 }
170 if conv := getConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversion().(gotEty, wantEty, true); conv != nil {
171 continue // conversion is available, so no problem
172 }
173 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("element %d: %s", i, MismatchMessagescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/MismatchMessage().(gotEty, wantEty))
174 }
175
176 // If we get down here then something weird is going on but we'll
177 // return a reasonable fallback message anyway.
178 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("all elements must be %s", wantEty.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().())
179
180 case got.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().():
181 for name, gotAty := range got.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().() {
182 if gotAty.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(wantEty) {
183 continue // exact match, so no problem
184 }
185 if conv := getConversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/getConversion().(gotAty, wantEty, true); conv != nil {
186 continue // conversion is available, so no problem
187 }
188 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("element %q: %s", name, MismatchMessagescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/MismatchMessage().(gotAty, wantEty))
189 }
190
191 // If we get down here then something weird is going on but we'll
192 // return a reasonable fallback message anyway.
193 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("all elements must be %s", wantEty.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().())
194
195 default:
196 // Should not be possible to get here since we only call this function
197 // with got as structural types, but...
198 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
199 }
200 }
201
202 func mismatchMessageCollectionsFromCollectionsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/mismatchMessageCollectionsFromCollections().(got, want ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) string {
203 // First some straightforward cases where the kind is just altogether wrong.
204 switch {
205 case want.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && !(got.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() || got.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().()):
206 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
207 case want.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() && !(got.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() || got.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().()):
208 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
209 case want.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() && !got.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().():
210 return want.FriendlyNameForConstraintscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyNameForConstraint().() + " required"
211 }
212
213 // If the kinds are matched well enough then we'll check the element types.
214 gotEty := got.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().()
215 wantEty := want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().()
216 noun := "element type"
217 switch {
218 case want.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().():
219 noun = "list element type"
220 case want.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().():
221 noun = "set element type"
222 case want.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().():
223 noun = "map element type"
224 }
225 return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("incorrect %s: %s", noun, MismatchMessagescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/MismatchMessage().(gotEty, wantEty))
226 }