github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/protoc-gen-toolkit/descriptor/types.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package descriptor
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"google.golang.org/protobuf/types/descriptorpb"
    24  	"google.golang.org/protobuf/types/pluginpb"
    25  )
    26  
    27  type GoPackage struct {
    28  	Path  string
    29  	Name  string
    30  	Alias string
    31  }
    32  
    33  func (p GoPackage) Standard() bool {
    34  	return !strings.Contains(p.Path, ".")
    35  }
    36  
    37  func (p GoPackage) String() string {
    38  	if p.Alias == "" {
    39  		return fmt.Sprintf("%q", p.Path)
    40  	}
    41  	return fmt.Sprintf("%s %q", p.Alias, p.Path)
    42  }
    43  
    44  type File struct {
    45  	*descriptorpb.FileDescriptorProto
    46  	GeneratedFilenamePrefix string
    47  	GoPkg                   GoPackage
    48  	Messages                []*Message
    49  	Enums                   []*Enum
    50  	Services                []*Service
    51  }
    52  
    53  func (f *File) Pkg() string {
    54  	pkg := f.GoPkg.Name
    55  	if alias := f.GoPkg.Alias; alias != "" {
    56  		pkg = alias
    57  	}
    58  	return pkg
    59  }
    60  
    61  type Message struct {
    62  	*descriptorpb.DescriptorProto
    63  	File   *File
    64  	Fields []*Field
    65  	Outers []string
    66  	Index  int
    67  }
    68  
    69  func (m *Message) FullyName() string {
    70  	parts := []string{""}
    71  	if m.File.Package != nil {
    72  		parts = append(parts, m.File.GetPackage())
    73  	}
    74  	parts = append(parts, m.Outers...)
    75  	parts = append(parts, m.GetName())
    76  	return strings.Join(parts, ".")
    77  }
    78  
    79  func (m *Message) GoType(currentPackage string) string {
    80  	var parts []string
    81  	parts = append(parts, m.Outers...)
    82  	parts = append(parts, m.GetName())
    83  
    84  	name := strings.Join(parts, "_")
    85  	if m.File.GoPkg.Path == currentPackage {
    86  		return name
    87  	}
    88  	return fmt.Sprintf("%s.%s", m.File.Pkg(), name)
    89  }
    90  
    91  func (m *Message) GoName() string {
    92  	var parts []string
    93  	parts = append(parts, m.Outers...)
    94  	parts = append(parts, m.GetName())
    95  	return strings.Join(parts, "_")
    96  }
    97  
    98  type Field struct {
    99  	*descriptorpb.FieldDescriptorProto
   100  	Message      *Message
   101  	FieldMessage *Message
   102  }
   103  
   104  type Enum struct {
   105  	*descriptorpb.EnumDescriptorProto
   106  	File   *File
   107  	Outers []string
   108  	Index  int
   109  }
   110  
   111  func (e *Enum) FullyName() string {
   112  	parts := []string{""}
   113  	if e.File.Package != nil {
   114  		parts = append(parts, e.File.GetPackage())
   115  	}
   116  	parts = append(parts, e.Outers...)
   117  	parts = append(parts, e.GetName())
   118  	return strings.Join(parts, ".")
   119  }
   120  
   121  type Plugin struct {
   122  	Prefix   string
   123  	Plugin   string
   124  	Pkg      string
   125  	IsGlobal bool
   126  }
   127  
   128  type Service struct {
   129  	*descriptorpb.ServiceDescriptorProto
   130  	File                    *File
   131  	Methods                 []*Method
   132  	Plugins                 map[string][]*Plugin
   133  	HTTPMiddlewares         []string
   134  	UseGRPCServer           bool
   135  	UseHTTPServer           bool
   136  	UseWebsocketProxyServer bool
   137  	UseWebsocketServer      bool
   138  }
   139  
   140  func (s *Service) FullyName() string {
   141  	var parts []string
   142  	if s.File.Package != nil {
   143  		parts = append(parts, s.File.GetPackage())
   144  	}
   145  	parts = append(parts, s.GetName())
   146  	return strings.Join(parts, ".")
   147  }
   148  
   149  type Method struct {
   150  	*descriptorpb.MethodDescriptorProto
   151  	Service          *Service
   152  	RequestType      *Message
   153  	ResponseType     *Message
   154  	Name             string
   155  	IsWebsocket      bool
   156  	IsWebsocketProxy bool
   157  	Bindings         []*Binding
   158  }
   159  
   160  func (m *Method) FullyName() string {
   161  	var parts []string
   162  	parts = append(parts, m.Service.FullyName())
   163  	parts = append(parts, m.GetName())
   164  	return strings.Join(parts, ".")
   165  }
   166  
   167  type ResponseFile struct {
   168  	*pluginpb.CodeGeneratorResponse_File
   169  	GoPkg   GoPackage
   170  	Rewrite bool
   171  }
   172  
   173  type Binding struct {
   174  	Method                   *Method
   175  	Index                    int
   176  	RpcMethod                string
   177  	RpcPath                  string
   178  	Service                  string
   179  	HttpMethod               string
   180  	HttpPath                 string
   181  	RawBody                  string
   182  	HttpParams               []string
   183  	Middlewares              []string
   184  	ExcludeGlobalMiddlewares []string
   185  	RequestType              *Message
   186  	ResponseType             *Message
   187  	Stream                   bool
   188  	Websocket                bool
   189  	WebsocketProxy           bool
   190  	AdditionalBinding        bool
   191  }