@@ -2,7 +2,6 @@ package provider
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"net/url"
8
7
"os"
@@ -16,6 +15,7 @@ import (
16
15
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
17
16
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18
17
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
18
+ "golang.org/x/xerrors"
19
19
)
20
20
21
21
type config struct {
@@ -509,17 +509,24 @@ func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err
509
509
// The cty package reports type mismatches by panicking
510
510
defer func () {
511
511
if r := recover (); r != nil {
512
- err = errors . New ( fmt . Sprintf ( "panic while handling coder_metadata: %#v" , r ) )
512
+ err = xerrors . Errorf ( "panic while handling coder_metadata: %#v" , r )
513
513
}
514
514
}()
515
515
516
516
rawPlan := resourceData .GetRawPlan ()
517
517
items := rawPlan .GetAttr ("item" ).AsValueSlice ()
518
518
519
519
var resultItems []interface {}
520
+ itemKeys := map [string ]struct {}{}
520
521
for _ , item := range items {
522
+ key := valueAsString (item .GetAttr ("key" ))
523
+ _ , exists := itemKeys [key ]
524
+ if exists {
525
+ return nil , xerrors .Errorf ("duplicate metadata key %q" , key )
526
+ }
527
+ itemKeys [key ] = struct {}{}
521
528
resultItem := map [string ]interface {}{
522
- "key" : valueAsString ( item . GetAttr ( " key" )) ,
529
+ "key" : key ,
523
530
"value" : valueAsString (item .GetAttr ("value" )),
524
531
"sensitive" : valueAsBool (item .GetAttr ("sensitive" )),
525
532
}
@@ -532,9 +539,10 @@ func populateIsNull(resourceData *schema.ResourceData) (result interface{}, err
532
539
return resultItems , nil
533
540
}
534
541
535
- // valueAsString takes a cty.Value that may be a string or null, and converts it to either a Go string
542
+ // valueAsString takes a cty.Value that may be a string or null, and converts it to a Go string,
543
+ // which will be empty if the input value was null.
536
544
// or a nil interface{}
537
- func valueAsString (value cty.Value ) interface {} {
545
+ func valueAsString (value cty.Value ) string {
538
546
if value .IsNull () {
539
547
return ""
540
548
}
0 commit comments