return nil
}
-func GetProperty(obj interface{}, name string, suffix string) interface{} {
+func GetProperty(obj interface{}, name string, suffix string, tags ...string) interface{} {
to := reflect.TypeOf(obj)
vo := reflect.ValueOf(obj)
- _, b := to.FieldByName(name + suffix)
+ fieldTO, b := to.FieldByName(name + suffix)
+
+ for _, tx := range tags {
+ if tagValue := fieldTO.Tag.Get(tx); tagValue != "true" {
+ b = false
+ break
+ }
+ }
if b {
fieldVO := vo.FieldByName(name + suffix)
+
return fieldVO.Interface()
}
import "testing"
type testType struct {
- A int
- B int
+ A int `con:"true" con2:"true"`
+ B int `con:"true" con2:"false"`
}
func (t *testType) GetAB(a, b int) (int, int) {
}
}
+func TestFieldTag(t *testing.T) {
+ obj := testType{0, 1}
+
+ i := GetProperty(obj, "B", "", "con", "con2")
+
+ if i != nil {
+ t.Fail()
+ }
+
+ i = GetProperty(obj, "A", "", "con", "con2")
+
+ if i.(int) != 0 {
+ t.Fail()
+ }
+}
+
func TestFieldKo(t *testing.T) {
obj := testType{0, 1}