Skip to content

Commit

Permalink
expose json marshal options context func
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed Feb 28, 2024
1 parent 44c4567 commit d327c8f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 2 additions & 2 deletions opencdc/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (d RawData) Clone() Data {

func (d RawData) MarshalJSON(ctx context.Context) ([]byte, error) {
if ctx != nil {
s := ctx.Value(jsonSerializerCtxKey{})
if s != nil && s.(*JSONSerializer).RawDataAsString {
s := ctx.Value(jsonMarshalOptionsCtxKey{})
if s != nil && s.(*JSONMarshalOptions).RawDataAsString {

Check failure on line 91 in opencdc/data.go

View workflow job for this annotation

GitHub Actions / golangci-lint

type assertion must be checked (forcetypeassert)
// We should serialize RawData as a string.
return json.Marshal(string(d))

Check failure on line 93 in opencdc/data.go

View workflow job for this annotation

GitHub Actions / golangci-lint

error returned from external package is unwrapped: sig: func github.com/goccy/go-json.Marshal(v interface{}) ([]byte, error) (wrapcheck)
}
Expand Down
19 changes: 19 additions & 0 deletions opencdc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,30 @@
package opencdc

import (
"context"
"fmt"

"github.com/goccy/go-json"
)

// JSONMarshalOptions can customize how a record is serialized to JSON. It can
// be attached to a context using WithJSONMarshalOptions and supplied to
// json.MarshalContext to customize the serialization behavior.
type JSONMarshalOptions struct {
// RawDataAsString is a flag that indicates if the RawData type should be
// serialized as a string. If set to false, RawData will be serialized as a
// base64 encoded string. If set to true, RawData will be serialized as a
// string without conversion.
RawDataAsString bool
}

type jsonMarshalOptionsCtxKey struct{}

// WithJSONMarshalOptions attaches JSONMarshalOptions to a context.
func WithJSONMarshalOptions(ctx context.Context, options *JSONMarshalOptions) context.Context {
return context.WithValue(ctx, jsonMarshalOptionsCtxKey{}, options)
}

func (r *Record) UnmarshalJSON(b []byte) error {
var raw struct {
Position Position `json:"position"`
Expand Down
15 changes: 4 additions & 11 deletions opencdc/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,12 @@ type RecordSerializer interface {
Serialize(Record) ([]byte, error)
}

// JSONSerializer is a RecordSerializer that serializes records to JSON.
type JSONSerializer struct {
// RawDataAsString is a flag that indicates if the RawData type should be
// serialized as a string. If set to false, RawData will be serialized as a
// base64 encoded string. If set to true, RawData will be serialized as a
// string without conversion.
RawDataAsString bool
}

type jsonSerializerCtxKey struct{}
// JSONSerializer is a RecordSerializer that serializes records to JSON using
// the configured options.
type JSONSerializer JSONMarshalOptions

func (s JSONSerializer) Serialize(r Record) ([]byte, error) {
ctx := context.WithValue(context.Background(), jsonSerializerCtxKey{}, &s)
ctx := WithJSONMarshalOptions(context.Background(), (*JSONMarshalOptions)(&s))
defer func() {
// Workaround because of https://github.com/goccy/go-json/issues/499.
// TODO: Remove this when the issue is fixed and store value in context
Expand Down

0 comments on commit d327c8f

Please sign in to comment.