github.com/ovsinc/prototool@v1.3.0/internal/reflect/handler.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package reflect
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/golang/protobuf/protoc-gen-go/descriptor"
    27  	"github.com/jhump/protoreflect/desc"
    28  	"github.com/jhump/protoreflect/dynamic"
    29  	intdesc "github.com/uber/prototool/internal/desc"
    30  	"github.com/uber/prototool/internal/extract"
    31  	"go.uber.org/zap"
    32  )
    33  
    34  type handler struct {
    35  	logger *zap.Logger
    36  
    37  	getter extract.Getter
    38  }
    39  
    40  func newHandler(options ...HandlerOption) *handler {
    41  	handler := &handler{
    42  		logger: zap.NewNop(),
    43  	}
    44  	for _, option := range options {
    45  		option(handler)
    46  	}
    47  	// TODO(pedge): composition
    48  	handler.getter = extract.NewGetter(
    49  		extract.GetterWithLogger(handler.logger),
    50  	)
    51  	return handler
    52  }
    53  
    54  func (h *handler) BinaryToJSON(fileDescriptorSets []*descriptor.FileDescriptorSet, messagePath string, binaryData []byte) ([]byte, error) {
    55  	dynamicMessage, err := h.getDynamicMessage(fileDescriptorSets, messagePath)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	if err := dynamicMessage.Unmarshal(binaryData); err != nil {
    60  		return nil, err
    61  	}
    62  	return dynamicMessage.MarshalJSON()
    63  }
    64  
    65  func (h *handler) JSONToBinary(fileDescriptorSets []*descriptor.FileDescriptorSet, messagePath string, jsonData []byte) ([]byte, error) {
    66  	dynamicMessage, err := h.getDynamicMessage(fileDescriptorSets, messagePath)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	if err := dynamicMessage.UnmarshalJSON(jsonData); err != nil {
    71  		return nil, err
    72  	}
    73  	return dynamicMessage.Marshal()
    74  }
    75  
    76  func (h *handler) getDynamicMessage(fileDescriptorSets []*descriptor.FileDescriptorSet, messagePath string) (*dynamic.Message, error) {
    77  	message, err := h.getter.GetMessage(fileDescriptorSets, messagePath)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	fileDescriptorSet, err := intdesc.SortFileDescriptorSet(message.FileDescriptorSet, message.FileDescriptorProto)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	fileDescriptor, err := desc.CreateFileDescriptorFromSet(fileDescriptorSet)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	if len(message.FullyQualifiedPath) == 0 || message.FullyQualifiedPath[0] != '.' {
    90  		return nil, fmt.Errorf("malformed FullyQualifiedPath: %s", message.FullyQualifiedPath)
    91  	}
    92  	messageDescriptor := fileDescriptor.FindMessage(message.FullyQualifiedPath[1:])
    93  	if messageDescriptor == nil {
    94  		return nil, fmt.Errorf("no MessageDescriptor for path %s", message.FullyQualifiedPath)
    95  	}
    96  	return dynamic.NewMessage(messageDescriptor), nil
    97  }