-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package message | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
|
||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/reflect/protoregistry" | ||
) | ||
|
||
// FromJSON creates a dynamic Proto messages from JSON input. | ||
func FromJSON(verbose bool, registry *protoregistry.Files, protoPackage string, msgName protoreflect.Name, jsonReader io.Reader) (proto.Message, error) { | ||
msg, err := MakeDynamicMessage(verbose, registry, protoPackage, msgName) | ||
if err != nil { | ||
return nil, fmt.Errorf("error MakeDynamicMessage(): %w", err) | ||
} | ||
inB, err := ioutil.ReadAll(jsonReader) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := protojson.Unmarshal(inB, msg); err != nil { | ||
return nil, fmt.Errorf("error protojson.Unmarshal(): %w", err) | ||
} | ||
return msg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package message | ||
|
||
import ( | ||
"fmt" | ||
|
||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/reflect/protoregistry" | ||
"google.golang.org/protobuf/types/dynamicpb" | ||
) | ||
|
||
// MakeDynamicMessage creates an unhydrated proto message using the registry information | ||
func MakeDynamicMessage(verbose bool, registry *protoregistry.Files, protoPackage string, msgName protoreflect.Name) (proto.Message, error) { | ||
var req protoreflect.MessageDescriptor | ||
registry.RangeFilesByPackage(protoreflect.FullName(protoPackage), func(fd protoreflect.FileDescriptor) bool { | ||
req = fd.Messages().ByName(msgName) | ||
if verbose { | ||
fmt.Println("package file:", fd.Name()) | ||
} | ||
return req == nil | ||
}) | ||
if req == nil { | ||
panic("no message found!") | ||
} | ||
msg := dynamicpb.NewMessage(req) | ||
return msg, nil | ||
} | ||
|
||
func UnmarshalProto(verbose bool, registry *protoregistry.Files, protoPackage string, msgName protoreflect.Name, in []byte) []byte { | ||
msg, err := MakeDynamicMessage(verbose, registry, protoPackage, msgName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = proto.Unmarshal(in, msg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
jsonBytes, err := protojson.Marshal(msg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return jsonBytes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters