github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/annotation/key_mapping.go (about)

     1  /**
     2   * Copyright 2023 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 annotation
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/cloudwego/dynamicgo/thrift"
    23  	"github.com/cloudwego/thriftgo/parser"
    24  )
    25  
    26  const (
    27  	APIKey thrift.AnnoType = 501
    28  	// NameCase thrift.AnnoType = 502
    29  
    30  	APIKeyName string = "api.key"
    31  )
    32  
    33  var (
    34  	NameCaseKeys = []string{"agw.to_snake", "janus.to_snake", "agw.to_lower_camel_case", "janus.to_lower_camel_case"}
    35  )
    36  
    37  type keyMappingAnnotation struct {
    38  	typ thrift.AnnoID
    39  }
    40  
    41  func newKeyMappingAnnotation(typ thrift.AnnoID) keyMappingAnnotation {
    42  	return keyMappingAnnotation{
    43  		typ: typ,
    44  	}
    45  }
    46  
    47  func (self keyMappingAnnotation) ID() thrift.AnnoID {
    48  	return self.typ
    49  }
    50  
    51  func (self keyMappingAnnotation) Make(ctx context.Context, values []parser.Annotation, ast interface{}) (interface{}, error) {
    52  	if len(values) == 0 {
    53  		return nil, nil
    54  	}
    55  	for _, v := range values {
    56  		// NOTICE: we only handle the first value here
    57  		if len(v.Values) == 1 {
    58  			switch self.typ.Type() {
    59  			case APIKey:
    60  				return &apiKey{v.Values[0]}, nil
    61  			default:
    62  				return nil, errNotImplemented("keyMappingAnnotation must have APIKey type")
    63  			}
    64  		}
    65  	}
    66  	return nil, nil
    67  }
    68  
    69  type apiKey struct {
    70  	Value string
    71  }
    72  
    73  func (m apiKey) Map(ctx context.Context, key string) string {
    74  	return m.Value
    75  }