/
record.go
177 lines (152 loc) · 5.49 KB
/
record.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright © 2022 Meroxa, 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.
//go:generate stringer -type=Operation -linecomment
package sdk
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/conduitio/conduit-connector-protocol/cpluginv1"
)
const (
OperationCreate Operation = iota + 1 // create
OperationUpdate // update
OperationDelete // delete
OperationSnapshot // snapshot
)
// Operation defines what triggered the creation of a record.
type Operation int
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
var cTypes [1]struct{}
_ = cTypes[int(OperationCreate)-int(cpluginv1.OperationCreate)]
_ = cTypes[int(OperationUpdate)-int(cpluginv1.OperationUpdate)]
_ = cTypes[int(OperationDelete)-int(cpluginv1.OperationDelete)]
_ = cTypes[int(OperationSnapshot)-int(cpluginv1.OperationSnapshot)]
}
func (i Operation) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
}
func (i *Operation) UnmarshalText(b []byte) error {
if len(b) == 0 {
return nil // empty string, do nothing
}
switch string(b) {
case OperationCreate.String():
*i = OperationCreate
case OperationUpdate.String():
*i = OperationUpdate
case OperationDelete.String():
*i = OperationDelete
case OperationSnapshot.String():
*i = OperationSnapshot
default:
// it's not a known operation, but we also allow Operation(int)
valIntRaw := strings.TrimSuffix(strings.TrimPrefix(string(b), "Operation("), ")")
valInt, err := strconv.Atoi(valIntRaw)
if err != nil {
return fmt.Errorf("unknown operation %q", b)
}
*i = Operation(valInt)
}
return nil
}
// Record represents a single data record produced by a source and/or consumed
// by a destination connector.
type Record struct {
// Position uniquely represents the record.
Position Position `json:"position"`
// Operation defines what triggered the creation of a record. There are four
// possibilities: create, update, delete or snapshot. The first three
// operations are encountered during normal CDC operation, while "snapshot"
// is meant to represent records during an initial load. Depending on the
// operation, the record will contain either the payload before the change,
// after the change, or both (see field Payload).
Operation Operation `json:"operation"`
// Metadata contains additional information regarding the record.
Metadata Metadata `json:"metadata"`
// Key represents a value that should identify the entity (e.g. database
// row).
Key Data `json:"key"`
// Payload holds the payload change (data before and after the operation
// occurred).
Payload Change `json:"payload"`
formatter RecordFormatter
}
type Metadata map[string]string
// Bytes returns the JSON encoding of the Record.
func (r Record) Bytes() []byte {
var b []byte
var err error
if r.formatter == nil {
b, err = defaultFormatter.Format(r)
} else {
b, err = r.formatter.Format(r)
}
if err != nil {
err = fmt.Errorf("error while formatting Record: %w", err)
Logger(context.Background()).Err(err).Msg("falling back to JSON format")
b, err = json.Marshal(r)
if err != nil {
// Unlikely to happen, we receive content from a plugin through GRPC.
// If the content could be marshaled as protobuf it can be as JSON.
panic(fmt.Errorf("error while marshaling Record as JSON: %w", err))
}
}
return b
}
type Change struct {
// Before contains the data before the operation occurred. This field is
// optional and should only be populated for operations OperationUpdate
// OperationDelete (if the system supports fetching the data before the
// operation).
Before Data `json:"before"`
// After contains the data after the operation occurred. This field should
// be populated for all operations except OperationDelete.
After Data `json:"after"`
}
// Position is a unique identifier for a record. It is the responsibility of the
// Source to choose and assign record positions, it can freely choose a format
// that makes sense and contains everything needed to restart a pipeline at a
// certain position.
type Position []byte
// Data is a structure that contains some bytes. The only structs implementing
// Data are RawData and StructuredData.
type Data interface {
isData()
Bytes() []byte
}
// RawData contains unstructured data in form of a byte slice.
type RawData []byte
func (RawData) isData() {}
// Bytes simply casts RawData to a byte slice.
func (d RawData) Bytes() []byte {
return d
}
// StructuredData contains data in form of a map with string keys and arbitrary
// values.
type StructuredData map[string]interface{}
func (StructuredData) isData() {}
// Bytes returns the JSON encoding of the map.
func (d StructuredData) Bytes() []byte {
b, err := json.Marshal(d)
if err != nil {
// Unlikely to happen, we receive content from a plugin through GRPC.
// If the content could be marshaled as protobuf it can be as JSON.
panic(fmt.Errorf("error while marshaling StructuredData as JSON: %w", err))
}
return b
}