-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8fa436e
Showing
5 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# omg.testingTools | ||
|
||
--- | ||
|
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,3 @@ | ||
module omg.testingtools | ||
|
||
go 1.17 |
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,45 @@ | ||
package testingtools | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
func SetPrivateValue(obj interface{}, fieldName string, value interface{}) { | ||
reflectValue := reflect.ValueOf(obj) | ||
|
||
if reflectValue.Kind() != reflect.Ptr { | ||
panic("object is not a pointer") | ||
} | ||
|
||
reflectValue = reflectValue.Elem() | ||
reflectType := reflect.TypeOf(obj).Elem() | ||
|
||
if reflectValue.Kind() != reflect.Struct { | ||
panic("object is not a pointer to struct") | ||
} | ||
|
||
newReflectValueType := reflect.TypeOf(value) | ||
|
||
var offset uintptr | ||
|
||
for i := 0; i < reflectValue.NumField(); i++ { | ||
if reflectType.Field(i).Name == fieldName { | ||
currentFieldType := reflectType.Field(i).Type.String() | ||
|
||
if newReflectValueType.String() != currentFieldType { | ||
panic(fmt.Sprintf("incorrect type: must be %s", currentFieldType)) | ||
} | ||
|
||
fieldPtr := unsafe.Pointer(reflectValue.UnsafeAddr() + offset) | ||
reflect.NewAt(newReflectValueType, fieldPtr).Elem().Set(reflect.ValueOf(value)) | ||
|
||
return | ||
} | ||
|
||
offset += reflectValue.Field(i).Type().Size() | ||
} | ||
|
||
panic(fmt.Sprintf("Field [%s] not found in struct", fieldName)) | ||
} |
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,18 @@ | ||
package testingtools | ||
|
||
import ( | ||
"fmt" | ||
"omg.testingtools/testmodule" | ||
"testing" | ||
) | ||
|
||
func TestSetPrivateValue(t *testing.T) { | ||
qq := testmodule.NewQQ(123, "abc", []int{5, 6, 7}) | ||
|
||
SetPrivateValue(&qq, "c", []int{1, 2, 3, 4}) | ||
SetPrivateValue(&qq, "b", "qwe") | ||
|
||
if fmt.Sprint(qq) != "{123 qwe [1 2 3 4]}" { | ||
t.Fail() | ||
} | ||
} |
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,13 @@ | ||
package testmodule | ||
|
||
type TestStruct struct { | ||
a int | ||
b string | ||
c []int | ||
} | ||
|
||
func NewQQ(a int, b string, c []int) TestStruct { | ||
return TestStruct{ | ||
a, b, c, | ||
} | ||
} |