mirror of
https://github.com/rqlite/rqlite.git
synced 2026-01-25 04:16:26 +00:00
CDC PB JSON
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rqlite/rqlite/v8/command/proto"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/reflect/protoregistry"
|
||||
)
|
||||
|
||||
// CDCMessagesEnvelope is the envelope for CDC messages as transported over HTTP.
|
||||
@@ -72,3 +74,22 @@ func MarshalToEnvelopeJSON(serviceID, nodeID string, ts bool, evs []*proto.CDCIn
|
||||
func UnmarshalFromEnvelopeJSON(data []byte, env *CDCMessagesEnvelope) error {
|
||||
return json.Unmarshal(data, env)
|
||||
}
|
||||
|
||||
var pj = protojson.MarshalOptions{
|
||||
// Control JSON shape:
|
||||
// - UseProtoNames: snake_case field names from .proto instead of lowerCamel JSON names
|
||||
// - EmitUnpopulated: include fields with zero values
|
||||
// - UseEnumNumbers: render enums as integers (false => strings)
|
||||
UseProtoNames: true,
|
||||
EmitUnpopulated: false,
|
||||
UseEnumNumbers: false,
|
||||
|
||||
// Resolve google.protobuf.Any to type URLs if you use Any:
|
||||
Resolver: protoregistry.GlobalTypes,
|
||||
}
|
||||
|
||||
func WriteJSON(m *proto.CDCIndexedEventGroup) ([]byte, error) {
|
||||
// Marshal directly to []byte; write it immediately.
|
||||
// Avoid string conversions (which copy) and avoid extra buffers.
|
||||
return pj.Marshal(m)
|
||||
}
|
||||
|
||||
28
cdc/json/marshal_test.go
Normal file
28
cdc/json/marshal_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/rqlite/rqlite/v8/command/proto"
|
||||
)
|
||||
|
||||
func Test_Single(t *testing.T) {
|
||||
e := &proto.CDCIndexedEventGroup{
|
||||
Index: 1,
|
||||
CommitTimestamp: 123456789,
|
||||
Events: []*proto.CDCEvent{
|
||||
{
|
||||
Op: proto.CDCEvent_INSERT,
|
||||
Table: "users",
|
||||
NewRowId: 42,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
b, err := WriteJSON(e)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %s", err)
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
Reference in New Issue
Block a user