Skip to content

Commit

Permalink
refactor into separate concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
aakarim committed Apr 6, 2021
1 parent c25b1bc commit 0a504b3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 60 deletions.
7 changes: 6 additions & 1 deletion cmd/pout/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"

"github.com/lix-it/pout/internal/proto/message"
"github.com/lix-it/pout/internal/proto/registry"
"github.com/lix-it/pout/pkg/pout"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -48,8 +49,12 @@ func main() {
}
}
defer r.Close()
regy, err := registry.BuildProtoRegistry(config.Verbose, *protoPath)
if err != nil {
panic(fmt.Errorf("error BuildProtoRegistry(): %w", err))
}

msg, err := registry.FromJSON(config.Verbose, *protoPath, protoPackage, protoreflect.Name(msgName), r)
msg, err := message.FromJSON(config.Verbose, regy, protoPackage, protoreflect.Name(msgName), r)
if err != nil {
wrapErr := fmt.Errorf("error converting JSON to proto: %w; check whether the message paths and types are correct", err)
panic(wrapErr)
Expand Down
29 changes: 29 additions & 0 deletions internal/proto/message/json.go
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
}
44 changes: 44 additions & 0 deletions internal/proto/message/message.go
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
}
59 changes: 0 additions & 59 deletions internal/proto/registry/registry.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package registry

import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
Expand All @@ -11,13 +9,10 @@ import (
"path/filepath"
"strings"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
)

// BuildProtoRegistry walks through every file in the proto folder
Expand Down Expand Up @@ -78,57 +73,3 @@ func BuildProtoRegistry(verbose bool, srcDir string) (*protoregistry.Files, erro

return files, nil
}

func UnmarshalProto(verbose bool, protoPath, protoPackage string, msgName protoreflect.Name, in []byte) []byte {
msg, err := MakeDynamicMessage(verbose, protoPath, 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
}

// MakeDynamicMessage creates an unhydrated proto message using the registry information
func MakeDynamicMessage(verbose bool, protoPath, protoPackage string, msgName protoreflect.Name) (proto.Message, error) {
registry, err := BuildProtoRegistry(verbose, protoPath)
if err != nil {
return nil, fmt.Errorf("error BuildProtoRegistry(): %w", err)
}
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
}

// FromJSON creates a dynamic Proto messages from JSON input.
func FromJSON(verbose bool, protoPath, protoFile string, msgName protoreflect.Name, jsonReader io.Reader) (proto.Message, error) {
msg, err := MakeDynamicMessage(verbose, protoPath, protoFile, 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
}

0 comments on commit 0a504b3

Please sign in to comment.