Skip to content

Commit

Permalink
Merge pull request #41762 from hashicorp/td-autoflex-remove-reflect-m…
Browse files Browse the repository at this point in the history
…ethod

autoflex: Handle all `StringValuable` values consistently
  • Loading branch information
gdavison authored Mar 10, 2025
2 parents cc02ae5 + 056965d commit 1b5de4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
19 changes: 7 additions & 12 deletions internal/framework/flex/auto_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,18 +1145,13 @@ func mapBlockKey(ctx context.Context, from any) (reflect.Value, diag.Diagnostics
if field.Name == mapBlockKeyFieldName {
fieldVal := valFrom.Field(i)

if v, ok := fieldVal.Interface().(basetypes.StringValue); ok {
return reflect.ValueOf(v.ValueString()), diags
}

// this handles things like StringEnum which has a ValueString method but is tricky to get a generic instantiation of
fieldType := fieldVal.Type()
method, found := fieldType.MethodByName("ValueString")
if found {
result := fieldType.Method(method.Index).Func.Call([]reflect.Value{fieldVal})
if len(result) > 0 {
return result[0], diags
if v, ok := fieldVal.Interface().(basetypes.StringValuable); ok {
v, d := v.ToStringValue(ctx)
diags.Append(d...)
if d.HasError() {
return reflect.Zero(reflect.TypeFor[string]()), diags
}
return reflect.ValueOf(v.ValueString()), diags
}

// this is not ideal but perhaps better than a panic?
Expand All @@ -1169,7 +1164,7 @@ func mapBlockKey(ctx context.Context, from any) (reflect.Value, diag.Diagnostics
tflog.SubsystemError(ctx, subsystemName, "Source has no map block key")
diags.Append(diagExpandingNoMapBlockKey(valFrom.Type()))

return reflect.Zero(reflect.TypeOf("")), diags
return reflect.Zero(reflect.TypeFor[string]()), diags
}

func expandExpander(ctx context.Context, fromExpander Expander, toVal reflect.Value) diag.Diagnostics {
Expand Down
22 changes: 13 additions & 9 deletions internal/framework/flex/auto_flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,19 +1513,23 @@ func setMapBlockKey(ctx context.Context, to any, key reflect.Value) diag.Diagnos
continue
}

if _, ok := valTo.Field(i).Interface().(basetypes.StringValue); ok {
valTo.Field(i).Set(reflect.ValueOf(basetypes.NewStringValue(key.String())))
fieldVal := valTo.Field(i) // vTo
fieldAttrVal, ok := fieldVal.Interface().(attr.Value) // valTo
if !ok {
tflog.SubsystemError(ctx, subsystemName, "Target does not implement attr.Value")
diags.Append(diagFlatteningTargetDoesNotImplementAttrValue(reflect.TypeOf(fieldVal.Interface())))
return diags
}
tTo := fieldAttrVal.Type(ctx)

fieldType := valTo.Field(i).Type()

method, found := fieldType.MethodByName("StringEnumValue")
if found {
result := fieldType.Method(method.Index).Func.Call([]reflect.Value{valTo.Field(i), key})
if len(result) > 0 {
valTo.Field(i).Set(result[0])
switch tTo := tTo.(type) {
case basetypes.StringTypable:
v, d := tTo.ValueFromString(ctx, types.StringValue(key.String()))
diags.Append(d...)
if diags.HasError() {
return diags
}
fieldVal.Set(reflect.ValueOf(v))
}

return diags
Expand Down

0 comments on commit 1b5de4c

Please sign in to comment.