forked from leanovate/gopter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bi_mapper.go
111 lines (101 loc) · 3.15 KB
/
bi_mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package gopter
import (
"fmt"
"reflect"
)
// BiMapper is a bi-directional (or bijective) mapper of a tuple of values (up)
// to another tuple of values (down).
type BiMapper struct {
UpTypes []reflect.Type
DownTypes []reflect.Type
Downstream reflect.Value
Upstream reflect.Value
}
// NewBiMapper creates a BiMapper of two functions `downstream` and its
// inverse `upstream`.
// That is: The return values of `downstream` must match the parameters of
// `upstream` and vice versa.
func NewBiMapper(downstream interface{}, upstream interface{}) *BiMapper {
downstreamVal := reflect.ValueOf(downstream)
if downstreamVal.Kind() != reflect.Func {
panic("downstream has to be a function")
}
upstreamVal := reflect.ValueOf(upstream)
if upstreamVal.Kind() != reflect.Func {
panic("upstream has to be a function")
}
downstreamType := downstreamVal.Type()
upTypes := make([]reflect.Type, downstreamType.NumIn())
for i := 0; i < len(upTypes); i++ {
upTypes[i] = downstreamType.In(i)
}
downTypes := make([]reflect.Type, downstreamType.NumOut())
for i := 0; i < len(downTypes); i++ {
downTypes[i] = downstreamType.Out(i)
}
upstreamType := upstreamVal.Type()
if len(upTypes) != upstreamType.NumOut() {
panic(fmt.Sprintf("upstream is expected to have %d return values", len(upTypes)))
}
for i, upType := range upTypes {
if upstreamType.Out(i) != upType {
panic(fmt.Sprintf("upstream has wrong return type %d: %v != %v", i, upstreamType.Out(i), upType))
}
}
if len(downTypes) != upstreamType.NumIn() {
panic(fmt.Sprintf("upstream is expected to have %d parameters", len(downTypes)))
}
for i, downType := range downTypes {
if upstreamType.In(i) != downType {
panic(fmt.Sprintf("upstream has wrong parameter type %d: %v != %v", i, upstreamType.In(i), downType))
}
}
return &BiMapper{
UpTypes: upTypes,
DownTypes: downTypes,
Downstream: downstreamVal,
Upstream: upstreamVal,
}
}
// ConvertUp calls the Upstream function on the arguments in the down array
// and returns the results.
func (b *BiMapper) ConvertUp(down []interface{}) []interface{} {
if len(down) != len(b.DownTypes) {
panic(fmt.Sprintf("Expected %d values != %d", len(b.DownTypes), len(down)))
}
downVals := make([]reflect.Value, len(b.DownTypes))
for i, val := range down {
if val == nil {
downVals[i] = reflect.Zero(b.DownTypes[i])
} else {
downVals[i] = reflect.ValueOf(val)
}
}
upVals := b.Upstream.Call(downVals)
up := make([]interface{}, len(upVals))
for i, upVal := range upVals {
up[i] = upVal.Interface()
}
return up
}
// ConvertDown calls the Downstream function on the elements of the up array
// and returns the results.
func (b *BiMapper) ConvertDown(up []interface{}) []interface{} {
if len(up) != len(b.UpTypes) {
panic(fmt.Sprintf("Expected %d values != %d", len(b.UpTypes), len(up)))
}
upVals := make([]reflect.Value, len(b.UpTypes))
for i, val := range up {
if val == nil {
upVals[i] = reflect.Zero(b.UpTypes[i])
} else {
upVals[i] = reflect.ValueOf(val)
}
}
downVals := b.Downstream.Call(upVals)
down := make([]interface{}, len(downVals))
for i, downVal := range downVals {
down[i] = downVal.Interface()
}
return down
}