-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into CLOUDP-287995_fail_index_key_too_long_work…
…around
- Loading branch information
Showing
17 changed files
with
436 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package conversion | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// 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. | ||
func CopyModel[T any](src any) (*T, error) { | ||
dest := new(T) | ||
valSrc := reflect.ValueOf(src) | ||
valDest := reflect.ValueOf(dest) | ||
if valSrc.Kind() != reflect.Ptr || valDest.Kind() != reflect.Ptr { | ||
return nil, fmt.Errorf("params must be pointers") | ||
} | ||
valSrc = valSrc.Elem() | ||
valDest = valDest.Elem() | ||
if valSrc.Kind() != reflect.Struct || valDest.Kind() != reflect.Struct { | ||
return nil, fmt.Errorf("params must be pointers to structs") | ||
} | ||
typeSrc := valSrc.Type() | ||
typeDest := valDest.Type() | ||
for i := 0; i < typeDest.NumField(); i++ { | ||
fieldDest := typeDest.Field(i) | ||
name := fieldDest.Name | ||
{ | ||
fieldSrc, found := typeSrc.FieldByName(name) | ||
if !found { | ||
continue | ||
} | ||
if fieldDest.Type != fieldSrc.Type { | ||
return nil, fmt.Errorf("field has different type: %s", name) | ||
} | ||
} | ||
if !valDest.Field(i).CanSet() { | ||
return nil, fmt.Errorf("field can't be set, probably unexported: %s", name) | ||
} | ||
valDest.Field(i).Set(valSrc.FieldByName(name)) | ||
} | ||
return dest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package conversion_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCopyModel(t *testing.T) { | ||
type destType struct { | ||
AttrStr string | ||
attrUnexported string | ||
AttrInt int | ||
} | ||
|
||
testCases := map[string]struct { | ||
input any | ||
expected any | ||
expectedErrorStr string | ||
}{ | ||
"basic": { | ||
input: &struct { | ||
AttrStr string | ||
AttrInt int | ||
}{ | ||
AttrStr: "val", | ||
AttrInt: 1, | ||
}, | ||
expected: &destType{ | ||
AttrStr: "val", | ||
AttrInt: 1, | ||
attrUnexported: "", | ||
}, | ||
}, | ||
"missing field": { | ||
input: &struct { | ||
AttrStr string | ||
}{ | ||
AttrStr: "val", | ||
}, | ||
expected: &destType{ | ||
AttrStr: "val", | ||
}, | ||
}, | ||
"extra field": { | ||
input: &struct { | ||
AttrStr string | ||
AttrExtra string | ||
AttrInt int | ||
}{ | ||
AttrStr: "val", | ||
AttrExtra: "extra", | ||
AttrInt: 1, | ||
}, | ||
expected: &destType{ | ||
AttrStr: "val", | ||
AttrInt: 1, | ||
}, | ||
}, | ||
"different type": { | ||
input: &struct { | ||
AttrStr bool | ||
}{ | ||
AttrStr: true, | ||
}, | ||
expectedErrorStr: "field has different type: AttrStr", | ||
}, | ||
"unexported": { | ||
input: &struct { | ||
attrUnexported string | ||
}{ | ||
attrUnexported: "val", | ||
}, | ||
expectedErrorStr: "field can't be set, probably unexported: attrUnexported", | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
dest, err := conversion.CopyModel[destType](tc.input) | ||
if err == nil { | ||
assert.Equal(t, tc.expected, dest) | ||
assert.Equal(t, "", tc.expectedErrorStr) | ||
} else { | ||
require.ErrorContains(t, err, tc.expectedErrorStr) | ||
assert.Nil(t, dest) | ||
assert.Nil(t, tc.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.