github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/api.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 conv 18 19 import ( 20 "sync" 21 ) 22 23 // ContextKey is the key type for context arguments 24 type ContextKey struct { 25 _ bool 26 } 27 28 var ( 29 // CtxKeyHTTPResponse is the key for http.ResponseSetter in context 30 CtxKeyHTTPResponse = &ContextKey{} 31 // CtxKeyHTTPRequest is the key for http.RequestGetter in context 32 CtxKeyHTTPRequest = &ContextKey{} 33 // CtxKeyThriftRespBase is the key for base.Base in context 34 CtxKeyThriftRespBase = &ContextKey{} 35 // CtxKeyThriftReqBase is the key for base.BaseResp in context 36 CtxKeyThriftReqBase = &ContextKey{} 37 // CtxKeyConvOptions is the key for Options in context 38 CtxKeyConvOptions = &ContextKey{} 39 ) 40 41 var ( 42 // DefaultBufferSize is the default buffer size for conversion 43 DefaultBufferSize = 4096 44 // DefaultHttpValueBufferSize is the default buffer size for copying a json value 45 DefaulHttpValueBufferSizeForJSON = 1024 46 // DefaultHttpValueBufferSize is the default buffer size for copying a http value 47 DefaulHttpValueBufferSizeForScalar = 64 48 ) 49 50 type Options struct { 51 52 // EnableValueMapping indicates if value mapping (api.js_conv...) should be enabled 53 EnableValueMapping bool 54 // EnableHttpMapping indicates if http mapping (api.query|api.header...) should be enabled 55 EnableHttpMapping bool 56 // EnableThriftBase indicates if thrift/base should be recognized and mapping to/from context 57 EnableThriftBase bool 58 59 // String2Int64 indicates if string value cane be read as **Int8/Int16/Int32/Int64/Float64**, 60 String2Int64 bool 61 62 // Int642Stringin indicates if a int64 value a **Int64** value can be written as string 63 Int642String bool 64 65 // NoBase64Binary indicates if base64 string should be Encode/Decode as []byte 66 NoBase64Binary bool 67 // ByteAsUint8 indicates if byte should be conv as uint8 (default is int8), this only works for t2j now 68 ByteAsUint8 bool 69 70 // WriteOptionalField indicates if optional-requireness fields should be written when not given 71 WriteOptionalField bool 72 // WriteDefaultField indicates if default-requireness fields should be written if 73 WriteDefaultField bool 74 // WriteRequireField indicates if required-requireness fields should be written empty value if 75 // not found 76 WriteRequireField bool 77 // DisallowUnknownField indicates if unknown fields should be skipped 78 DisallowUnknownField bool 79 80 // ReadHttpValueFallback indicates if http-annotated fields should fallback to http body after reading from non-body parts (header,cookie...) failed 81 ReadHttpValueFallback bool 82 // WriteHttpValueFallback indicates if http-annotated fields should fallback to http body after writing to non-body parts (header,cookie...) failed 83 WriteHttpValueFallback bool 84 // TracebackRequredOrRootFields indicates if required-requireness 85 // or root-level fields should be seeking on http-values when reading failed from current layer of json. 86 // this option is only used in j2t now. 87 TracebackRequredOrRootFields bool 88 // OmitHttpMappingErrors indicates to omit http-mapping failing errors. 89 // If there are more-than-one HTTP annotations on the field, dynamicgo will try to mapping next annotation source (from left to right) until succeed. 90 OmitHttpMappingErrors bool 91 92 // NoCopyString indicates if string-kind http values should be copied or just referenced (if possible) 93 NoCopyString bool 94 // UseNativeSkip indicates if using thrift.SkipNative() or thrift.SkipGo() 95 UseNativeSkip bool 96 97 // ConvertException indicates that it returns error for thrift exception fields when doing BinaryConv t2j 98 ConvertException bool 99 100 // UseKitexHttpEncoding indicating using kitex's text encoding to output complex http values 101 UseKitexHttpEncoding bool 102 } 103 104 var bufPool = sync.Pool{ 105 New: func() interface{} { 106 ret := make([]byte, 0, DefaultBufferSize) 107 return &ret 108 }, 109 } 110 111 // NewBytes returns a new byte slice from pool 112 func NewBytes() *[]byte { 113 return bufPool.Get().(*[]byte) 114 } 115 116 // FreeBytes returns a byte slice to pool and reset it 117 func FreeBytes(b *[]byte) { 118 *b = (*b)[:0] 119 bufPool.Put(b) 120 }