Skip to content

Commit

Permalink
fix: type
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Dec 3, 2022
1 parent bb8ecc9 commit 05442d7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
8 changes: 8 additions & 0 deletions pkg/typex/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package typex

import "reflect"

func TypeOf[T any]() reflect.Type {
var t T
return reflect.TypeOf(t)
}
34 changes: 29 additions & 5 deletions pkg/typex/multitype.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@ import "reflect"
type MultiType2[T1, T2 any] struct {
obj any
t reflect.Type
t1 reflect.Type
t2 reflect.Type
}

func NewMultiType2WithT1[T1, T2 any](obj T1) *MultiType2[T1, T2] {
return &MultiType2[T1, T2]{
obj: obj,
t: reflect.TypeOf(obj),
t1: TypeOf[T1](),
t2: TypeOf[T2](),
}
}

func NewMultiType2WithT2[T1, T2 any](obj T2) *MultiType2[T1, T2] {
return &MultiType2[T1, T2]{
obj: obj,
t: reflect.TypeOf(obj),
t1: TypeOf[T1](),
t2: TypeOf[T2](),
}
}

func (m *MultiType2[T1, T2]) IsT1() bool {
return m.t == reflect.TypeOf(m.obj)
return m.t == m.t1
}

func (m *MultiType2[T1, T2]) IsT2() bool {
return m.t == reflect.TypeOf(m.obj)
return m.t == m.t2
}

func (m *MultiType2[T1, T2]) GetT1() T1 {
Expand All @@ -39,10 +45,12 @@ func (m *MultiType2[T1, T2]) GetT2() T2 {

func (m *MultiType2[T1, T2]) SetT1(t T1) {
m.obj = t
m.t = m.t1
}

func (m *MultiType2[T1, T2]) SetT2(t T2) {
m.obj = t
m.t = m.t2
}

func (m *MultiType2[T1, T2]) Get() any {
Expand All @@ -52,39 +60,51 @@ func (m *MultiType2[T1, T2]) Get() any {
type MultiType3[T1, T2, T3 any] struct {
obj any
t reflect.Type
t1 reflect.Type
t2 reflect.Type
t3 reflect.Type
}

func NewMultiType3WithT1[T1, T2, T3 any](obj T1) *MultiType3[T1, T2, T3] {
return &MultiType3[T1, T2, T3]{
obj: obj,
t: reflect.TypeOf(obj),
t1: TypeOf[T1](),
t2: TypeOf[T2](),
t3: TypeOf[T3](),
}
}

func NewMultiType3WithT2[T1, T2, T3 any](obj T2) *MultiType3[T1, T2, T3] {
return &MultiType3[T1, T2, T3]{
obj: obj,
t: reflect.TypeOf(obj),
t1: TypeOf[T1](),
t2: TypeOf[T2](),
t3: TypeOf[T3](),
}
}

func NewMultiType3WithT3[T1, T2, T3 any](obj T3) *MultiType3[T1, T2, T3] {
return &MultiType3[T1, T2, T3]{
obj: obj,
t: reflect.TypeOf(obj),
t1: TypeOf[T1](),
t2: TypeOf[T2](),
t3: TypeOf[T3](),
}
}

func (m *MultiType3[T1, T2, T3]) IsT1() bool {
return m.t == reflect.TypeOf(m.obj)
return m.t == m.t1
}

func (m *MultiType3[T1, T2, T3]) IsT2() bool {
return m.t == reflect.TypeOf(m.obj)
return m.t == m.t2
}

func (m *MultiType3[T1, T2, T3]) IsT3() bool {
return m.t == reflect.TypeOf(m.obj)
return m.t == m.t3
}

func (m *MultiType3[T1, T2, T3]) GetT1() T1 {
Expand All @@ -101,14 +121,18 @@ func (m *MultiType3[T1, T2, T3]) GetT3() T3 {

func (m *MultiType3[T1, T2, T3]) SetT1(t T1) {
m.obj = t
m.t = m.t1
}

func (m *MultiType3[T1, T2, T3]) SetT2(t T2) {
m.obj = t
m.t = m.t2

}

func (m *MultiType3[T1, T2, T3]) SetT3(t T3) {
m.obj = t
m.t = m.t3
}

func (m *MultiType3[T1, T2, T3]) Get() any {
Expand Down
30 changes: 30 additions & 0 deletions pkg/typex/multitype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package typex

import (
"testing"
)

func TestMultiType(t *testing.T) {
m := NewMultiType2WithT1[double, string](1)
if !m.IsT1() {
t.Errorf("IsT1 failed")
t.Fail()
}
if m.IsT2() {
t.Errorf("IsT2 failed")
t.Fail()
}
m.SetT2("hello")
if !m.IsT2() {
t.Errorf("IsT2 failed")
t.Fail()
}
if m.IsT1() {
t.Errorf("IsT1 failed")
t.Fail()
}
if m.GetT2() != "hello" {
t.Errorf("GetT2 failed")
t.Fail()
}
}
1 change: 1 addition & 0 deletions pkg/typex/num.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ type u32 uint32
type u64 uint64
type f32 float32
type f64 float64
type double float64

0 comments on commit 05442d7

Please sign in to comment.