Debian Codegraph @

cty/type_conform.go

raw symbols SCIP
1 package ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/
2
3 // TestConformance recursively walks the receiver and the given other type and
4 // returns nil if the receiver *conforms* to the given type.
5 //
6 // Type conformance is similar to type equality but has one crucial difference:
7 // PseudoTypeDynamic can be used within the given type to represent that
8 // *any* type is allowed.
9 //
10 // If any non-conformities are found, the returned slice will be non-nil and
11 // contain at least one error value. It will be nil if the type is entirely
12 // conformant.
13 //
14 // Note that the special behavior of PseudoTypeDynamic is the *only* exception
15 // to normal type equality. Calling applications may wish to apply their own
16 // automatic conversion logic to the given data structure to create a more
17 // liberal notion of conformance to a type.
18 //
19 // Returned errors are usually (but not always) PathError instances that
20 // indicate where in the structure the error was found. If a returned error
21 // is of that type then the error message is written for (English-speaking)
22 // end-users working within the cty type system, not mentioning any Go-oriented
23 // implementation details.
24 func (t Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) TestConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TestConformance().(other Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) []error {
25 path := make(Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#, 0)
26 var errs []error
27 testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(t, other, path, &errs)
28 return errs
29 }
30
31 func testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(given Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, want Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#, path Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#, errs *[]error) {
32 if want.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.) {
33 // anything goes!
34 return
35 }
36
37 if given.Equalsscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#Equals().(want) {
38 // Any equal types are always conformant
39 return
40 }
41
42 // The remainder of this function is concerned with detecting
43 // and reporting the specific non-conformance, since we wouldn't
44 // have got here if the types were not divergent.
45 // We treat compound structures as special so that we can report
46 // specifically what is non-conforming, rather than simply returning
47 // the entire type names and letting the user puzzle it out.
48
49 if given.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().() {
50 givenAttrs := given.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().()
51 wantAttrs := want.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().()
52
53 for k := range givenAttrs {
54 if _, exists := wantAttrs[k]; !exists {
55 *errs = append(
56 *errs,
57 errorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/errorf().(path, "unsupported attribute %q", k),
58 )
59 }
60 }
61 for k := range wantAttrs {
62 if _, exists := givenAttrs[k]; !exists {
63 *errs = append(
64 *errs,
65 errorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/errorf().(path, "missing required attribute %q", k),
66 )
67 }
68 }
69
70 path = append(path, nil)
71 pathIdx := len(path) - 1
72
73 for k, wantAttrType := range wantAttrs {
74 if givenAttrType, exists := givenAttrs[k]; exists {
75 path[pathIdx] = GetAttrStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/GetAttrStep#{Namescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/GetAttrStep#Name.: k}
76 testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(givenAttrType, wantAttrType, path, errs)
77 }
78 }
79
80 path = path[0:pathIdx]
81
82 return
83 }
84
85 if given.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() && want.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() {
86 givenElems := given.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().()
87 wantElems := want.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().()
88
89 if len(givenElems) != len(wantElems) {
90 *errs = append(
91 *errs,
92 errorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/errorf().(path, "%d elements are required, but got %d", len(wantElems), len(givenElems)),
93 )
94 return
95 }
96
97 path = append(path, nil)
98 pathIdx := len(path) - 1
99
100 for i, wantElemType := range wantElems {
101 givenElemType := givenElems[i]
102 path[pathIdx] = IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: NumberIntValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NumberIntVal().(int64(i))}
103 testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(givenElemType, wantElemType, path, errs)
104 }
105
106 path = path[0:pathIdx]
107
108 return
109 }
110
111 if given.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() && want.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() {
112 path = append(path, IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: UnknownValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownVal().(Numberscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Number.)})
113 pathIdx := len(path) - 1
114 testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(given.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), path, errs)
115 path = path[0:pathIdx]
116 return
117 }
118
119 if given.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() && want.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() {
120 path = append(path, IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: UnknownValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownVal().(Stringscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/String.)})
121 pathIdx := len(path) - 1
122 testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(given.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), path, errs)
123 path = path[0:pathIdx]
124 return
125 }
126
127 if given.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() && want.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() {
128 path = append(path, IndexStepscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#{Keyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/IndexStep#Key.: UnknownValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/UnknownVal().(given.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())})
129 pathIdx := len(path) - 1
130 testConformancescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/testConformance().(given.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), want.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), path, errs)
131 path = path[0:pathIdx]
132 return
133 }
134
135 *errs = append(
136 *errs,
137 errorfscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/errorf().(path, "%s required, but received %s", want.FriendlyNamescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyName().(), given.FriendlyNamescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyName().()),
138 )
139 }