-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplanner_finalize.go
More file actions
207 lines (179 loc) · 5.88 KB
/
Copy pathplanner_finalize.go
File metadata and controls
207 lines (179 loc) · 5.88 KB
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
206
207
package morph
import (
"fmt"
"github.com/seeruk/morph/plan"
"github.com/seeruk/morph/types"
)
type callableBan struct {
Path string
SourceKey string
TargetKey string
Callable plan.CallableRef
}
func callableBanFor(path string, source, target types.Type, callable plan.CallableRef) callableBan {
return callableBan{
Path: path,
SourceKey: types.TypeKey(source),
TargetKey: types.TypeKey(target),
Callable: callable,
}
}
type callableErrabilityFailure struct {
Ban callableBan
Value *plan.Value
Type *plan.Type
Diagnostic plan.Diagnostic
}
type finalPlanValidation struct {
ImportSites []importRequirementSite
VisibilitySites []visibilityRequirementSite
CallableFailures []callableErrabilityFailure
}
func (p *attemptPlanner) isCallableBanned(path string, source, target types.Type, callable plan.CallableRef) bool {
_, banned := p.callableBans[callableBanFor(path, source, target, callable)]
return banned
}
func (p *attemptPlanner) finalizePlanErrability(outputGroups []plan.OutputGroup) {
for {
var changed bool
walkOutputGroups(outputGroups, walkCallbacks{
ValuePost: func(ctx walkContext) {
if finalizeValueErrability(ctx.Value) {
changed = true
}
},
TypePost: func(ctx walkContext) {
if finalizeTypeErrability(ctx.Type) {
changed = true
}
},
})
if !changed {
return
}
}
}
func finalizeTypeErrability(typ *plan.Type) bool {
if typ == nil {
return false
}
var canError, changed bool
switch {
case typ.EnumPlan != nil:
// We can trust enums, because there's no opportunity for callables.
canError = typ.EnumPlan != nil && typ.CanError
case typ.StructPlan != nil:
// Structs can have callables and erroring accessors, so we need to check each property.
for i := range typ.StructPlan.Properties {
property := &typ.StructPlan.Properties[i]
memberCanError := property.Source.CanError || property.Target.CanError
canError = canError || memberCanError || property.Mapping.CanError
}
}
if typ.CanError != canError {
typ.CanError = canError
changed = true
}
return changed
}
func finalizeValueErrability(value *plan.Value) bool {
if value == nil {
return false
}
canError := canAdaptationsError(value.SourceAdaptations, value.Optionality) ||
canAdaptationsError(value.TargetAdaptations, value.Optionality)
switch value.Operation {
case plan.OperationFunction, plan.OperationMethod:
canError = canError || value.Callable != nil && value.Callable.ReturnsError
case plan.OperationStruct, plan.OperationEnum:
canError = canError || value.Plan != nil && value.Plan.CanError
case plan.OperationSlice, plan.OperationArray, plan.OperationMap, plan.OperationUnsupported:
canError = canError || canAnyValueChildError(value)
}
if value.CanError != canError {
value.CanError = canError
return true
}
return false
}
func canAnyValueChildError(value *plan.Value) bool {
return value.Elem != nil && value.Elem.CanError ||
value.Key != nil && value.Key.CanError ||
value.Value != nil && value.Value.CanError
}
func (p *attemptPlanner) validateFinalPlan(outputGroups []plan.OutputGroup) *callableBan {
validation := collectFinalPlanValidation(outputGroups)
for _, failure := range validation.CallableFailures {
if _, banned := p.callableBans[failure.Ban]; !banned {
ban := failure.Ban
return &ban
}
}
p.validateImportRequirements(validation.ImportSites)
p.validateVisibilityRequirements(validation.VisibilitySites)
for _, failure := range validation.CallableFailures {
failure.Value.Diagnostics = appendDiagnostic(failure.Value.Diagnostics, failure.Diagnostic)
if failure.Type != nil {
failure.Type.Diagnostics = appendDiagnostic(failure.Type.Diagnostics, failure.Diagnostic)
}
p.diagnostics = appendDiagnostic(p.diagnostics, failure.Diagnostic)
}
return nil
}
func collectFinalPlanValidation(outputGroups []plan.OutputGroup) finalPlanValidation {
var validation finalPlanValidation
walkOutputGroups(outputGroups, walkCallbacks{
TypePre: func(ctx walkContext) {
validation.ImportSites = append(validation.ImportSites, typeSignatureImportSites(ctx)...)
validation.VisibilitySites = append(validation.VisibilitySites, typeSignatureVisibilitySites(ctx)...)
},
ValuePre: func(ctx walkContext) {
validation.ImportSites = append(validation.ImportSites, valueImportSites(ctx)...)
validation.VisibilitySites = append(validation.VisibilitySites, valueVisibilitySites(ctx)...)
validation.CallableFailures = append(validation.CallableFailures, callableErrabilityFailures(ctx)...)
},
})
validation.ImportSites = dedupeImportRequirementSites(validation.ImportSites)
validation.VisibilitySites = dedupeVisibilityRequirementSites(validation.VisibilitySites)
return validation
}
func callableErrabilityFailures(ctx walkContext) []callableErrabilityFailure {
value := ctx.Value
if value == nil || value.Callable == nil {
return nil
}
var failures []callableErrabilityFailure
for i := range value.CallableMapperArgs {
arg := &value.CallableMapperArgs[i]
if !arg.Mapping.CanError || arg.ReturnsError {
continue
}
failures = append(failures, callableErrabilityFailure{
Ban: callableBanFor(
ctx.Path,
value.Source,
value.Target,
*value.Callable,
),
Value: value,
Type: ctx.Owner,
Diagnostic: callableErrabilityDiagnostic(callableMapperArgPath(ctx.Path, i), i, arg.Mapping),
})
}
return failures
}
func callableMapperArgPath(path string, index int) string {
return fmt.Sprintf("%s :: callable argument %d", path, index+1)
}
func callableErrabilityDiagnostic(path string, index int, mapping plan.Value) plan.Diagnostic {
return plan.Diagnostic{
Level: plan.DiagnosticLevelFatal,
Path: path,
Message: fmt.Sprintf(
"callable argument %d maps %s to %s and can return an error, but the higher-order callable argument does not return error",
index+1,
types.TypeKey(mapping.Source),
types.TypeKey(mapping.Target),
),
}
}