Adding a default case for type conversion using the reflect package
This commit is contained in:
parent
95b4154abb
commit
656c40c1a7
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -733,6 +734,14 @@ func ToActivity(it Item) (*Activity, error) {
|
|||
return (*Activity)(unsafe.Pointer(i)), nil
|
||||
case Question:
|
||||
return (*Activity)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Activity))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Activity); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert activity")
|
||||
}
|
||||
|
|
9
actor.go
9
actor.go
|
@ -3,6 +3,7 @@ package activitypub
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/buger/jsonparser"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -448,6 +449,14 @@ func ToActor(it Item) (*Actor, error) {
|
|||
return (*Actor)(unsafe.Pointer(i)), nil
|
||||
case Object:
|
||||
return (*Actor)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Actor))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Actor); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to convert %T to actor", it)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -273,6 +274,14 @@ func ToCollection(it Item) (*Collection, error) {
|
|||
return (*Collection)(unsafe.Pointer(i)), nil
|
||||
case CollectionPage:
|
||||
return (*Collection)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Collection))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Collection); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to collection")
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -281,6 +282,14 @@ func ToCollectionPage(it Item) (*CollectionPage, error) {
|
|||
return i, nil
|
||||
case CollectionPage:
|
||||
return &i, nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(CollectionPage))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*CollectionPage); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to collection page")
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -218,6 +219,14 @@ func ToIntransitiveActivity(it Item) (*IntransitiveActivity, error) {
|
|||
return i, nil
|
||||
case IntransitiveActivity:
|
||||
return &i, nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(IntransitiveActivity))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*IntransitiveActivity); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to intransitive activity")
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
@ -173,6 +174,14 @@ func ToItemCollection(it Item) (*ItemCollection, error) {
|
|||
return i, nil
|
||||
case ItemCollection:
|
||||
return &i, nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(ItemCollection))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*ItemCollection); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to item collection")
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package activitypub
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/buger/jsonparser"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
@ -366,6 +367,14 @@ func ToObject(it Item) (*Object, error) {
|
|||
return (*Object)(unsafe.Pointer(i)), nil
|
||||
case OrderedCollectionPage:
|
||||
return (*Object)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Object))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Object); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -281,6 +282,14 @@ func ToOrderedCollection(it Item) (*OrderedCollection, error) {
|
|||
return (*OrderedCollection)(unsafe.Pointer(i)), nil
|
||||
case OrderedCollectionPage:
|
||||
return (*OrderedCollection)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(OrderedCollection))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*OrderedCollection); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to ordered collection")
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -236,6 +237,12 @@ func ToOrderedCollectionPage(it Item) (*OrderedCollectionPage, error) {
|
|||
return i, nil
|
||||
case OrderedCollectionPage:
|
||||
return &i, nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(OrderedCollectionPage))
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*OrderedCollectionPage); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to ordered collection page")
|
||||
}
|
||||
|
|
9
place.go
9
place.go
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -216,6 +217,14 @@ func ToPlace(it Item) (*Place, error) {
|
|||
// FIXME(marius): **memory_safety** Place has extra properties which will point to invalid memory
|
||||
// we need a safe version for converting from smaller objects to larger ones
|
||||
return (*Place)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Place))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Place); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -184,6 +185,14 @@ func ToProfile(it Item) (*Profile, error) {
|
|||
return (*Profile)(unsafe.Pointer(i)), nil
|
||||
case Object:
|
||||
return (*Profile)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Profile))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Profile); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -192,6 +193,14 @@ func ToQuestion(it Item) (*Question, error) {
|
|||
return i, nil
|
||||
case Question:
|
||||
return &i, nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Question))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Question); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unable to convert to question activity")
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -201,6 +202,12 @@ func ToRelationship(it Item) (*Relationship, error) {
|
|||
return (*Relationship)(unsafe.Pointer(i)), nil
|
||||
case Object:
|
||||
return (*Relationship)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Relationship))
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Relationship); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package activitypub
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -191,6 +192,14 @@ func ToTombstone(it Item) (*Tombstone, error) {
|
|||
return (*Tombstone)(unsafe.Pointer(i)), nil
|
||||
case Object:
|
||||
return (*Tombstone)(unsafe.Pointer(&i)), nil
|
||||
default:
|
||||
// NOTE(marius): this is an ugly way of dealing with the interface conversion error: types from different scopes
|
||||
typ := reflect.TypeOf(new(Tombstone))
|
||||
if reflect.TypeOf(it).ConvertibleTo(typ) {
|
||||
if i, ok := reflect.ValueOf(it).Convert(typ).Interface().(*Tombstone); ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
||||
}
|
||||
|
|
Reference in a new issue