Skip to content

Commit c7e7b4e

Browse files
committed
remove singleDiag helper
1 parent 8342a18 commit c7e7b4e

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

provider/parameter.go

+53-44
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,14 @@ func parameterDataSource() *schema.Resource {
157157
// Validate options
158158
_, parameter.FormType, err = ValidateFormType(parameter.Type, len(parameter.Option), parameter.FormType)
159159
if err != nil {
160-
return singleDiag(diag.Diagnostic{
161-
Severity: diag.Error,
162-
Summary: "Invalid form_type for parameter",
163-
Detail: err.Error(),
164-
AttributePath: cty.Path{cty.GetAttrStep{Name: "form_type"}},
165-
})
160+
return diag.Diagnostics{
161+
{
162+
Severity: diag.Error,
163+
Summary: "Invalid form_type for parameter",
164+
Detail: err.Error(),
165+
AttributePath: cty.Path{cty.GetAttrStep{Name: "form_type"}},
166+
},
167+
}
166168
}
167169
// Set the form_type back in case the value was changed.
168170
// Eg via a default. If a user does not specify, a default value
@@ -417,12 +419,14 @@ func (v *Parameter) Valid() diag.Diagnostics {
417419
// should be used for the value type, and optionType for options.
418420
optionType, _, err := ValidateFormType(v.Type, len(v.Option), v.FormType)
419421
if err != nil {
420-
return singleDiag(diag.Diagnostic{
421-
Severity: diag.Error,
422-
Summary: "Invalid form_type for parameter",
423-
Detail: err.Error(),
424-
AttributePath: cty.Path{cty.GetAttrStep{Name: "form_type"}},
425-
})
422+
return diag.Diagnostics{
423+
{
424+
Severity: diag.Error,
425+
Summary: "Invalid form_type for parameter",
426+
Detail: err.Error(),
427+
AttributePath: cty.Path{cty.GetAttrStep{Name: "form_type"}},
428+
},
429+
}
426430
}
427431

428432
optionNames := map[string]interface{}{}
@@ -431,19 +435,22 @@ func (v *Parameter) Valid() diag.Diagnostics {
431435
for _, option := range v.Option {
432436
_, exists := optionNames[option.Name]
433437
if exists {
434-
return singleDiag(diag.Diagnostic{
438+
return diag.Diagnostics{{
435439
Severity: diag.Error,
436440
Summary: "Option names must be unique.",
437441
Detail: fmt.Sprintf("multiple options found with the same name %q", option.Name),
438-
})
442+
},
443+
}
439444
}
440445
_, exists = optionValues[option.Value]
441446
if exists {
442-
return singleDiag(diag.Diagnostic{
443-
Severity: diag.Error,
444-
Summary: "Option values must be unique.",
445-
Detail: fmt.Sprintf("multiple options found with the same value %q", option.Value),
446-
})
447+
return diag.Diagnostics{
448+
{
449+
Severity: diag.Error,
450+
Summary: "Option values must be unique.",
451+
Detail: fmt.Sprintf("multiple options found with the same value %q", option.Value),
452+
},
453+
}
447454
}
448455
diags := valueIsType(optionType, option.Value, cty.Path{})
449456
if diags.HasError() {
@@ -473,25 +480,29 @@ func (v *Parameter) Valid() diag.Diagnostics {
473480
}
474481

475482
if len(missing) > 0 {
476-
return singleDiag(diag.Diagnostic{
477-
Severity: diag.Error,
478-
Summary: "Default values must be a valid option",
479-
Detail: fmt.Sprintf(
480-
"default value %q is not a valid option, values %q are missing from the options",
481-
v.Default, strings.Join(missing, ", "),
482-
),
483-
AttributePath: cty.Path{cty.GetAttrStep{Name: "default"}},
484-
})
483+
return diag.Diagnostics{
484+
{
485+
Severity: diag.Error,
486+
Summary: "Default values must be a valid option",
487+
Detail: fmt.Sprintf(
488+
"default value %q is not a valid option, values %q are missing from the options",
489+
v.Default, strings.Join(missing, ", "),
490+
),
491+
AttributePath: cty.Path{cty.GetAttrStep{Name: "default"}},
492+
},
493+
}
485494
}
486495
} else {
487496
_, defaultIsValid := optionValues[v.Default]
488497
if !defaultIsValid {
489-
return singleDiag(diag.Diagnostic{
490-
Severity: diag.Error,
491-
Summary: "Default value must be a valid option",
492-
Detail: fmt.Sprintf("the value %q must be defined as one of options", v.Default),
493-
AttributePath: cty.Path{cty.GetAttrStep{Name: "default"}},
494-
})
498+
return diag.Diagnostics{
499+
{
500+
Severity: diag.Error,
501+
Summary: "Default value must be a valid option",
502+
Detail: fmt.Sprintf("the value %q must be defined as one of options", v.Default),
503+
AttributePath: cty.Path{cty.GetAttrStep{Name: "default"}},
504+
},
505+
}
495506
}
496507
}
497508
}
@@ -563,12 +574,14 @@ func valueIsListString(value string, path cty.Path) ([]string, diag.Diagnostics)
563574
var items []string
564575
err := json.Unmarshal([]byte(value), &items)
565576
if err != nil {
566-
return nil, singleDiag(diag.Diagnostic{
567-
Severity: diag.Error,
568-
Summary: fmt.Sprintf("When using list(string) type, value must be a json encoded list of strings"),
569-
Detail: fmt.Sprintf("value %q is not a valid list of strings", value),
570-
AttributePath: path,
571-
})
577+
return nil, diag.Diagnostics{
578+
{
579+
Severity: diag.Error,
580+
Summary: fmt.Sprintf("When using list(string) type, value must be a json encoded list of strings"),
581+
Detail: fmt.Sprintf("value %q is not a valid list of strings", value),
582+
AttributePath: path,
583+
},
584+
}
572585
}
573586
return items, nil
574587
}
@@ -600,7 +613,3 @@ func (v *Validation) errorRendered(value string) error {
600613
"{value}", value)
601614
return xerrors.Errorf(r.Replace(v.Error))
602615
}
603-
604-
func singleDiag(diagnostic diag.Diagnostic) diag.Diagnostics {
605-
return diag.Diagnostics{diagnostic}
606-
}

0 commit comments

Comments
 (0)