Skip to content

Commit 9618335

Browse files
committed
object_mapof: Prep for flatten needs
1 parent 031897a commit 9618335

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

internal/framework/types/object_mapof.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,50 @@ func (t ObjectMapTypeOf[T]) ValueFromMap(ctx context.Context, in basetypes.MapVa
5353
return NewObjectMapUnknownValueMapOf[T](ctx), diags
5454
}
5555

56-
listValue, d := basetypes.NewMapValue(NewObjectTypeOf[T](ctx), in.Elements())
56+
v, d := basetypes.NewMapValue(NewObjectTypeOf[T](ctx), in.Elements())
5757
diags.Append(d...)
5858
if diags.HasError() {
5959
return NewObjectMapUnknownValueMapOf[T](ctx), diags
6060
}
6161

6262
value := ObjectMapValueOf[T]{
63-
MapValue: listValue,
63+
MapValue: v,
6464
}
6565

6666
return value, diags
6767
}
6868

69+
func (t ObjectMapTypeOf[T]) ValueFromRawMap(ctx context.Context, in any) (basetypes.MapValuable, diag.Diagnostics) {
70+
var diags diag.Diagnostics
71+
72+
if _, ok := in.(map[string]T); !ok {
73+
diags.Append(diag.NewErrorDiagnostic("Invalid map value", fmt.Sprintf("incorrect type: want %T, got %T", (map[string]T)(nil), in)))
74+
return nil, diags
75+
}
76+
77+
return NewObjectMapValueMapOf(ctx, in.(map[string]T)), diags
78+
}
79+
6980
func (t ObjectMapTypeOf[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
7081
attrValue, err := t.MapType.ValueFromTerraform(ctx, in)
7182

7283
if err != nil {
7384
return nil, err
7485
}
7586

76-
listValue, ok := attrValue.(basetypes.MapValue)
87+
v, ok := attrValue.(basetypes.MapValue)
7788

7889
if !ok {
7990
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
8091
}
8192

82-
listValuable, diags := t.ValueFromMap(ctx, listValue)
93+
valuable, diags := t.ValueFromMap(ctx, v)
8394

8495
if diags.HasError() {
8596
return nil, fmt.Errorf("unexpected error converting MapValue to MapValuable: %v", diags)
8697
}
8798

88-
return listValuable, nil
99+
return valuable, nil
89100
}
90101

91102
func (t ObjectMapTypeOf[T]) ValueType(ctx context.Context) attr.Value {
@@ -98,6 +109,16 @@ func (t ObjectMapTypeOf[T]) NullValue(ctx context.Context) (attr.Value, diag.Dia
98109
return NewObjectMapNullValueMapOf[T](ctx), diags
99110
}
100111

112+
func (t ObjectMapTypeOf[T]) New(ctx context.Context) (any, diag.Diagnostics) {
113+
return newMap[T](ctx)
114+
}
115+
116+
func newMap[T any](_ context.Context) (map[string]T, diag.Diagnostics) {
117+
var diags diag.Diagnostics
118+
119+
return make(map[string]T), diags
120+
}
121+
101122
// ObjectMapValueOf represents a Terraform Plugin Framework Map value whose elements are of type ObjectTypeOf.
102123
type ObjectMapValueOf[T any] struct {
103124
basetypes.MapValue

0 commit comments

Comments
 (0)