mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-14 06:50:25 +00:00
- Go has a suite of small linters that helps with modernizing Go code by using newer functions and catching small mistakes, https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize. - Enable this linter in golangci-lint. - There's also [`go fix`](https://go.dev/blog/gofix), which is not yet released as a linter in golangci-lint: https://github.com/golangci/golangci-lint/pull/6385 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11936 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
40 lines
805 B
Go
40 lines
805 B
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package unittest
|
|
|
|
import (
|
|
"log"
|
|
"reflect"
|
|
)
|
|
|
|
func fieldByName(v reflect.Value, field string) reflect.Value {
|
|
if v.Kind() == reflect.Pointer {
|
|
v = v.Elem()
|
|
}
|
|
f := v.FieldByName(field)
|
|
if !f.IsValid() {
|
|
log.Panicf("can not read %s for %v", field, v)
|
|
}
|
|
return f
|
|
}
|
|
|
|
type reflectionValue struct {
|
|
v reflect.Value
|
|
}
|
|
|
|
func reflectionWrap(v any) *reflectionValue {
|
|
return &reflectionValue{v: reflect.ValueOf(v)}
|
|
}
|
|
|
|
func (rv *reflectionValue) int(field string) int {
|
|
return int(fieldByName(rv.v, field).Int())
|
|
}
|
|
|
|
func (rv *reflectionValue) str(field string) string {
|
|
return fieldByName(rv.v, field).String()
|
|
}
|
|
|
|
func (rv *reflectionValue) bool(field string) bool {
|
|
return fieldByName(rv.v, field).Bool()
|
|
}
|