gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/deserializerLogic.go (about) 1 package corejson 2 3 import ( 4 "encoding/json" 5 6 "gitlab.com/evatix-go/core/constants" 7 "gitlab.com/evatix-go/core/errcore" 8 "gitlab.com/evatix-go/core/internal/reflectinternal" 9 ) 10 11 type deserializerLogic struct { 12 BytesTo deserializeFromBytesTo 13 ResultTo deserializeFromResultTo 14 } 15 16 func (it deserializerLogic) Apply( 17 jsonResult *Result, 18 toPtr interface{}, 19 ) error { 20 return jsonResult.Unmarshal( 21 toPtr) 22 } 23 24 func (it deserializerLogic) UsingStringPtr( 25 jsonString *string, 26 toPtr interface{}, 27 ) error { 28 if jsonString == nil { 29 return it.UsingBytes( 30 nil, 31 toPtr) 32 } 33 34 return it.UsingString( 35 *jsonString, 36 toPtr) 37 } 38 39 func (it deserializerLogic) UsingError( 40 errInJsonFormat error, 41 toPtr interface{}, 42 ) error { 43 if errInJsonFormat == nil { 44 return nil 45 } 46 47 return it.UsingString( 48 errInJsonFormat.Error(), 49 toPtr) 50 } 51 52 // UsingErrorWhichJsonResult 53 // 54 // given error is in json format for json result 55 func (it deserializerLogic) UsingErrorWhichJsonResult( 56 errInJsonResultJson error, 57 toPtr interface{}, 58 ) error { 59 if errInJsonResultJson == nil { 60 return nil 61 } 62 63 jsonResult := NewResult.UsingStringWithType( 64 errInJsonResultJson.Error(), 65 "ErrorAsJsonResult") 66 67 return jsonResult.Deserialize(toPtr) 68 } 69 70 func (it deserializerLogic) UsingResult( 71 jsonResult *Result, 72 toPtr interface{}, 73 ) error { 74 return jsonResult.Unmarshal( 75 toPtr) 76 } 77 78 func (it deserializerLogic) ApplyMust( 79 jsonResult *Result, 80 toPtr interface{}, 81 ) { 82 err := jsonResult.Unmarshal( 83 toPtr) 84 85 if err != nil { 86 panic(err) 87 } 88 } 89 90 func (it deserializerLogic) UsingString( 91 jsonString string, 92 toPtr interface{}, 93 ) error { 94 return it.UsingBytes( 95 []byte(jsonString), 96 toPtr) 97 } 98 99 func (it deserializerLogic) FromString( 100 jsonString string, 101 toPtr interface{}, 102 ) error { 103 return it.UsingBytes( 104 []byte(jsonString), 105 toPtr) 106 } 107 108 // FromTo 109 // 110 // Giving nil is not support from to. 111 // 112 // Warning: must check nil before for from, to both. 113 // 114 // Casting from to steps: 115 // - reflection first if equal type + right ptr and not nil. 116 // - []byte 117 // - string 118 // - Jsoner 119 // - Result 120 // - *Result 121 // - bytesSerializer 122 // - serializerFunc 123 // - error to string then cast from json string then to actual unmarshal 124 func (it deserializerLogic) FromTo( 125 fromAny interface{}, 126 toPtr interface{}, 127 ) error { 128 return CastAny.FromToDefault( 129 fromAny, 130 toPtr) 131 } 132 133 func (it deserializerLogic) MapAnyToPointer( 134 isSkipOnEmpty bool, 135 currentItemMap map[string]interface{}, 136 toPtr interface{}, 137 ) error { 138 if isSkipOnEmpty && len(currentItemMap) == 0 { 139 return nil 140 } 141 142 jsonResult := New(currentItemMap) 143 144 if jsonResult.HasIssuesOrEmpty() { 145 return jsonResult.MeaningfulError() 146 } 147 148 return jsonResult.Deserialize(toPtr) 149 } 150 151 func (it deserializerLogic) UsingStringOption( 152 isIgnoreEmptyString bool, 153 jsonString string, 154 toPtr interface{}, 155 ) error { 156 if isIgnoreEmptyString && jsonString == "" { 157 return nil 158 } 159 160 return it.UsingBytes( 161 []byte(jsonString), 162 toPtr) 163 } 164 165 func (it deserializerLogic) UsingStringIgnoreEmpty( 166 jsonString string, 167 toPtr interface{}, 168 ) error { 169 if jsonString == "" { 170 return nil 171 } 172 173 return it.UsingBytes( 174 []byte(jsonString), 175 toPtr) 176 } 177 178 // UsingBytes 179 // 180 // json.Unmarshal bytes to object 181 func (it deserializerLogic) UsingBytes( 182 rawBytes []byte, 183 toPtr interface{}, 184 ) error { 185 err := json.Unmarshal( 186 rawBytes, 187 toPtr) 188 189 if err == nil { 190 return nil 191 } 192 193 var payloadString string 194 if len(rawBytes) > 0 { 195 payloadString = string(rawBytes) 196 } 197 198 // has error 199 compiledMessage := errcore.MessageVarMap( 200 "json unmarshal failed", 201 map[string]interface{}{ 202 "err": err, 203 "dst": reflectinternal.TypeName(toPtr), 204 "payload": payloadString, 205 }) 206 207 return errcore. 208 UnMarshallingFailedType. 209 ErrorNoRefs(compiledMessage) 210 211 } 212 213 func (it deserializerLogic) UsingBytesPointerMust( 214 rawBytesPointer *[]byte, 215 toPtr interface{}, 216 ) { 217 err := it.UsingBytesPointer( 218 rawBytesPointer, 219 toPtr) 220 221 if err != nil { 222 panic(err) 223 } 224 } 225 226 func (it deserializerLogic) UsingBytesIf( 227 isDeserialize bool, 228 rawBytes []byte, 229 toPtr interface{}, 230 ) error { 231 if !isDeserialize { 232 return nil 233 } 234 235 return it.UsingBytes( 236 rawBytes, 237 toPtr) 238 } 239 240 func (it deserializerLogic) UsingBytesPointerIf( 241 isDeserialize bool, 242 rawBytesPointer *[]byte, 243 toPtr interface{}, 244 ) error { 245 if !isDeserialize { 246 return nil 247 } 248 249 return it.UsingBytesPointer( 250 rawBytesPointer, 251 toPtr) 252 } 253 254 func (it deserializerLogic) UsingBytesPointer( 255 rawBytesPointer *[]byte, 256 toPtr interface{}, 257 ) error { 258 if rawBytesPointer == nil || *rawBytesPointer == nil { 259 reference := errcore.VarTwoNoType( 260 "rawBytesPointer", constants.NilAngelBracket, 261 "To Reference Type", reflectinternal.TypeName(toPtr)) 262 263 return errcore. 264 UnMarshallingFailedType. 265 Error( 266 "failed to unmarshal nil bytes pointer.", 267 reference) 268 } 269 270 return it.UsingBytes( 271 *rawBytesPointer, 272 toPtr) 273 } 274 275 func (it deserializerLogic) UsingBytesMust( 276 rawBytes []byte, 277 toPtr interface{}, 278 ) { 279 err := it.UsingBytes( 280 rawBytes, 281 toPtr) 282 283 if err != nil { 284 panic(err) 285 } 286 } 287 288 func (it deserializerLogic) UsingSafeBytesMust( 289 rawBytes []byte, 290 toPtr interface{}, 291 ) { 292 if len(rawBytes) == 0 { 293 return 294 } 295 296 err := it.UsingBytes(rawBytes, toPtr) 297 298 if err != nil { 299 panic(err) 300 } 301 } 302 303 func (it deserializerLogic) AnyToFieldsMap( 304 anyItem interface{}, 305 ) (map[string]interface{}, error) { 306 jsonResult := New(anyItem) 307 308 return jsonResult.DeserializedFieldsToMap() 309 } 310 311 func (it deserializerLogic) UsingSerializerTo( 312 serializer bytesSerializer, 313 toPtr interface{}, 314 ) (parsingErr error) { 315 jsonResult := NewResult.UsingSerializer( 316 serializer) 317 318 return jsonResult.Deserialize(toPtr) 319 } 320 321 func (it deserializerLogic) UsingSerializerFuncTo( 322 serializerFunc func() ([]byte, error), 323 toPtr interface{}, 324 ) (parsingErr error) { 325 jsonResult := NewResult.UsingSerializerFunc( 326 serializerFunc) 327 328 return jsonResult.Deserialize(toPtr) 329 } 330 331 func (it deserializerLogic) UsingDeserializerToOption( 332 isSkipOnDeserializerNull bool, 333 deserializer bytesDeserializer, 334 toPtr interface{}, 335 ) (parsingErr error) { 336 if isSkipOnDeserializerNull && deserializer == nil { 337 return nil 338 } 339 340 if deserializer == nil { 341 return errcore.CannotBeNilType.ErrorNoRefs( 342 "deserializer is nil", 343 ) 344 } 345 346 return deserializer.Deserialize(toPtr) 347 } 348 349 // UsingDeserializerDefined 350 // 351 // on deserializer null it will not do anything but return nil error 352 // 353 // only deserialize if deserializer is not null. 354 func (it deserializerLogic) UsingDeserializerDefined( 355 deserializer bytesDeserializer, 356 toPtr interface{}, 357 ) (parsingErr error) { 358 return it.UsingDeserializerToOption( 359 true, 360 deserializer, 361 toPtr) 362 } 363 364 // UsingDeserializerFuncDefined 365 // 366 // on deserializer null it will not do anything but return nil error 367 // 368 // only deserialize if deserializer is not null. 369 func (it deserializerLogic) UsingDeserializerFuncDefined( 370 deserializerFunc func(toPtr interface{}) error, 371 toPtr interface{}, 372 ) (parsingErr error) { 373 if deserializerFunc == nil { 374 return errcore.CannotBeNilType.ErrorNoRefs( 375 "deserializer function is nil", 376 ) 377 } 378 379 return deserializerFunc(toPtr) 380 } 381 382 func (it deserializerLogic) UsingJsonerToAny( 383 isSkipOnNullJsoner bool, 384 jsoner Jsoner, 385 toPtr interface{}, 386 ) error { 387 if isSkipOnNullJsoner && jsoner == nil { 388 return nil 389 } 390 391 if jsoner == nil { 392 return errcore. 393 CannotBeNilType. 394 ErrorNoRefs("jsoner given as nil cannot deserialize to") 395 } 396 397 jsonResult := jsoner.JsonPtr() 398 399 return jsonResult.Deserialize(toPtr) 400 }