github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-grpc-middleware/tags/options.go (about)

     1  // Copyright 2017 Michal Witkowski. All Rights Reserved.
     2  // See LICENSE for licensing terms.
     3  
     4  package grpc_ctxtags
     5  
     6  var (
     7  	defaultOptions = &options{
     8  		requestFieldsFunc: nil,
     9  	}
    10  )
    11  
    12  type options struct {
    13  	requestFieldsFunc        RequestFieldExtractorFunc
    14  	requestFieldsFromInitial bool
    15  }
    16  
    17  func evaluateOptions(opts []Option) *options {
    18  	optCopy := &options{}
    19  	*optCopy = *defaultOptions
    20  	for _, o := range opts {
    21  		o(optCopy)
    22  	}
    23  	return optCopy
    24  }
    25  
    26  type Option func(*options)
    27  
    28  // WithFieldExtractor customizes the function for extracting log fields from protobuf messages, for
    29  // unary and server-streamed methods only.
    30  func WithFieldExtractor(f RequestFieldExtractorFunc) Option {
    31  	return func(o *options) {
    32  		o.requestFieldsFunc = f
    33  	}
    34  }
    35  
    36  // WithFieldExtractorForInitialReq customizes the function for extracting log fields from protobuf messages,
    37  // for all unary and streaming methods. For client-streams and bidirectional-streams, the tags will be
    38  // extracted from the first message from the client.
    39  func WithFieldExtractorForInitialReq(f RequestFieldExtractorFunc) Option {
    40  	return func(o *options) {
    41  		o.requestFieldsFunc = f
    42  		o.requestFieldsFromInitial = true
    43  	}
    44  }