github.com/nikron/prototool@v1.3.0/internal/extract/extract.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 extract is used to extract elements from FileDescriptorSets created
    22  // from internal/protoc, for use in json-to-binary and back conversion, and for
    23  // use for gRPC.
    24  package extract
    25  
    26  import (
    27  	"github.com/golang/protobuf/protoc-gen-go/descriptor"
    28  	"go.uber.org/zap"
    29  )
    30  
    31  // Field is an extracted field.
    32  type Field struct {
    33  	*descriptor.FieldDescriptorProto
    34  
    35  	FullyQualifiedPath  string
    36  	DescriptorProto     *descriptor.DescriptorProto
    37  	FileDescriptorProto *descriptor.FileDescriptorProto
    38  	FileDescriptorSet   *descriptor.FileDescriptorSet
    39  }
    40  
    41  // Message is an extracted message.
    42  type Message struct {
    43  	*descriptor.DescriptorProto
    44  
    45  	FullyQualifiedPath  string
    46  	FileDescriptorProto *descriptor.FileDescriptorProto
    47  	FileDescriptorSet   *descriptor.FileDescriptorSet
    48  }
    49  
    50  // Service is an extracted service.
    51  type Service struct {
    52  	*descriptor.ServiceDescriptorProto
    53  
    54  	FullyQualifiedPath  string
    55  	FileDescriptorProto *descriptor.FileDescriptorProto
    56  	FileDescriptorSet   *descriptor.FileDescriptorSet
    57  }
    58  
    59  // Getter extracts elements.
    60  //
    61  // Paths can begin with ".".
    62  // The first FileDescriptorSet with a match will be returned.
    63  type Getter interface {
    64  	// Get the field that matches the path.
    65  	// Return non-nil value, or error otherwise including if nothing found.
    66  	GetField(fileDescriptorSets []*descriptor.FileDescriptorSet, path string) (*Field, error)
    67  	// Get the message that matches the path.
    68  	// Return non-nil value, or error otherwise including if nothing found.
    69  	GetMessage(fileDescriptorSets []*descriptor.FileDescriptorSet, path string) (*Message, error)
    70  	// Get the service that matches the path.
    71  	// Return non-nil value, or error otherwise including if nothing found.
    72  	GetService(fileDescriptorSets []*descriptor.FileDescriptorSet, path string) (*Service, error)
    73  }
    74  
    75  // GetterOption is an option for a new Getter.
    76  type GetterOption func(*getter)
    77  
    78  // GetterWithLogger returns a GetterOption that uses the given logger.
    79  //
    80  // The default is to use zap.NewNop().
    81  func GetterWithLogger(logger *zap.Logger) GetterOption {
    82  	return func(getter *getter) {
    83  		getter.logger = logger
    84  	}
    85  }
    86  
    87  // NewGetter returns a new Getter.
    88  func NewGetter(options ...GetterOption) Getter {
    89  	return newGetter(options...)
    90  }