code.gitea.io/gitea@v1.21.7/models/unittest/reflection.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package unittest 5 6 import ( 7 "log" 8 "reflect" 9 ) 10 11 func fieldByName(v reflect.Value, field string) reflect.Value { 12 if v.Kind() == reflect.Ptr { 13 v = v.Elem() 14 } 15 f := v.FieldByName(field) 16 if !f.IsValid() { 17 log.Panicf("can not read %s for %v", field, v) 18 } 19 return f 20 } 21 22 type reflectionValue struct { 23 v reflect.Value 24 } 25 26 func reflectionWrap(v any) *reflectionValue { 27 return &reflectionValue{v: reflect.ValueOf(v)} 28 } 29 30 func (rv *reflectionValue) int(field string) int { 31 return int(fieldByName(rv.v, field).Int()) 32 } 33 34 func (rv *reflectionValue) str(field string) string { 35 return fieldByName(rv.v, field).String() 36 } 37 38 func (rv *reflectionValue) bool(field string) bool { 39 return fieldByName(rv.v, field).Bool() 40 }