-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecoder.go
119 lines (96 loc) · 2.62 KB
/
decoder.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
112
113
114
115
116
117
118
119
// Fluent Bit Go!
// ==============
// Copyright (C) 2015-2017 Treasure Data Inc.
//
// 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 output
import "C"
import (
"encoding/binary"
"reflect"
"time"
"unsafe"
"github.com/ugorji/go/codec"
)
type FLBDecoder struct {
handle *codec.MsgpackHandle
mpdec *codec.Decoder
}
type FLBTime struct {
time.Time
}
func (f FLBTime) WriteExt(interface{}) []byte {
panic("unsupported")
}
func (f FLBTime) ReadExt(i interface{}, b []byte) {
out, _ := i.(*FLBTime)
sec := binary.BigEndian.Uint32(b)
usec := binary.BigEndian.Uint32(b[4:])
out.Time = time.Unix(int64(sec), int64(usec))
}
func (f FLBTime) ConvertExt(v interface{}) interface{} {
return nil
}
func (f FLBTime) UpdateExt(dest interface{}, v interface{}) {
panic("unsupported")
}
func NewDecoder(data unsafe.Pointer, length int) *FLBDecoder {
var b []byte
dec := new(FLBDecoder)
dec.handle = new(codec.MsgpackHandle)
// TODO: handle error.
_ = dec.handle.SetBytesExt(reflect.TypeOf(FLBTime{}), 0, &FLBTime{})
b = C.GoBytes(data, C.int(length))
dec.mpdec = codec.NewDecoderBytes(b, dec.handle)
return dec
}
func NewByteDecoder(b []byte) *FLBDecoder {
dec := new(FLBDecoder)
dec.handle = new(codec.MsgpackHandle)
// TODO: handle error.
_ = dec.handle.SetBytesExt(reflect.TypeOf(FLBTime{}), 0, &FLBTime{})
dec.mpdec = codec.NewDecoderBytes(b, dec.handle)
return dec
}
func GetRecord(dec *FLBDecoder) (ret int, ts interface{}, rec map[interface{}]interface{}) {
var check error
var m interface{}
check = dec.mpdec.Decode(&m)
if check != nil {
return -1, 0, nil
}
slice := reflect.ValueOf(m)
if slice.Kind() != reflect.Slice || slice.Len() != 2 {
return -2, 0, nil
}
var t interface{}
ts = slice.Index(0).Interface()
switch ty := ts.(type) {
case FLBTime:
t = ty
case uint64:
t = ty
case []interface{}: // for Fluent Bit V2 metadata type of format
s := reflect.ValueOf(ty)
if s.Kind() != reflect.Slice || s.Len() < 2 {
return -4, 0, nil
}
t = s.Index(0).Interface()
default:
return -5, 0, nil
}
data := slice.Index(1)
md, _ := data.Interface().(map[any]any)
return 0, t, md
}