/
decode.go
102 lines (88 loc) · 2.73 KB
/
decode.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 exec
import (
"fmt"
"reflect"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
)
// Decoder is a uniform custom decoder interface. It wraps various
// forms of reflectx.Funcs.
type Decoder interface {
// Decode decodes the []byte in to a value of the given type.
Decode(reflect.Type, []byte) (interface{}, error)
}
func makeDecoder(fn reflectx.Func) Decoder {
// Detect one of the valid decoder forms and allow it to be invoked
// efficiently, relying on the general reflectx.Func framework for
// avoiding expensive reflection calls. There are 4 forms:
//
// []byte -> T
// []byte -> (T, error)
// (Type, []byte) -> T
// (Type, []byte) -> (T, error)
//
// We simply enumerate the forms. The wrappers simply ignore the
// appropriate parts.
switch fn.Type().NumIn() {
case 1:
switch fn.Type().NumOut() {
case 1:
return &decoder1x1{fn: reflectx.ToFunc1x1(fn)}
case 2:
return &decoder1x2{fn: reflectx.ToFunc1x2(fn)}
}
case 2:
switch fn.Type().NumOut() {
case 1:
return &decoder2x1{fn: reflectx.ToFunc2x1(fn)}
case 2:
return &decoder2x2{fn: reflectx.ToFunc2x2(fn)}
}
}
panic(fmt.Sprintf("Invalid decoder: %v", fn))
}
type decoder1x1 struct {
fn reflectx.Func1x1
}
func (d *decoder1x1) Decode(t reflect.Type, data []byte) (interface{}, error) {
return d.fn.Call1x1(data), nil
}
type decoder1x2 struct {
fn reflectx.Func1x2
}
func (d *decoder1x2) Decode(t reflect.Type, data []byte) (interface{}, error) {
val, err := d.fn.Call1x2(data)
if err != nil {
return nil, err.(error)
}
return val, nil
}
type decoder2x1 struct {
fn reflectx.Func2x1
}
func (d *decoder2x1) Decode(t reflect.Type, data []byte) (interface{}, error) {
return d.fn.Call2x1(t, data), nil
}
type decoder2x2 struct {
fn reflectx.Func2x2
}
func (d *decoder2x2) Decode(t reflect.Type, data []byte) (interface{}, error) {
val, err := d.fn.Call2x2(t, data)
if err != nil {
return nil, err.(error)
}
return val, nil
}