github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/common/utils/metadata/meta_data.go (about)

     1  package metadata
     2  
     3  import (
     4  	"context"
     5  	"github.com/nyan233/littlerpc/core/common/metadata"
     6  	"github.com/nyan233/littlerpc/core/common/stream"
     7  	"reflect"
     8  )
     9  
    10  func InputOffset(m *metadata.Process) int {
    11  	switch {
    12  	case m.SupportStream && m.SupportContext:
    13  		return 2
    14  	case m.SupportContext, m.SupportStream:
    15  		return 1
    16  	default:
    17  		return 0
    18  	}
    19  }
    20  
    21  // IFContextOrStream 检查输入参数中是否有context&stream
    22  // type必须为Method/Func类型
    23  func IFContextOrStream(opt *metadata.Process, typ reflect.Type) (offset int) {
    24  	for j := 0; j < typ.NumIn() && j < 2; j++ {
    25  		switch reflect.New(typ.In(j)).Interface().(type) {
    26  		case *context.Context:
    27  			opt.SupportContext = true
    28  			offset++
    29  		case *stream.LStream:
    30  			opt.SupportStream = true
    31  			offset++
    32  		}
    33  	}
    34  	return
    35  }