Skip to content

Commit 9cdf77c

Browse files
authored
refactor CopyModel to return only de model and panic if there is any structural issue (#2905)
1 parent dcc8d35 commit 9cdf77c

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

internal/common/conversion/model_generation.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import (
66
)
77

88
// CopyModel creates a new struct with the same values as the source struct. Fields in destination struct that are not in source are left with zero value.
9-
func CopyModel[T any](src any) (*T, error) {
9+
// It panics if there are some structural problems so it should only happen during development.
10+
func CopyModel[T any](src any) *T {
1011
dest := new(T)
1112
valSrc := reflect.ValueOf(src)
1213
valDest := reflect.ValueOf(dest)
1314
if valSrc.Kind() != reflect.Ptr || valDest.Kind() != reflect.Ptr {
14-
return nil, fmt.Errorf("params must be pointers")
15+
panic("params must be pointers")
1516
}
1617
valSrc = valSrc.Elem()
1718
valDest = valDest.Elem()
1819
if valSrc.Kind() != reflect.Struct || valDest.Kind() != reflect.Struct {
19-
return nil, fmt.Errorf("params must be pointers to structs")
20+
panic("params must be pointers to structs")
2021
}
2122
typeSrc := valSrc.Type()
2223
typeDest := valDest.Type()
@@ -29,13 +30,13 @@ func CopyModel[T any](src any) (*T, error) {
2930
continue
3031
}
3132
if fieldDest.Type != fieldSrc.Type {
32-
return nil, fmt.Errorf("field has different type: %s", name)
33+
panic(fmt.Sprintf("field has different type: %s", name))
3334
}
3435
}
3536
if !valDest.Field(i).CanSet() {
36-
return nil, fmt.Errorf("field can't be set, probably unexported: %s", name)
37+
panic(fmt.Sprintf("field can't be set, probably unexported: %s", name))
3738
}
3839
valDest.Field(i).Set(valSrc.FieldByName(name))
3940
}
40-
return dest, nil
41+
return dest
4142
}

internal/common/conversion/model_generation_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
77
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/require"
98
)
109

1110
func TestCopyModel(t *testing.T) {
@@ -18,7 +17,7 @@ func TestCopyModel(t *testing.T) {
1817
testCases := map[string]struct {
1918
input any
2019
expected any
21-
expectedErrorStr string
20+
expectedPanicStr string
2221
}{
2322
"basic": {
2423
input: &struct {
@@ -65,27 +64,26 @@ func TestCopyModel(t *testing.T) {
6564
}{
6665
AttrStr: true,
6766
},
68-
expectedErrorStr: "field has different type: AttrStr",
67+
expectedPanicStr: "field has different type: AttrStr",
6968
},
7069
"unexported": {
7170
input: &struct {
7271
attrUnexported string
7372
}{
7473
attrUnexported: "val",
7574
},
76-
expectedErrorStr: "field can't be set, probably unexported: attrUnexported",
75+
expectedPanicStr: "field can't be set, probably unexported: attrUnexported",
7776
},
7877
}
7978
for name, tc := range testCases {
8079
t.Run(name, func(t *testing.T) {
81-
dest, err := conversion.CopyModel[destType](tc.input)
82-
if err == nil {
83-
assert.Equal(t, tc.expected, dest)
84-
assert.Equal(t, "", tc.expectedErrorStr)
80+
if tc.expectedPanicStr == "" {
81+
assert.Equal(t, tc.expected, conversion.CopyModel[destType](tc.input))
8582
} else {
86-
require.ErrorContains(t, err, tc.expectedErrorStr)
87-
assert.Nil(t, dest)
8883
assert.Nil(t, tc.expected)
84+
assert.PanicsWithValue(t, tc.expectedPanicStr, func() {
85+
conversion.CopyModel[destType](tc.input)
86+
})
8987
}
9088
})
9189
}

internal/service/advancedclustertpf/data_source.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ func (d *ds) readCluster(ctx context.Context, diags *diag.Diagnostics, modelDS *
7272
if diags.HasError() {
7373
return nil
7474
}
75-
modelOutDS, err := conversion.CopyModel[TFModelDS](modelOut)
76-
if err != nil {
77-
diags.AddError(errorRead, fmt.Sprintf("error setting model: %s", err.Error()))
78-
return nil
79-
}
75+
modelOutDS := conversion.CopyModel[TFModelDS](modelOut)
8076
modelOutDS.UseReplicationSpecPerShard = modelDS.UseReplicationSpecPerShard // attrs not in resource model
8177
return modelOutDS
8278
}

internal/service/advancedclustertpf/plural_data_source.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ func (d *pluralDS) readClusters(ctx context.Context, diags *diag.Diagnostics, pl
8484
if diags.HasError() {
8585
return nil
8686
}
87-
modelOutDS, err := conversion.CopyModel[TFModelDS](modelOut)
88-
if err != nil {
89-
diags.AddError(errorList, fmt.Sprintf("error setting model: %s", err.Error()))
90-
return nil
91-
}
87+
modelOutDS := conversion.CopyModel[TFModelDS](modelOut)
9288
modelOutDS.UseReplicationSpecPerShard = pluralModel.UseReplicationSpecPerShard // attrs not in resource model
9389
outs.Results = append(outs.Results, modelOutDS)
9490
}

0 commit comments

Comments
 (0)