github.com/cloudwego/kitex@v0.9.0/pkg/generic/descriptor/descriptor.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo 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 the idl descriptor for describe the idls with golang
    18  package descriptor
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  
    24  	dthrift "github.com/cloudwego/dynamicgo/thrift"
    25  )
    26  
    27  var isGoTagAliasDisabled = os.Getenv("KITEX_GENERIC_GOTAG_ALIAS_DISABLED") == "True"
    28  
    29  func init() {
    30  	if isGoTagAliasDisabled {
    31  		// disable go.tag for dynamicgo
    32  		dthrift.RemoveAnnotationMapper(dthrift.AnnoScopeField, "go.tag")
    33  	}
    34  }
    35  
    36  // FieldDescriptor idl field descriptor
    37  type FieldDescriptor struct {
    38  	Name         string // field name
    39  	Alias        string // alias name
    40  	ID           int32
    41  	Required     bool
    42  	Optional     bool
    43  	DefaultValue interface{}
    44  	IsException  bool
    45  	Type         *TypeDescriptor
    46  	HTTPMapping  HTTPMapping
    47  	ValueMapping ValueMapping
    48  }
    49  
    50  // FieldName return field name maybe with an alias
    51  func (d *FieldDescriptor) FieldName() string {
    52  	if d.Alias != "" && !isGoTagAliasDisabled {
    53  		return d.Alias
    54  	}
    55  	return d.Name
    56  }
    57  
    58  // TypeDescriptor idl type descriptor
    59  type TypeDescriptor struct {
    60  	Name          string
    61  	Type          Type
    62  	Key           *TypeDescriptor   // for map key
    63  	Elem          *TypeDescriptor   // for slice or map element
    64  	Struct        *StructDescriptor // for struct
    65  	IsRequestBase bool
    66  }
    67  
    68  // StructDescriptor idl struct descriptor
    69  type StructDescriptor struct {
    70  	Name           string
    71  	FieldsByID     map[int32]*FieldDescriptor
    72  	FieldsByName   map[string]*FieldDescriptor
    73  	RequiredFields map[int32]*FieldDescriptor
    74  	DefaultFields  map[string]*FieldDescriptor
    75  }
    76  
    77  // CheckRequired check all required fields at the end of read or write
    78  func (d *StructDescriptor) CheckRequired(rw map[int32]struct{}) error {
    79  	for fieldID, field := range d.RequiredFields {
    80  		if _, ok := rw[fieldID]; !ok {
    81  			return fmt.Errorf("required field (%d/%s) missing", fieldID, field.FieldName())
    82  		}
    83  	}
    84  	return nil
    85  }
    86  
    87  // FunctionDescriptor idl function descriptor
    88  type FunctionDescriptor struct {
    89  	Name           string
    90  	Oneway         bool
    91  	Request        *TypeDescriptor
    92  	Response       *TypeDescriptor
    93  	HasRequestBase bool
    94  }
    95  
    96  // ServiceDescriptor idl service descriptor
    97  type ServiceDescriptor struct {
    98  	Name         string
    99  	Functions    map[string]*FunctionDescriptor
   100  	Router       Router
   101  	DynamicGoDsc *dthrift.ServiceDescriptor
   102  }
   103  
   104  // LookupFunctionByMethod lookup function by method
   105  func (s *ServiceDescriptor) LookupFunctionByMethod(method string) (*FunctionDescriptor, error) {
   106  	fnSvc, ok := s.Functions[method]
   107  	if !ok {
   108  		return nil, fmt.Errorf("missing method: %s in service: %s", method, s.Name)
   109  	}
   110  	return fnSvc, nil
   111  }