-
Notifications
You must be signed in to change notification settings - Fork 132
/
convert.go
43 lines (39 loc) · 1.37 KB
/
convert.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
package fastly
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)
func uintOrDefault(int *uint) uint {
if int == nil {
return 0
}
return *int
}
// diagToErr takes a diag.Diagnostics and finds the first Error (ignoring Warnings).
// This is useful for some of the SDK functions which are context aware but still return Go errors, e.g. StateContext
// and resource.RetryContext.
func diagToErr(diagnostics diag.Diagnostics) error {
if diagnostics.HasError() {
// diagnostics could have multiple Warnings as well as an Error
for _, diagnostic := range diagnostics {
if diagnostic.Severity == diag.Error {
return fmt.Errorf("%s", diagnostic.Summary)
}
}
}
return nil
}
// diagToWarnsAndErrs takes a diag.Diagnostics and produces two slices of warnings and errors.
// This is to enable emulation of deprecated SchemaValidateFunc behaviour in the unit tests for SchemaValidateDiagFuncs.
func diagToWarnsAndErrs(diagnostics diag.Diagnostics) (warnings []string, errors []string) {
for _, diagnostic := range diagnostics {
if diagnostic.Severity == diag.Warning {
warnings = append(warnings, diagnostic.Summary)
} else if diagnostic.Severity == diag.Error {
errors = append(errors, diagnostic.Summary)
} else {
errors = append(errors, fmt.Sprintf("%s (unknown diagnostic severity: %d)", diagnostic.Summary, diagnostic.Severity))
}
}
return
}