62e6c9bc6c
* Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
113 lines
3 KiB
Go
Vendored
113 lines
3 KiB
Go
Vendored
package reflect2
|
|
|
|
import (
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"unsafe"
|
|
)
|
|
|
|
// typelinks1 for 1.5 ~ 1.6
|
|
//go:linkname typelinks1 reflect.typelinks
|
|
func typelinks1() [][]unsafe.Pointer
|
|
|
|
// typelinks2 for 1.7 ~
|
|
//go:linkname typelinks2 reflect.typelinks
|
|
func typelinks2() (sections []unsafe.Pointer, offset [][]int32)
|
|
|
|
// initOnce guards initialization of types and packages
|
|
var initOnce sync.Once
|
|
|
|
var types map[string]reflect.Type
|
|
var packages map[string]map[string]reflect.Type
|
|
|
|
// discoverTypes initializes types and packages
|
|
func discoverTypes() {
|
|
types = make(map[string]reflect.Type)
|
|
packages = make(map[string]map[string]reflect.Type)
|
|
|
|
ver := runtime.Version()
|
|
if ver == "go1.5" || strings.HasPrefix(ver, "go1.5.") {
|
|
loadGo15Types()
|
|
} else if ver == "go1.6" || strings.HasPrefix(ver, "go1.6.") {
|
|
loadGo15Types()
|
|
} else {
|
|
loadGo17Types()
|
|
}
|
|
}
|
|
|
|
func loadGo15Types() {
|
|
var obj interface{} = reflect.TypeOf(0)
|
|
typePtrss := typelinks1()
|
|
for _, typePtrs := range typePtrss {
|
|
for _, typePtr := range typePtrs {
|
|
(*emptyInterface)(unsafe.Pointer(&obj)).word = typePtr
|
|
typ := obj.(reflect.Type)
|
|
if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
|
|
loadedType := typ.Elem()
|
|
pkgTypes := packages[loadedType.PkgPath()]
|
|
if pkgTypes == nil {
|
|
pkgTypes = map[string]reflect.Type{}
|
|
packages[loadedType.PkgPath()] = pkgTypes
|
|
}
|
|
types[loadedType.String()] = loadedType
|
|
pkgTypes[loadedType.Name()] = loadedType
|
|
}
|
|
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Ptr &&
|
|
typ.Elem().Elem().Kind() == reflect.Struct {
|
|
loadedType := typ.Elem().Elem()
|
|
pkgTypes := packages[loadedType.PkgPath()]
|
|
if pkgTypes == nil {
|
|
pkgTypes = map[string]reflect.Type{}
|
|
packages[loadedType.PkgPath()] = pkgTypes
|
|
}
|
|
types[loadedType.String()] = loadedType
|
|
pkgTypes[loadedType.Name()] = loadedType
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadGo17Types() {
|
|
var obj interface{} = reflect.TypeOf(0)
|
|
sections, offset := typelinks2()
|
|
for i, offs := range offset {
|
|
rodata := sections[i]
|
|
for _, off := range offs {
|
|
(*emptyInterface)(unsafe.Pointer(&obj)).word = resolveTypeOff(unsafe.Pointer(rodata), off)
|
|
typ := obj.(reflect.Type)
|
|
if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
|
|
loadedType := typ.Elem()
|
|
pkgTypes := packages[loadedType.PkgPath()]
|
|
if pkgTypes == nil {
|
|
pkgTypes = map[string]reflect.Type{}
|
|
packages[loadedType.PkgPath()] = pkgTypes
|
|
}
|
|
types[loadedType.String()] = loadedType
|
|
pkgTypes[loadedType.Name()] = loadedType
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type emptyInterface struct {
|
|
typ unsafe.Pointer
|
|
word unsafe.Pointer
|
|
}
|
|
|
|
// TypeByName return the type by its name, just like Class.forName in java
|
|
func TypeByName(typeName string) Type {
|
|
initOnce.Do(discoverTypes)
|
|
return Type2(types[typeName])
|
|
}
|
|
|
|
// TypeByPackageName return the type by its package and name
|
|
func TypeByPackageName(pkgPath string, name string) Type {
|
|
initOnce.Do(discoverTypes)
|
|
pkgTypes := packages[pkgPath]
|
|
if pkgTypes == nil {
|
|
return nil
|
|
}
|
|
return Type2(pkgTypes[name])
|
|
}
|