public Either <string, DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex) { try { var a1Opt = a1RW.deserialize(serialized, startIndex); if (a1Opt.leftValueOut(out var a1Err)) { return($"{nameof(AndRW3<A1, A2, A3, B>)} a1 failed: {a1Err}"); } var a1Info = a1Opt.__unsafeGetRight; var a2Opt = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead); if (a2Opt.leftValueOut(out var a2Err)) { return($"{nameof(AndRW3<A1, A2, A3, B>)} a2 failed: {a2Err}"); } var a2Info = a2Opt.__unsafeGetRight; var a3Opt = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead); if (a3Opt.leftValueOut(out var a3Err)) { return($"{nameof(AndRW3<A1, A2, A3, B>)} a3 failed: {a3Err}"); } var a3Info = a3Opt.__unsafeGetRight; var info = new DeserializeInfo <B>( mapper(a1Info.value, a2Info.value, a3Info.value), a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead ); return(info); } catch (Exception e) { return($"{nameof(AndRW3<A1, A2, A3, B>)} threw {e}"); } }
public static ICachedBlob <A> a <A>( ISerializedRW <A> rw, PathStr path, A defaultValue, ILog log = null, Log.Level onDeserializeFailureLogLevel = Log.Level.ERROR ) { log = log ?? Log.d; return(new FileCachedBlob(path).bimap(BiMapper.a( (byte[] bytes) => { var deserializedOpt = rw.deserialize(bytes, 0); if (deserializedOpt.isNone) { if (log.willLog(onDeserializeFailureLogLevel)) { log.log( onDeserializeFailureLogLevel, $"Can't deserialize {path}, deleting and returning default value." ); } clear(path).getOrLog($"Couldn't clear file: '{path}'", log: log); return defaultValue; } return deserializedOpt.__unsafeGetValue.value; }, a => rw.serialize(a).toArray() ))); }
public Option <DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex) { try { var a1Opt = a1RW.deserialize(serialized, startIndex); if (a1Opt.isNone) { return(Option <DeserializeInfo <B> > .None); } var a1Info = a1Opt.__unsafeGetValue; var a2Opt = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead); if (a2Opt.isNone) { return(Option <DeserializeInfo <B> > .None); } var a2Info = a2Opt.__unsafeGetValue; var a3Opt = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead); if (a3Opt.isNone) { return(Option <DeserializeInfo <B> > .None); } var a3Info = a3Opt.__unsafeGetValue; var info = new DeserializeInfo <B>( mapper(a1Info.value, a2Info.value, a3Info.value), a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead ); return(F.some(info)); } catch (Exception) { return(Option <DeserializeInfo <B> > .None); } }
public Either <string, DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex) { const string rwName = nameof(AndRW4 <A1, A2, A3, A4, B>); var step = "a1"; try { var a1Opt = a1RW.deserialize(serialized, startIndex); if (a1Opt.leftValueOut(out var a1Err)) { return($"{rwName} a1 failed: {a1Err}"); } var a1Info = a1Opt.__unsafeGetRight; step = "a2"; var a2Opt = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead); if (a2Opt.leftValueOut(out var a2Err)) { return($"{rwName} a2 failed: {a2Err}"); } var a2Info = a2Opt.__unsafeGetRight; step = "a3"; var a3Opt = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead); if (a3Opt.leftValueOut(out var a3Err)) { return($"{rwName} a3 failed: {a3Err}"); } var a3Info = a3Opt.__unsafeGetRight; step = "a4"; var a4Opt = a4RW.deserialize( serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead ); if (a4Opt.leftValueOut(out var a4Err)) { return($"{rwName} a4 failed: {a4Err}"); } var a4Info = a4Opt.__unsafeGetRight; step = "mapper"; var info = new DeserializeInfo <B>( mapper(a1Info.value, a2Info.value, a3Info.value, a4Info.value), a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead ); return(info); } catch (Exception e) { return($"{nameof(rwName)} at index {startIndex}, step {step} threw {e}"); } }
public static IPrefValueRW<A> custom<A>( ISerializedRW<A> aRW, PrefVal.OnDeserializeFailure onDeserializeFailure = PrefVal.OnDeserializeFailure.ReturnDefault, ILog log = null ) => custom( a => Convert.ToBase64String(aRW.serialize(a).toArray()), s => { try { var bytes = Convert.FromBase64String(s); return aRW.deserialize(bytes, 0).map(_ => _.value); } catch (FormatException) { return Option<A>.None; } }, onDeserializeFailure, log );
public Option <DeserializeInfo <Option <A> > > deserialize(byte[] bytes, int startIndex) { if (bytes.Length == 0 || startIndex > bytes.Length - 1) { return(Option <DeserializeInfo <Option <A> > > .None); } var discriminator = bytes[startIndex]; switch (discriminator) { case OptByteArrayRW.DISCRIMINATOR_NONE: return(F.some(new DeserializeInfo <Option <A> >(Option <A> .None, 1))); case OptByteArrayRW.DISCRIMINATOR_SOME: return(rw.deserialize(bytes, startIndex + 1).map(info => new DeserializeInfo <Option <A> >(F.some(info.value), info.bytesRead + 1) )); default: return(Option <DeserializeInfo <Option <A> > > .None); } }
public Option <DeserializeInfo <Tpl <A, B> > > deserialize(byte[] serialized, int startIndex) { try { var aOpt = aRW.deserialize(serialized, startIndex); if (aOpt.isNone) { return(Option <DeserializeInfo <Tpl <A, B> > > .None); } var aInfo = aOpt.get; var bOpt = bRW.deserialize(serialized, startIndex + aInfo.bytesRead); if (bOpt.isNone) { return(Option <DeserializeInfo <Tpl <A, B> > > .None); } var bInfo = bOpt.get; var info = new DeserializeInfo <Tpl <A, B> >( F.t(aInfo.value, bInfo.value), aInfo.bytesRead + bInfo.bytesRead ); return(F.some(info)); } catch (Exception) { return(Option <DeserializeInfo <Tpl <A, B> > > .None); } }
public Option <DeserializeInfo <Either <A, B> > > deserialize(byte[] serialized, int startIndex) { if (serialized.Length == 0 || startIndex > serialized.Length - 1) { return(Option <DeserializeInfo <Either <A, B> > > .None); } var discriminator = serialized[startIndex]; switch (discriminator) { case EitherByteArrayRW.DISCRIMINATOR_LEFT: return(aRW.deserialize(serialized, startIndex + 1).map(info => new DeserializeInfo <Either <A, B> >(Either <A, B> .Left(info.value), info.bytesRead + 1) )); case EitherByteArrayRW.DISCRIMINATOR_RIGHT: return(bRW.deserialize(serialized, startIndex + 1).map(info => new DeserializeInfo <Either <A, B> >(Either <A, B> .Right(info.value), info.bytesRead + 1) )); default: return(Option <DeserializeInfo <Either <A, B> > > .None); } }
public void TestFailure() => rw.deserialize(noise.toArray(), 0).shouldBeNone();
public static A clone <A>(this A a, ISerializedRW <A> rw) { var bytes = rw.serialize(a).toArray(); return(rw.deserialize(bytes, 0).rightOrThrow.value); }
public Either <string, DeserializeInfo <B> > deserialize(byte[] serialized, int startIndex) { const string rwName = nameof(AndRW7 <A1, A2, A3, A4, A5, A6, A7, B>); var step = "a1"; try { var a1Opt = a1RW.deserialize(serialized, startIndex); if (a1Opt.leftValueOut(out var a1Err)) { return($"{rwName} a1 failed: {a1Err}"); } var a1Info = a1Opt.__unsafeGetRight; step = "a2"; var a2Opt = a2RW.deserialize(serialized, startIndex + a1Info.bytesRead); if (a2Opt.leftValueOut(out var a2Err)) { return($"{rwName} a2 failed: {a2Err}"); } var a2Info = a2Opt.__unsafeGetRight; step = "a3"; var a3Opt = a3RW.deserialize(serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead); if (a3Opt.leftValueOut(out var a3Err)) { return($"{rwName} a3 failed: {a3Err}"); } var a3Info = a3Opt.__unsafeGetRight; step = "a4"; var a4Opt = a4RW.deserialize( serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead ); if (a4Opt.leftValueOut(out var a4Err)) { return($"{rwName} a4 failed: {a4Err}"); } var a4Info = a4Opt.__unsafeGetRight; step = "a5"; var a5Opt = a5RW.deserialize( serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead ); if (a5Opt.leftValueOut(out var a5Err)) { return($"{rwName} a5 failed: {a5Err}"); } var a5Info = a5Opt.__unsafeGetRight; step = "a6"; var a6Opt = a6RW.deserialize( serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead + a5Info.bytesRead ); if (a6Opt.leftValueOut(out var a6Err)) { return($"{rwName} a6 failed: {a6Err}"); } var a6Info = a6Opt.__unsafeGetRight; step = "a7"; var a7Opt = a7RW.deserialize( serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead + a5Info.bytesRead + a6Info.bytesRead ); if (a7Opt.leftValueOut(out var a7Err)) { return($"{rwName} a7 failed: {a7Err}"); } var a7Info = a7Opt.__unsafeGetRight; step = "a8"; var a8Opt = a8RW.deserialize( serialized, startIndex + a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead + a5Info.bytesRead + a6Info.bytesRead + a7Info.bytesRead ); if (a8Opt.leftValueOut(out var a8Err)) { return($"{rwName} a8 failed: {a8Err}"); } var a8Info = a8Opt.__unsafeGetRight; step = "mapper"; var info = new DeserializeInfo <B>( mapper(a1Info.value, a2Info.value, a3Info.value, a4Info.value, a5Info.value, a6Info.value, a7Info.value, a8Info.value), a1Info.bytesRead + a2Info.bytesRead + a3Info.bytesRead + a4Info.bytesRead + a5Info.bytesRead + a6Info.bytesRead + a7Info.bytesRead + a8Info.bytesRead ); return(info); } catch (Exception e) { return($"{rwName} at index {startIndex}, step {step} threw {e}"); } }