You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reflection.go 881B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package unittest
  5. import (
  6. "log"
  7. "reflect"
  8. )
  9. func fieldByName(v reflect.Value, field string) reflect.Value {
  10. if v.Kind() == reflect.Ptr {
  11. v = v.Elem()
  12. }
  13. f := v.FieldByName(field)
  14. if !f.IsValid() {
  15. log.Panicf("can not read %s for %v", field, v)
  16. }
  17. return f
  18. }
  19. type reflectionValue struct {
  20. v reflect.Value
  21. }
  22. func reflectionWrap(v interface{}) *reflectionValue {
  23. return &reflectionValue{v: reflect.ValueOf(v)}
  24. }
  25. func (rv *reflectionValue) int(field string) int {
  26. return int(fieldByName(rv.v, field).Int())
  27. }
  28. func (rv *reflectionValue) str(field string) string {
  29. return fieldByName(rv.v, field).String()
  30. }
  31. func (rv *reflectionValue) bool(field string) bool {
  32. return fieldByName(rv.v, field).Bool()
  33. }