From: Mattia Cabrini Date: Sat, 8 Jul 2023 07:31:52 +0000 (+0200) Subject: Reflect Field X-Git-Tag: v0.0.2 X-Git-Url: https://git.theboydaily.dev/mattia?a=commitdiff_plain;h=064b8903efc3a60da6fffc99198362d97ce89db6;p=go-utility.git Reflect Field --- diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4bc0337 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/methods_test.go b/methods_test.go deleted file mode 100644 index 1b340b2..0000000 --- a/methods_test.go +++ /dev/null @@ -1,163 +0,0 @@ -/* -MIT License - -Copyright (c) 2023 Mattia Cabrini - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -package utility - -import "testing" - -type testType struct { - A int - B int -} - -func (t *testType) GetAB(a, b int) (int, int) { - return a + t.A, b + t.B -} - -func (t *testType) GetABinterface(a int, b interface{}) (int, int) { - return a + t.A, 0 -} - -func (t *testType) GetABstruct(a int, b testType) (int, int) { - return a + t.A, t.B + b.B -} - -func TestMethodOk0(t *testing.T) { - obj := testType{0, 1} - - f := GetMethod(&obj, "GetAB", "") - - if f == nil { - t.Fail() - return - } - - results, err := f(0, 1) - - if err != nil { - t.Error(err) - return - } - - if results[0].(int) != 0 { - t.Error(0) - } - if results[1].(int) != 2 { - t.Error(1) - } -} - -func TestMethodOk1(t *testing.T) { - obj := testType{0, 1} - - f := GetMethod(&obj, "GetAB", "interface") - - if f == nil { - t.Fail() - return - } - - results, err := f(0, 1) - - if err != nil { - t.Error(err) - return - } - - if results[0].(int) != 0 { - t.Error(0) - } - if results[1].(int) != 0 { - t.Error(1) - } -} - -func TestMethodOk2(t *testing.T) { - obj := testType{0, 1} - - f := GetMethod(&obj, "GetAB", "struct") - - if f == nil { - t.Fail() - return - } - - results, err := f(0, obj) - - if err != nil { - t.Error(err) - return - } - - if results[0].(int) != 0 { - t.Error(0) - } - if results[1].(int) != 2 { - t.Error(1) - } -} - -func TestMethodArgCountKo(t *testing.T) { - obj := testType{0, 1} - - f := GetMethod(&obj, "Get", "AB") - - if f == nil { - t.Fail() - return - } - - _, err := f(0) - - if err == nil { - t.Fail() - } -} - -func TestMethodArgTypeKo(t *testing.T) { - obj := testType{0, 1} - - f := GetMethod(&obj, "Get", "AB") - - if f == nil { - t.Fail() - return - } - - _, err := f(0, "1") - - if err == nil { - t.Fail() - } -} - -func TestMethodNameKo(t *testing.T) { - obj := testType{0, 1} - - f := GetMethod(&obj, "Get2", "AB") - - if f != nil { - t.Fail() - } -} diff --git a/reflect.go b/reflect.go index ed89d80..1589c0c 100644 --- a/reflect.go +++ b/reflect.go @@ -88,6 +88,20 @@ func GetMethod(obj interface{}, name string, suffix string) GenericFunc { return nil } +func GetProperty(obj interface{}, name string, suffix string) interface{} { + to := reflect.TypeOf(obj) + vo := reflect.ValueOf(obj) + + _, b := to.FieldByName(name + suffix) + + if b { + fieldVO := vo.FieldByName(name + suffix) + return fieldVO.Interface() + } + + return nil +} + var typeDefault = map[reflect.Kind]interface{}{ reflect.String: "", reflect.Int64: int64(0), diff --git a/reflect_test.go b/reflect_test.go new file mode 100644 index 0000000..cc27ac2 --- /dev/null +++ b/reflect_test.go @@ -0,0 +1,183 @@ +/* +MIT License + +Copyright (c) 2023 Mattia Cabrini + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package utility + +import "testing" + +type testType struct { + A int + B int +} + +func (t *testType) GetAB(a, b int) (int, int) { + return a + t.A, b + t.B +} + +func (t *testType) GetABinterface(a int, b interface{}) (int, int) { + return a + t.A, 0 +} + +func (t *testType) GetABstruct(a int, b testType) (int, int) { + return a + t.A, t.B + b.B +} + +func TestMethodOk0(t *testing.T) { + obj := testType{0, 1} + + f := GetMethod(&obj, "GetAB", "") + + if f == nil { + t.Fail() + return + } + + results, err := f(0, 1) + + if err != nil { + t.Error(err) + return + } + + if results[0].(int) != 0 { + t.Error(0) + } + if results[1].(int) != 2 { + t.Error(1) + } +} + +func TestMethodOk1(t *testing.T) { + obj := testType{0, 1} + + f := GetMethod(&obj, "GetAB", "interface") + + if f == nil { + t.Fail() + return + } + + results, err := f(0, 1) + + if err != nil { + t.Error(err) + return + } + + if results[0].(int) != 0 { + t.Error(0) + } + if results[1].(int) != 0 { + t.Error(1) + } +} + +func TestMethodOk2(t *testing.T) { + obj := testType{0, 1} + + f := GetMethod(&obj, "GetAB", "struct") + + if f == nil { + t.Fail() + return + } + + results, err := f(0, obj) + + if err != nil { + t.Error(err) + return + } + + if results[0].(int) != 0 { + t.Error(0) + } + if results[1].(int) != 2 { + t.Error(1) + } +} + +func TestMethodArgCountKo(t *testing.T) { + obj := testType{0, 1} + + f := GetMethod(&obj, "Get", "AB") + + if f == nil { + t.Fail() + return + } + + _, err := f(0) + + if err == nil { + t.Fail() + } +} + +func TestMethodArgTypeKo(t *testing.T) { + obj := testType{0, 1} + + f := GetMethod(&obj, "Get", "AB") + + if f == nil { + t.Fail() + return + } + + _, err := f(0, "1") + + if err == nil { + t.Fail() + } +} + +func TestMethodNameKo(t *testing.T) { + obj := testType{0, 1} + + f := GetMethod(&obj, "Get2", "AB") + + if f != nil { + t.Fail() + } +} + +func TestFieldOk(t *testing.T) { + obj := testType{0, 1} + + i := GetProperty(obj, "B", "") + + if i.(int) != 1 { + t.Fail() + } +} + +func TestFieldKo(t *testing.T) { + obj := testType{0, 1} + + i := GetProperty(obj, "c", "") + + if i != nil { + t.Fail() + } +}