github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_map.go (about) 1 package jzon 2 3 import ( 4 "reflect" 5 "unsafe" 6 ) 7 8 type mapDecoderBuilder struct { 9 decoder *mapDecoder 10 valPtrRType rtype 11 } 12 13 func newMapDecoder(mapType reflect.Type) *mapDecoderBuilder { 14 // Compatible with standard lib 15 // Map key must either have string kind, have an integer kind, 16 // or be an encoding.TextUnmarshaler. 17 keyType := mapType.Key() 18 var ( 19 keyDecoder ValDecoder 20 ) 21 keyPtrType := reflect.PtrTo(keyType) 22 keyKind := keyType.Kind() 23 // the string type is specially treated in order to be 24 // compatible with the standard lib 25 // the order of two first cases has been reversed in go1.14 26 switch { 27 case keyPtrType.Implements(textUnmarshalerType): 28 keyDecoder = textUnmarshalerDecoder(rtypeOfType(keyPtrType)) 29 case keyKind == reflect.String: 30 keyDecoder = keyDecoders[keyKind] 31 default: 32 if keyDecoder = keyDecoders[keyType.Kind()]; keyDecoder == nil { 33 return nil 34 } 35 } 36 return &mapDecoderBuilder{ 37 decoder: &mapDecoder{ 38 rtype: rtypeOfType(mapType), 39 keyRType: rtypeOfType(keyType), 40 keyDec: keyDecoder, 41 valRType: rtypeOfType(mapType.Elem()), 42 }, 43 valPtrRType: rtypeOfType(reflect.PtrTo(mapType.Elem())), 44 } 45 } 46 47 func (builder *mapDecoderBuilder) build(cache decoderCache) { 48 builder.decoder.valDec = cache[builder.valPtrRType] 49 } 50 51 type mapDecoder struct { 52 rtype rtype 53 54 keyRType rtype 55 keyDec ValDecoder 56 57 valRType rtype 58 valDec ValDecoder 59 } 60 61 func (dec *mapDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 62 c, err := it.nextToken() 63 if err != nil { 64 return err 65 } 66 if c == 'n' { 67 it.head++ 68 if err = it.expectBytes("ull"); err != nil { 69 return err 70 } 71 *(*unsafe.Pointer)(ptr) = nil 72 return nil 73 } 74 if c != '{' { 75 return UnexpectedByteError{got: c, exp: '{', exp2: 'n'} 76 } 77 it.head++ 78 c, err = it.nextToken() 79 if err != nil { 80 return err 81 } 82 if c == '}' { 83 it.head++ 84 if *(*unsafe.Pointer)(ptr) == nil { 85 typedmemmove(dec.rtype, ptr, unsafeMakeMap(dec.rtype, 0)) 86 } 87 return nil 88 } 89 if *(*unsafe.Pointer)(ptr) == nil { 90 typedmemmove(dec.rtype, ptr, unsafeMakeMap(dec.rtype, 0)) 91 } 92 for { 93 key := unsafe_New(dec.keyRType) 94 opt := DecOpts{ 95 MapKey: true, 96 } 97 if err = dec.keyDec.Decode(key, it, opt.noescape()); err != nil { 98 return err 99 } 100 c, err = it.nextToken() 101 if err != nil { 102 return err 103 } 104 if c != ':' { 105 return UnexpectedByteError{got: c, exp: ':'} 106 } 107 it.head++ 108 val := unsafe_New(dec.valRType) 109 if err = dec.valDec.Decode(val, it, nil); err != nil { 110 return err 111 } 112 mapassign(dec.rtype, *(*unsafe.Pointer)(ptr), key, val) 113 c, err = it.nextToken() 114 if err != nil { 115 return err 116 } 117 switch c { 118 case '}': 119 it.head++ 120 return nil 121 case ',': 122 it.head++ 123 default: 124 return UnexpectedByteError{got: c, exp: '}', exp2: ','} 125 } 126 } 127 } 128 129 /* 130 // key decoders 131 type stringKeyDecoder struct { 132 } 133 134 func (*stringKeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 135 s, err := it.ReadString() 136 if err != nil { 137 return err 138 } 139 *(*string)(ptr) = s 140 return nil 141 } 142 143 // int key decoders 144 type int8KeyDecoder struct { 145 } 146 147 func (*int8KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 148 if err := it.expectQuote(); err != nil { 149 return err 150 } 151 i, err := it.ReadInt8() 152 if err != nil { 153 return err 154 } 155 if err := it.expectQuote(); err != nil { 156 return err 157 } 158 *(*int8)(ptr) = i 159 return nil 160 } 161 162 type int16KeyDecoder struct { 163 } 164 165 func (*int16KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 166 if err := it.expectQuote(); err != nil { 167 return err 168 } 169 i, err := it.ReadInt16() 170 if err != nil { 171 return err 172 } 173 if err := it.expectQuote(); err != nil { 174 return err 175 } 176 *(*int16)(ptr) = i 177 return nil 178 } 179 180 type int32KeyDecoder struct { 181 } 182 183 func (*int32KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 184 if err := it.expectQuote(); err != nil { 185 return err 186 } 187 i, err := it.ReadInt32() 188 if err != nil { 189 return err 190 } 191 if err := it.expectQuote(); err != nil { 192 return err 193 } 194 *(*int32)(ptr) = i 195 return nil 196 } 197 198 type int64KeyDecoder struct { 199 } 200 201 func (*int64KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 202 if err := it.expectQuote(); err != nil { 203 return err 204 } 205 i, err := it.ReadInt64() 206 if err != nil { 207 return err 208 } 209 if err := it.expectQuote(); err != nil { 210 return err 211 } 212 *(*int64)(ptr) = i 213 return nil 214 } 215 216 // uint key decoders 217 type uint8KeyDecoder struct { 218 } 219 220 func (*uint8KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 221 if err := it.expectQuote(); err != nil { 222 return err 223 } 224 i, err := it.ReadUint8() 225 if err != nil { 226 return err 227 } 228 if err := it.expectQuote(); err != nil { 229 return err 230 } 231 *(*uint8)(ptr) = i 232 return nil 233 } 234 235 type uint16KeyDecoder struct { 236 } 237 238 func (*uint16KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 239 if err := it.expectQuote(); err != nil { 240 return err 241 } 242 i, err := it.ReadUint16() 243 if err != nil { 244 return err 245 } 246 if err := it.expectQuote(); err != nil { 247 return err 248 } 249 *(*uint16)(ptr) = i 250 return nil 251 } 252 253 type uint32KeyDecoder struct { 254 } 255 256 func (*uint32KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 257 if err := it.expectQuote(); err != nil { 258 return err 259 } 260 i, err := it.ReadUint32() 261 if err != nil { 262 return err 263 } 264 if err := it.expectQuote(); err != nil { 265 return err 266 } 267 *(*uint32)(ptr) = i 268 return nil 269 } 270 271 type uint64KeyDecoder struct { 272 } 273 274 func (*uint64KeyDecoder) Decode(ptr unsafe.Pointer, it *Iterator, _ *DecOpts) error { 275 if err := it.expectQuote(); err != nil { 276 return err 277 } 278 i, err := it.ReadUint64() 279 if err != nil { 280 return err 281 } 282 if err := it.expectQuote(); err != nil { 283 return err 284 } 285 *(*uint64)(ptr) = i 286 return nil 287 } 288 */