Replace direct usage of the API with go-jira
This commit is contained in:
parent
ae4b6e6620
commit
4933ed1147
57 changed files with 7699 additions and 114 deletions
80
vendor/github.com/trivago/tgo/treflect/clone.go
generated
vendored
Normal file
80
vendor/github.com/trivago/tgo/treflect/clone.go
generated
vendored
Normal file
|
@ -0,0 +1,80 @@
|
|||
// Copyright 2015-2018 trivago N.V.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package treflect
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Clone does a deep copy of the given value.
|
||||
// Please note that field have to be public in order to be copied.
|
||||
// Private fields will be ignored.
|
||||
func Clone(v interface{}) interface{} {
|
||||
value := reflect.ValueOf(v)
|
||||
copy := clone(value)
|
||||
return copy.Interface()
|
||||
}
|
||||
|
||||
func clone(v reflect.Value) reflect.Value {
|
||||
switch v.Kind() {
|
||||
case reflect.Struct:
|
||||
copy := reflect.New(v.Type())
|
||||
|
||||
for i := 0; i < v.Type().NumField(); i++ {
|
||||
field := v.Field(i)
|
||||
targetField := copy.Elem().Field(i)
|
||||
if !targetField.CanSet() {
|
||||
continue // ignore private fields
|
||||
}
|
||||
fieldCopy := clone(field)
|
||||
targetField.Set(fieldCopy)
|
||||
}
|
||||
return copy.Elem()
|
||||
|
||||
case reflect.Chan:
|
||||
copy := reflect.MakeChan(v.Type(), v.Len())
|
||||
return copy
|
||||
|
||||
case reflect.Map:
|
||||
copy := reflect.MakeMap(v.Type())
|
||||
keys := v.MapKeys()
|
||||
for _, k := range keys {
|
||||
fieldCopy := clone(v.MapIndex(k))
|
||||
copy.SetMapIndex(k, fieldCopy)
|
||||
}
|
||||
return copy
|
||||
|
||||
case reflect.Slice:
|
||||
copy := reflect.MakeSlice(v.Type(), v.Len(), v.Len())
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
elementCopy := clone(v.Index(i))
|
||||
copy.Index(i).Set(elementCopy)
|
||||
}
|
||||
return copy
|
||||
|
||||
case reflect.Array:
|
||||
copy := reflect.New(v.Type()).Elem()
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
elementCopy := clone(v.Index(i))
|
||||
copy.Index(i).Set(elementCopy)
|
||||
}
|
||||
return copy
|
||||
|
||||
default:
|
||||
copy := reflect.New(v.Type())
|
||||
copy.Elem().Set(v)
|
||||
return copy.Elem()
|
||||
}
|
||||
}
|
371
vendor/github.com/trivago/tgo/treflect/reflection.go
generated
vendored
Normal file
371
vendor/github.com/trivago/tgo/treflect/reflection.go
generated
vendored
Normal file
|
@ -0,0 +1,371 @@
|
|||
// Copyright 2015-2018 trivago N.V.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package treflect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// GetMissingMethods checks if a given object implements all methods of a
|
||||
// given interface. It returns the interface coverage [0..1] as well as an array
|
||||
// of error messages. If the interface is correctly implemented the coverage is
|
||||
// 1 and the error message array is empty.
|
||||
func GetMissingMethods(objType reflect.Type, ifaceType reflect.Type) (float32, []string) {
|
||||
missing := []string{}
|
||||
if objType.Implements(ifaceType) {
|
||||
return 1.0, missing
|
||||
}
|
||||
|
||||
methodCount := ifaceType.NumMethod()
|
||||
for mIdx := 0; mIdx < methodCount; mIdx++ {
|
||||
ifaceMethod := ifaceType.Method(mIdx)
|
||||
objMethod, exists := objType.MethodByName(ifaceMethod.Name)
|
||||
signatureMismatch := false
|
||||
|
||||
switch {
|
||||
case !exists:
|
||||
missing = append(missing, fmt.Sprintf("Missing: \"%s\" %v", ifaceMethod.Name, ifaceMethod.Type))
|
||||
continue // ### continue, error found ###
|
||||
|
||||
case ifaceMethod.Type.NumOut() != objMethod.Type.NumOut():
|
||||
signatureMismatch = true
|
||||
|
||||
case ifaceMethod.Type.NumIn()+1 != objMethod.Type.NumIn():
|
||||
signatureMismatch = true
|
||||
|
||||
default:
|
||||
for oIdx := 0; !signatureMismatch && oIdx < ifaceMethod.Type.NumOut(); oIdx++ {
|
||||
signatureMismatch = ifaceMethod.Type.Out(oIdx) != objMethod.Type.Out(oIdx)
|
||||
}
|
||||
for iIdx := 0; !signatureMismatch && iIdx < ifaceMethod.Type.NumIn(); iIdx++ {
|
||||
signatureMismatch = ifaceMethod.Type.In(iIdx) != objMethod.Type.In(iIdx+1)
|
||||
}
|
||||
}
|
||||
|
||||
if signatureMismatch {
|
||||
missing = append(missing, fmt.Sprintf("Invalid: \"%s\" %v is not %v", ifaceMethod.Name, objMethod.Type, ifaceMethod.Type))
|
||||
}
|
||||
}
|
||||
|
||||
return float32(methodCount-len(missing)) / float32(methodCount), missing
|
||||
}
|
||||
|
||||
// Int64 converts any signed number type to an int64.
|
||||
// The second parameter is returned as false if a non-number type was given.
|
||||
func Int64(v interface{}) (int64, bool) {
|
||||
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.Int:
|
||||
return int64(v.(int)), true
|
||||
case reflect.Int8:
|
||||
return int64(v.(int8)), true
|
||||
case reflect.Int16:
|
||||
return int64(v.(int16)), true
|
||||
case reflect.Int32:
|
||||
return int64(v.(int32)), true
|
||||
case reflect.Int64:
|
||||
return v.(int64), true
|
||||
case reflect.Float32:
|
||||
return int64(v.(float32)), true
|
||||
case reflect.Float64:
|
||||
return int64(v.(float64)), true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Uint64 converts any unsigned number type to an uint64.
|
||||
// The second parameter is returned as false if a non-number type was given.
|
||||
func Uint64(v interface{}) (uint64, bool) {
|
||||
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.Uint:
|
||||
return uint64(v.(uint)), true
|
||||
case reflect.Uint8:
|
||||
return uint64(v.(uint8)), true
|
||||
case reflect.Uint16:
|
||||
return uint64(v.(uint16)), true
|
||||
case reflect.Uint32:
|
||||
return uint64(v.(uint32)), true
|
||||
case reflect.Uint64:
|
||||
return v.(uint64), true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Float32 converts any number type to an float32.
|
||||
// The second parameter is returned as false if a non-number type was given.
|
||||
func Float32(v interface{}) (float32, bool) {
|
||||
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.Int:
|
||||
return float32(v.(int)), true
|
||||
case reflect.Uint:
|
||||
return float32(v.(uint)), true
|
||||
case reflect.Int8:
|
||||
return float32(v.(int8)), true
|
||||
case reflect.Uint8:
|
||||
return float32(v.(uint8)), true
|
||||
case reflect.Int16:
|
||||
return float32(v.(int16)), true
|
||||
case reflect.Uint16:
|
||||
return float32(v.(uint16)), true
|
||||
case reflect.Int32:
|
||||
return float32(v.(int32)), true
|
||||
case reflect.Uint32:
|
||||
return float32(v.(uint32)), true
|
||||
case reflect.Int64:
|
||||
return float32(v.(int64)), true
|
||||
case reflect.Uint64:
|
||||
return float32(v.(uint64)), true
|
||||
case reflect.Float32:
|
||||
return v.(float32), true
|
||||
case reflect.Float64:
|
||||
return float32(v.(float64)), true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Float64 converts any number type to an float64.
|
||||
// The second parameter is returned as false if a non-number type was given.
|
||||
func Float64(v interface{}) (float64, bool) {
|
||||
|
||||
switch reflect.TypeOf(v).Kind() {
|
||||
case reflect.Int:
|
||||
return float64(v.(int)), true
|
||||
case reflect.Uint:
|
||||
return float64(v.(uint)), true
|
||||
case reflect.Int8:
|
||||
return float64(v.(int8)), true
|
||||
case reflect.Uint8:
|
||||
return float64(v.(uint8)), true
|
||||
case reflect.Int16:
|
||||
return float64(v.(int16)), true
|
||||
case reflect.Uint16:
|
||||
return float64(v.(uint16)), true
|
||||
case reflect.Int32:
|
||||
return float64(v.(int32)), true
|
||||
case reflect.Uint32:
|
||||
return float64(v.(uint32)), true
|
||||
case reflect.Int64:
|
||||
return float64(v.(int64)), true
|
||||
case reflect.Uint64:
|
||||
return float64(v.(uint64)), true
|
||||
case reflect.Float32:
|
||||
return float64(v.(float32)), true
|
||||
case reflect.Float64:
|
||||
return v.(float64), true
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// RemovePtrFromType will return the type of t and strips away any pointer(s)
|
||||
// in front of the actual type.
|
||||
func RemovePtrFromType(t interface{}) reflect.Type {
|
||||
var v reflect.Type
|
||||
if rt, isType := t.(reflect.Type); isType {
|
||||
v = rt
|
||||
} else {
|
||||
v = reflect.TypeOf(t)
|
||||
}
|
||||
for v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// RemovePtrFromValue will return the value of t and strips away any pointer(s)
|
||||
// in front of the actual type.
|
||||
func RemovePtrFromValue(t interface{}) reflect.Value {
|
||||
var v reflect.Value
|
||||
if rv, isValue := t.(reflect.Value); isValue {
|
||||
v = rv
|
||||
} else {
|
||||
v = reflect.ValueOf(t)
|
||||
}
|
||||
for v.Type().Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// UnsafeCopy will copy data from src to dst while ignoring type information.
|
||||
// Both types need to be of the same size and dst and src have to be pointers.
|
||||
// UnsafeCopy will panic if these requirements are not met.
|
||||
func UnsafeCopy(dst, src interface{}) {
|
||||
dstValue := reflect.ValueOf(dst)
|
||||
srcValue := reflect.ValueOf(src)
|
||||
UnsafeCopyValue(dstValue, srcValue)
|
||||
}
|
||||
|
||||
// UnsafeCopyValue will copy data from src to dst while ignoring type
|
||||
// information. Both types need to be of the same size or this function will
|
||||
// panic. Also both types must support dereferencing via reflect.Elem()
|
||||
func UnsafeCopyValue(dstValue reflect.Value, srcValue reflect.Value) {
|
||||
dstType := dstValue.Elem().Type()
|
||||
srcType := srcValue.Type()
|
||||
|
||||
var srcPtr uintptr
|
||||
if srcValue.Kind() != reflect.Ptr {
|
||||
// If we don't get a pointer to our source data we need to forcefully
|
||||
// retrieve it by accessing the interface pointer. This is ok as we
|
||||
// only read from it.
|
||||
iface := srcValue.Interface()
|
||||
srcPtr = reflect.ValueOf(&iface).Elem().InterfaceData()[1] // Pointer to data
|
||||
} else {
|
||||
srcType = srcValue.Elem().Type()
|
||||
srcPtr = srcValue.Pointer()
|
||||
}
|
||||
|
||||
if dstType.Size() != srcType.Size() {
|
||||
panic("Type size mismatch between " + dstType.String() + " and " + srcType.String())
|
||||
}
|
||||
|
||||
dstAsSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: dstValue.Pointer(),
|
||||
Len: int(dstType.Size()),
|
||||
Cap: int(dstType.Size()),
|
||||
}))
|
||||
|
||||
srcAsSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: srcPtr,
|
||||
Len: int(srcType.Size()),
|
||||
Cap: int(srcType.Size()),
|
||||
}))
|
||||
|
||||
copy(dstAsSlice, srcAsSlice)
|
||||
}
|
||||
|
||||
// SetMemberByName sets member name of the given pointer-to-struct to the data
|
||||
// passed to this function. The member may be private, too.
|
||||
func SetMemberByName(ptrToStruct interface{}, name string, data interface{}) {
|
||||
structVal := reflect.Indirect(reflect.ValueOf(ptrToStruct))
|
||||
member := structVal.FieldByName(name)
|
||||
|
||||
SetValue(member, data)
|
||||
}
|
||||
|
||||
// SetMemberByIndex sets member idx of the given pointer-to-struct to the data
|
||||
// passed to this function. The member may be private, too.
|
||||
func SetMemberByIndex(ptrToStruct interface{}, idx int, data interface{}) {
|
||||
structVal := reflect.Indirect(reflect.ValueOf(ptrToStruct))
|
||||
member := structVal.Field(idx)
|
||||
|
||||
SetValue(member, data)
|
||||
}
|
||||
|
||||
// SetValue sets an addressable value to the data passed to this function.
|
||||
// In contrast to golangs reflect package this will also work with private
|
||||
// variables. Please note that this function may not support all types, yet.
|
||||
func SetValue(member reflect.Value, data interface{}) {
|
||||
if member.CanSet() {
|
||||
member.Set(reflect.ValueOf(data).Convert(member.Type()))
|
||||
return // ### return, easy way ###
|
||||
}
|
||||
|
||||
if !member.CanAddr() {
|
||||
panic("SetValue requires addressable member type")
|
||||
}
|
||||
|
||||
ptrToMember := unsafe.Pointer(member.UnsafeAddr())
|
||||
dataValue := reflect.ValueOf(data)
|
||||
|
||||
switch member.Kind() {
|
||||
case reflect.Bool:
|
||||
*(*bool)(ptrToMember) = dataValue.Bool()
|
||||
|
||||
case reflect.Uint:
|
||||
*(*uint)(ptrToMember) = uint(dataValue.Uint())
|
||||
|
||||
case reflect.Uint8:
|
||||
*(*uint8)(ptrToMember) = uint8(dataValue.Uint())
|
||||
|
||||
case reflect.Uint16:
|
||||
*(*uint16)(ptrToMember) = uint16(dataValue.Uint())
|
||||
|
||||
case reflect.Uint32:
|
||||
*(*uint32)(ptrToMember) = uint32(dataValue.Uint())
|
||||
|
||||
case reflect.Uint64:
|
||||
*(*uint64)(ptrToMember) = dataValue.Uint()
|
||||
|
||||
case reflect.Int:
|
||||
*(*int)(ptrToMember) = int(dataValue.Int())
|
||||
|
||||
case reflect.Int8:
|
||||
*(*int8)(ptrToMember) = int8(dataValue.Int())
|
||||
|
||||
case reflect.Int16:
|
||||
*(*int16)(ptrToMember) = int16(dataValue.Int())
|
||||
|
||||
case reflect.Int32:
|
||||
*(*int32)(ptrToMember) = int32(dataValue.Int())
|
||||
|
||||
case reflect.Int64:
|
||||
*(*int64)(ptrToMember) = dataValue.Int()
|
||||
|
||||
case reflect.Float32:
|
||||
*(*float32)(ptrToMember) = float32(dataValue.Float())
|
||||
|
||||
case reflect.Float64:
|
||||
*(*float64)(ptrToMember) = dataValue.Float()
|
||||
|
||||
case reflect.Complex64:
|
||||
*(*complex64)(ptrToMember) = complex64(dataValue.Complex())
|
||||
|
||||
case reflect.Complex128:
|
||||
*(*complex128)(ptrToMember) = dataValue.Complex()
|
||||
|
||||
case reflect.String:
|
||||
*(*string)(ptrToMember) = dataValue.String()
|
||||
|
||||
case reflect.Map, reflect.Chan:
|
||||
// Exploit the fact that "map" is actually "*runtime.hmap" and force
|
||||
// overwrite that pointer in the passed struct.
|
||||
// Same foes for "chan" which is actually "*runtime.hchan".
|
||||
|
||||
// Note: Assigning a map or channel to another variable does NOT copy
|
||||
// the contents so copying the pointer follows go's standard behavior.
|
||||
dataAsPtr := unsafe.Pointer(dataValue.Pointer())
|
||||
*(**uintptr)(ptrToMember) = (*uintptr)(dataAsPtr)
|
||||
|
||||
case reflect.Interface:
|
||||
// Interfaces are basically two pointers, see runtime.iface.
|
||||
// We want to modify exactly that data, which is returned by
|
||||
// the InterfaceData() method.
|
||||
|
||||
if dataValue.Kind() != reflect.Interface {
|
||||
// A type reference was passed. In order to overwrite the memory
|
||||
// Representation of an interface we need to generate it first.
|
||||
// Reflect does not allow us to do that unless we use the
|
||||
// InterfaceData method which exposes the internal representation
|
||||
// of an interface.
|
||||
interfaceData := reflect.ValueOf(&data).Elem().InterfaceData()
|
||||
dataValue = reflect.ValueOf(interfaceData)
|
||||
}
|
||||
fallthrough
|
||||
|
||||
default:
|
||||
// Complex types are assigned memcpy style.
|
||||
// Note: This should not break the garbage collector although we cannot
|
||||
// be 100% sure on this.
|
||||
UnsafeCopyValue(member.Addr(), dataValue)
|
||||
}
|
||||
}
|
97
vendor/github.com/trivago/tgo/treflect/typeregistry.go
generated
vendored
Normal file
97
vendor/github.com/trivago/tgo/treflect/typeregistry.go
generated
vendored
Normal file
|
@ -0,0 +1,97 @@
|
|||
// Copyright 2015-2018 trivago N.V.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package treflect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TypeRegistry is a name to type registry used to create objects by name.
|
||||
type TypeRegistry struct {
|
||||
namedType map[string]reflect.Type
|
||||
}
|
||||
|
||||
// NewTypeRegistry creates a new TypeRegistry. Note that there is a global type
|
||||
// registry available in the main tgo package (tgo.TypeRegistry).
|
||||
func NewTypeRegistry() TypeRegistry {
|
||||
return TypeRegistry{
|
||||
namedType: make(map[string]reflect.Type),
|
||||
}
|
||||
}
|
||||
|
||||
// Register a plugin to the TypeRegistry by passing an uninitialized object.
|
||||
func (registry TypeRegistry) Register(typeInstance interface{}) {
|
||||
registry.RegisterWithDepth(typeInstance, 1)
|
||||
}
|
||||
|
||||
// RegisterWithDepth to register a plugin to the TypeRegistry by passing an uninitialized object.
|
||||
func (registry TypeRegistry) RegisterWithDepth(typeInstance interface{}, depth int) {
|
||||
structType := reflect.TypeOf(typeInstance)
|
||||
packageName := structType.PkgPath()
|
||||
typeName := structType.Name()
|
||||
|
||||
pathTokens := strings.Split(packageName, "/")
|
||||
maxDepth := 3
|
||||
if len(pathTokens) < maxDepth {
|
||||
maxDepth = len(pathTokens)
|
||||
}
|
||||
|
||||
for n := depth; n <= maxDepth; n++ {
|
||||
shortTypeName := strings.Join(pathTokens[len(pathTokens)-n:], ".") + "." + typeName
|
||||
registry.namedType[shortTypeName] = structType
|
||||
}
|
||||
}
|
||||
|
||||
// New creates an uninitialized object by class name.
|
||||
// The class name has to be "package.class" or "package/subpackage.class".
|
||||
// The gollum package is omitted from the package path.
|
||||
func (registry TypeRegistry) New(typeName string) (interface{}, error) {
|
||||
structType, exists := registry.namedType[typeName]
|
||||
if exists {
|
||||
return reflect.New(structType).Interface(), nil
|
||||
}
|
||||
return nil, fmt.Errorf("Unknown class: %s", typeName)
|
||||
}
|
||||
|
||||
// GetTypeOf returns only the type asscociated with the given name.
|
||||
// If the name is not registered, nil is returned.
|
||||
// The type returned will be a pointer type.
|
||||
func (registry TypeRegistry) GetTypeOf(typeName string) reflect.Type {
|
||||
if structType, exists := registry.namedType[typeName]; exists {
|
||||
return reflect.PtrTo(structType)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsTypeRegistered returns true if a type is registered to this registry.
|
||||
// Note that GetTypeOf can do the same thing by checking for nil but also
|
||||
// returns the type, so in many cases you will want to call this function.
|
||||
func (registry TypeRegistry) IsTypeRegistered(typeName string) bool {
|
||||
_, exists := registry.namedType[typeName]
|
||||
return exists
|
||||
}
|
||||
|
||||
// GetRegistered returns the names of all registered types for a given package
|
||||
func (registry TypeRegistry) GetRegistered(packageName string) []string {
|
||||
var result []string
|
||||
for key := range registry.namedType {
|
||||
if strings.HasPrefix(key, packageName) {
|
||||
result = append(result, key)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue