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 801B

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