@@ -123,28 +123,12 @@ Example of an OpenCDC record:
123
123
// openCDCUnwrapper unwraps an OpenCDC record from the payload, by unmarhsalling rec.Payload.After into type Record.
124
124
type openCDCUnwrapper struct {}
125
125
126
- // Unwrap replaces the whole record.payload with record.payload.after.payload except position.
127
- func (o * openCDCUnwrapper ) Unwrap (rec record.Record ) (record.Record , error ) {
128
- var structData record.StructuredData
129
- data := rec .Payload .After
130
- switch d := data .(type ) {
131
- case record.RawData :
132
- // unmarshal raw data to structured
133
- err := json .Unmarshal (data .Bytes (), & structData )
134
- if err != nil {
135
- return record.Record {}, cerrors .Errorf ("failed to unmarshal raw data as JSON: %w" , unwrapProcType , err )
136
- }
137
- case record.StructuredData :
138
- structData = d
139
- default :
140
- return record.Record {}, cerrors .Errorf ("unexpected data type %T" , unwrapProcType , data )
141
- }
142
-
143
- // get record.payload.after.operation
126
+ // UnwrapOperation extracts operation from a structuredData record.
127
+ func (o * openCDCUnwrapper ) UnwrapOperation (structData record.StructuredData ) (record.Operation , error ) {
144
128
var operation record.Operation
145
129
op , ok := structData ["operation" ]
146
130
if ! ok {
147
- return record. Record {} , cerrors .Errorf ("record payload after doesn't contain operation" )
131
+ return operation , cerrors .Errorf ("record payload after doesn't contain operation" )
148
132
}
149
133
150
134
switch o := op .(type ) {
@@ -153,15 +137,18 @@ func (o *openCDCUnwrapper) Unwrap(rec record.Record) (record.Record, error) {
153
137
case string :
154
138
err := operation .UnmarshalText ([]byte (o ))
155
139
if err != nil {
156
- return record. Record {} , cerrors .Errorf ("couldn't unmarshal record operation" )
140
+ return operation , cerrors .Errorf ("couldn't unmarshal record operation" )
157
141
}
158
142
}
143
+ return operation , nil
144
+ }
159
145
160
- // get record.payload.after.metadata
146
+ // UnwrapMetadata extracts metadata from a structuredData record.
147
+ func (o * openCDCUnwrapper ) UnwrapMetadata (structData record.StructuredData ) (record.Metadata , error ) {
161
148
var metadata record.Metadata
162
149
meta , ok := structData ["metadata" ]
163
150
if ! ok {
164
- return record. Record {} , cerrors .Errorf ("record payload after doesn't contain metadata" )
151
+ return metadata , cerrors .Errorf ("record payload after doesn't contain metadata" )
165
152
}
166
153
167
154
switch m := meta .(type ) {
@@ -173,12 +160,15 @@ func (o *openCDCUnwrapper) Unwrap(rec record.Record) (record.Record, error) {
173
160
metadata [k ] = fmt .Sprint (v )
174
161
}
175
162
}
163
+ return metadata , nil
164
+ }
176
165
177
- // get record.payload.after.key
166
+ // UnwrapKey extracts key from a structuredData record.
167
+ func (o * openCDCUnwrapper ) UnwrapKey (structData record.StructuredData ) (record.Data , error ) {
178
168
var key record.Data
179
169
ky , ok := structData ["key" ]
180
170
if ! ok {
181
- return record. Record {} , cerrors .Errorf ("record payload after doesn't contain key" )
171
+ return key , cerrors .Errorf ("record payload after doesn't contain key" )
182
172
}
183
173
184
174
switch k := ky .(type ) {
@@ -188,11 +178,15 @@ func (o *openCDCUnwrapper) Unwrap(rec record.Record) (record.Record, error) {
188
178
key = record.RawData {Raw : []byte (k )}
189
179
}
190
180
191
- // get record.payload.after.payload.after
181
+ return key , nil
182
+ }
183
+
184
+ // UnwrapPayload extracts payload from a structuredData record.
185
+ func (o * openCDCUnwrapper ) UnwrapPayload (structData record.StructuredData ) (record.Change , error ) {
192
186
var payload record.Change
193
187
pl , ok := structData ["payload" ]
194
188
if ! ok {
195
- return record. Record {} , cerrors .Errorf ("record payload doesn't contain payload" )
189
+ return payload , cerrors .Errorf ("record payload doesn't contain payload" )
196
190
}
197
191
198
192
switch p := pl .(type ) {
@@ -201,12 +195,12 @@ func (o *openCDCUnwrapper) Unwrap(rec record.Record) (record.Record, error) {
201
195
case map [string ]interface {}:
202
196
afterData , ok := p ["after" ]
203
197
if ! ok {
204
- return record. Record {} , cerrors .Errorf ("record payload after doesn't contain payload.after" )
198
+ return payload , cerrors .Errorf ("record payload after doesn't contain payload.after" )
205
199
}
206
200
207
201
data , ok := afterData .(map [string ]interface {})
208
202
if ! ok {
209
- return record. Record {} , cerrors .Errorf ("record payload after payload.after is not a map" )
203
+ return payload , cerrors .Errorf ("record payload after payload.after is not a map" )
210
204
}
211
205
212
206
convertedData := make (record.StructuredData , len (data ))
@@ -219,6 +213,45 @@ func (o *openCDCUnwrapper) Unwrap(rec record.Record) (record.Record, error) {
219
213
After : convertedData ,
220
214
}
221
215
}
216
+ return payload , nil
217
+ }
218
+
219
+ // Unwrap replaces the whole record.payload with record.payload.after.payload except position.
220
+ func (o * openCDCUnwrapper ) Unwrap (rec record.Record ) (record.Record , error ) {
221
+ var structData record.StructuredData
222
+ data := rec .Payload .After
223
+ switch d := data .(type ) {
224
+ case record.RawData :
225
+ // unmarshal raw data to structured
226
+ err := json .Unmarshal (data .Bytes (), & structData )
227
+ if err != nil {
228
+ return record.Record {}, cerrors .Errorf ("failed to unmarshal raw data as JSON: %w" , unwrapProcType , err )
229
+ }
230
+ case record.StructuredData :
231
+ structData = d
232
+ default :
233
+ return record.Record {}, cerrors .Errorf ("unexpected data type %T" , unwrapProcType , data )
234
+ }
235
+
236
+ operation , err := o .UnwrapOperation (structData )
237
+ if err != nil {
238
+ return record.Record {}, err
239
+ }
240
+
241
+ metadata , err := o .UnwrapMetadata (structData )
242
+ if err != nil {
243
+ return record.Record {}, err
244
+ }
245
+
246
+ key , err := o .UnwrapKey (structData )
247
+ if err != nil {
248
+ return record.Record {}, err
249
+ }
250
+
251
+ payload , err := o .UnwrapPayload (structData )
252
+ if err != nil {
253
+ return record.Record {}, err
254
+ }
222
255
223
256
return record.Record {
224
257
Key : key ,
0 commit comments