github.com/cloudwego/kitex@v0.9.0/pkg/generic/descriptor/annotation.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 18 19 // Annotation idl annotation interface 20 type Annotation interface { 21 // Equal assert the given key/value is this Annotation 22 Equal(key, value string) bool // for search 23 // Handle the handle function of the Annotation 24 Handle() interface{} // one of NewHttpMapping/NewKeyMapping/NewValueMapping/NewRoute 25 } 26 27 var annotations = []Annotation{} 28 29 // RegisterAnnotation register an annotation for parser 30 func RegisterAnnotation(an Annotation) { 31 annotations = append(annotations, an) 32 } 33 34 func init() { 35 // HttpMapping 36 RegisterAnnotation(APIQueryAnnotation) 37 RegisterAnnotation(APIPathAnnotation) 38 RegisterAnnotation(APIHeaderAnnotation) 39 RegisterAnnotation(APICookieAnnotation) 40 RegisterAnnotation(APIBodyAnnotation) 41 RegisterAnnotation(APIHttpCodeAnnotation) 42 RegisterAnnotation(APINoneAnnotation) 43 RegisterAnnotation(APIRawBodyAnnotation) 44 // Route 45 RegisterAnnotation(APIGetAnnotation) 46 RegisterAnnotation(APIPostAnnotation) 47 RegisterAnnotation(APIPutAnnotation) 48 RegisterAnnotation(APIDeleteAnnotation) 49 // FieldMapping 50 RegisterAnnotation(GoTagAnnatition) 51 // ValueMapping 52 RegisterAnnotation(APIJSConvAnnotation) 53 // none annotation 54 RegisterAnnotation(apiVdAnnotation) 55 RegisterAnnotation(apiSerializerAnnotation) 56 RegisterAnnotation(apiParamAnnotation) 57 RegisterAnnotation(apiBaseURLAnnotation) 58 RegisterAnnotation(apiGenPathAnnotation) 59 RegisterAnnotation(apiVersionAnnotation) 60 RegisterAnnotation(apiTagAnnotation) 61 RegisterAnnotation(apiVDAnnotation) 62 } 63 64 // FindAnnotation search an annotation by given key/value 65 func FindAnnotation(key, value string) (interface{}, bool) { 66 for _, an := range annotations { 67 if an.Equal(key, value) { 68 return an.Handle(), true 69 } 70 } 71 // not in registered list 72 return nil, false 73 } 74 75 type bamAnnotation struct { 76 key string 77 handle interface{} 78 } 79 80 // NewBAMAnnotation create a bam annotation 81 func NewBAMAnnotation(key string, handle interface{}) Annotation { 82 return &bamAnnotation{key, handle} 83 } 84 85 func (a *bamAnnotation) Equal(key, value string) bool { 86 return a.key == key 87 } 88 89 func (a *bamAnnotation) Handle() interface{} { 90 return a.handle 91 } 92 93 type noneAnnotation struct { 94 key string 95 } 96 97 // NewNoneAnnotation create do nothing annotation 98 func NewNoneAnnotation(key string) Annotation { 99 return &noneAnnotation{key} 100 } 101 102 func (a *noneAnnotation) Equal(key, value string) bool { 103 return a.key == key 104 } 105 106 func (a *noneAnnotation) Handle() interface{} { 107 return nil 108 } 109 110 var ( 111 apiVdAnnotation = NewNoneAnnotation("api.vd") 112 apiSerializerAnnotation = NewNoneAnnotation("api.serializer") 113 apiParamAnnotation = NewNoneAnnotation("api.param") 114 apiBaseURLAnnotation = NewNoneAnnotation("api.baseurl") 115 apiGenPathAnnotation = NewNoneAnnotation("api.gen_path") 116 apiVersionAnnotation = NewNoneAnnotation("api.version") 117 apiTagAnnotation = NewNoneAnnotation("api.tag") 118 apiVDAnnotation = NewNoneAnnotation("api.vd") 119 ) 120 121 type noneWithValueAnnotation struct { 122 key, value string 123 } 124 125 // NewNoneWithValueAnnotation create do nothing annotation 126 func NewNoneWithValueAnnotation(key, value string) Annotation { 127 return &noneWithValueAnnotation{key, value} 128 } 129 130 func (a *noneWithValueAnnotation) Equal(key, value string) bool { 131 return a.key == key && a.value == value 132 } 133 134 func (a *noneWithValueAnnotation) Handle() interface{} { 135 return nil 136 }