Skip to content

Commit 210a03f

Browse files
committed
Use an enum instead of string and update tests
1 parent 63e863b commit 210a03f

File tree

2 files changed

+235
-233
lines changed

2 files changed

+235
-233
lines changed

helper/schema/schema.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,13 @@ type Schema struct {
199199
RequiredWith []string
200200

201201
// ConditionsMode specifies the behavior of the SDK when one of the
202-
// above conditions are met. (ConflictsWith, ExactlyOneOf, etc).
203-
// When set to WARNING, a warning message is displayed to the user.
204-
// When set to ERROR (default), a fatal error is returned.
205-
// Optionally, a message can be returned with the warning or error.
206-
ConditionsMode string
202+
// above conditions are met (ConflictsWith, ExactlyOneOf, etc).
203+
// The default value is SchemaConditionsModeError, which returns an error.
204+
// Set ConditionsMode to SchemaConditionsModeWarning to return a warning instead.
205+
//
206+
// ConditionsModeMessage can optionally be used to display a message
207+
// to the user when one of the above conditions are met.
208+
ConditionsMode SchemaConditionsMode
207209
ConditionsMessage string
208210

209211
// When Deprecated is set, this attribute is deprecated.
@@ -263,6 +265,13 @@ const (
263265
SchemaConfigModeBlock
264266
)
265267

268+
type SchemaConditionsMode uint8
269+
270+
const (
271+
SchemaConditionsModeError SchemaConditionsMode = 0
272+
SchemaConditionsModeWarning SchemaConditionsMode = 1
273+
)
274+
266275
// SchemaDiffSuppressFunc is a function which can be used to determine
267276
// whether a detected diff on a schema element is "valid" or not, and
268277
// suppress it from the plan if necessary.
@@ -1457,17 +1466,10 @@ func (m schemaMap) validate(
14571466
ok = raw != nil
14581467
}
14591468

1460-
var conditionsMode diag.Severity
1461-
if schema.ConditionsMode == strings.ToLower("warning") {
1462-
conditionsMode = diag.Warning
1463-
} else {
1464-
conditionsMode = diag.Error
1465-
}
1466-
14671469
err := validateExactlyOneAttribute(k, schema, c)
14681470
if err != nil {
14691471
return append(diags, diag.Diagnostic{
1470-
Severity: conditionsMode,
1472+
Severity: diag.Severity(schema.ConditionsMode),
14711473
Summary: "ExactlyOne",
14721474
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
14731475
AttributePath: path,
@@ -1477,7 +1479,7 @@ func (m schemaMap) validate(
14771479
err = validateAtLeastOneAttribute(k, schema, c)
14781480
if err != nil {
14791481
return append(diags, diag.Diagnostic{
1480-
Severity: conditionsMode,
1482+
Severity: diag.Severity(schema.ConditionsMode),
14811483
Summary: "AtLeastOne",
14821484
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
14831485
AttributePath: path,
@@ -1509,7 +1511,7 @@ func (m schemaMap) validate(
15091511
err = validateRequiredWithAttribute(k, schema, c)
15101512
if err != nil {
15111513
return append(diags, diag.Diagnostic{
1512-
Severity: conditionsMode,
1514+
Severity: diag.Severity(schema.ConditionsMode),
15131515
Summary: "RequiredWith",
15141516
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
15151517
AttributePath: path,
@@ -1536,7 +1538,7 @@ func (m schemaMap) validate(
15361538
err = validateConflictingAttributes(k, schema, c)
15371539
if err != nil {
15381540
return append(diags, diag.Diagnostic{
1539-
Severity: conditionsMode,
1541+
Severity: diag.Severity(schema.ConditionsMode),
15401542
Summary: "ConflictsWith",
15411543
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
15421544
AttributePath: path,

0 commit comments

Comments
 (0)