MCPcopy
hub / github.com/keploy/keploy / ProtoWireToJSON

Function ProtoWireToJSON

utils/grpc.go:351–374  ·  view source on GitHub ↗

ProtoWireToJSON takes a MessageDescriptor, compiled files, and a wire-format []byte, and returns the JSON encoding ([]byte). The files parameter is crucial for resolving google.protobuf.Any types which require access to all message types in the compiled schema.

(md protoreflect.MessageDescriptor, files []protoreflect.FileDescriptor, wire []byte)

Source from the content-addressed store, hash-verified

349// and returns the JSON encoding ([]byte). The files parameter is crucial for resolving
350// google.protobuf.Any types which require access to all message types in the compiled schema.
351func ProtoWireToJSON(md protoreflect.MessageDescriptor, files []protoreflect.FileDescriptor, wire []byte) ([]byte, error) {
352 // Create type resolver for Any type resolution - this fixes the error:
353 // "proto: google.protobuf.Any: unable to resolve \"type.googleapis.com/fuzz.Inner\": not found"
354 typeResolver := createTypeResolver(files)
355
356 // Unmarshal into dynamic message
357 msg := dynamicpb.NewMessage(md)
358 if err := proto.Unmarshal(wire, msg); err != nil {
359 return nil, err
360 }
361
362 // Marshal to JSON with custom type resolver
363 actRespJson, err := protojson.MarshalOptions{
364 Indent: " ",
365 EmitUnpopulated: true, // include false/0/""/empty fields
366 UseProtoNames: true, // snake_case field names
367 Resolver: typeResolver, // Custom resolver for Any types
368 }.Marshal(msg)
369 if err != nil {
370 return nil, err
371 }
372
373 return actRespJson, nil
374}
375
376// ProtoTextToJSON converts a Protoscope text payload to JSON via:
377//

Callers 1

ProtoTextToJSONFunction · 0.85

Calls 3

createTypeResolverFunction · 0.85
UnmarshalMethod · 0.80
MarshalMethod · 0.80

Tested by

no test coverage detected