Skip to content

Commit

Permalink
rename RecordType to SerializationRecordType, remove the byte size co…
Browse files Browse the repository at this point in the history
…nstraint, re-add all values present in the NRBF spec
  • Loading branch information
adamsitnik committed Jun 11, 2024
1 parent e703c6b commit 7bd4b27
Show file tree
Hide file tree
Showing 36 changed files with 212 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ namespace System.Formats.Nrbf;
internal enum AllowedRecordTypes : uint
{
None = 0,
SerializedStreamHeader = 1 << RecordType.SerializedStreamHeader,
ClassWithId = 1 << RecordType.ClassWithId,
SystemClassWithMembersAndTypes = 1 << RecordType.SystemClassWithMembersAndTypes,
ClassWithMembersAndTypes = 1 << RecordType.ClassWithMembersAndTypes,
BinaryObjectString = 1 << RecordType.BinaryObjectString,
BinaryArray = 1 << RecordType.BinaryArray,
MemberPrimitiveTyped = 1 << RecordType.MemberPrimitiveTyped,
MemberReference = 1 << RecordType.MemberReference,
ObjectNull = 1 << RecordType.ObjectNull,
MessageEnd = 1 << RecordType.MessageEnd,
BinaryLibrary = 1 << RecordType.BinaryLibrary,
ObjectNullMultiple256 = 1 << RecordType.ObjectNullMultiple256,
ObjectNullMultiple = 1 << RecordType.ObjectNullMultiple,
ArraySinglePrimitive = 1 << RecordType.ArraySinglePrimitive,
ArraySingleObject = 1 << RecordType.ArraySingleObject,
ArraySingleString = 1 << RecordType.ArraySingleString,
SerializedStreamHeader = 1 << SerializationRecordType.SerializedStreamHeader,
ClassWithId = 1 << SerializationRecordType.ClassWithId,
SystemClassWithMembersAndTypes = 1 << SerializationRecordType.SystemClassWithMembersAndTypes,
ClassWithMembersAndTypes = 1 << SerializationRecordType.ClassWithMembersAndTypes,
BinaryObjectString = 1 << SerializationRecordType.BinaryObjectString,
BinaryArray = 1 << SerializationRecordType.BinaryArray,
MemberPrimitiveTyped = 1 << SerializationRecordType.MemberPrimitiveTyped,
MemberReference = 1 << SerializationRecordType.MemberReference,
ObjectNull = 1 << SerializationRecordType.ObjectNull,
MessageEnd = 1 << SerializationRecordType.MessageEnd,
BinaryLibrary = 1 << SerializationRecordType.BinaryLibrary,
ObjectNullMultiple256 = 1 << SerializationRecordType.ObjectNullMultiple256,
ObjectNullMultiple = 1 << SerializationRecordType.ObjectNullMultiple,
ArraySinglePrimitive = 1 << SerializationRecordType.ArraySinglePrimitive,
ArraySingleObject = 1 << SerializationRecordType.ArraySingleObject,
ArraySingleString = 1 << SerializationRecordType.ArraySingleString,

Nulls = ObjectNull | ObjectNullMultiple256 | ObjectNullMultiple,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal ArrayOfClassesRecord(ArrayInfo arrayInfo, MemberTypeInfo memberTypeInfo
Records = [];
}

public override RecordType RecordType => RecordType.BinaryArray;
public override SerializationRecordType RecordType => SerializationRecordType.BinaryArray;

internal List<SerializationRecord> Records { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class ArraySingleObjectRecord : SZArrayRecord<object?>

private ArraySingleObjectRecord(ArrayInfo arrayInfo) : base(arrayInfo) => Records = [];

public override RecordType RecordType => RecordType.ArraySingleObject;
public override SerializationRecordType RecordType => SerializationRecordType.ArraySingleObject;

public override TypeName TypeName
=> s_typeName ??= TypeName.Parse("System.Object[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".AsSpan());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal ArraySinglePrimitiveRecord(ArrayInfo arrayInfo, IReadOnlyList<T> values
ValuesToRead = 0; // there is nothing to read anymore
}

public override RecordType RecordType => RecordType.ArraySinglePrimitive;
public override SerializationRecordType RecordType => SerializationRecordType.ArraySinglePrimitive;

/// <inheritdoc />
public override TypeName TypeName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal sealed class ArraySingleStringRecord : SZArrayRecord<string?>

private ArraySingleStringRecord(ArrayInfo arrayInfo) : base(arrayInfo) => Records = [];

public override RecordType RecordType => RecordType.ArraySingleString;
public override SerializationRecordType RecordType => SerializationRecordType.ArraySingleString;

/// <inheritdoc />
public override TypeName TypeName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private BinaryArrayRecord(ArrayInfo arrayInfo, MemberTypeInfo memberTypeInfo)
Values = [];
}

public override RecordType RecordType => RecordType.BinaryArray;
public override SerializationRecordType RecordType => SerializationRecordType.BinaryArray;

/// <inheritdoc/>
public override ReadOnlySpan<int> Lengths => new int[1] { Length };
Expand Down Expand Up @@ -80,17 +80,17 @@ private protected override Array Deserialize(Type arrayType, bool allowNulls)

switch (record.RecordType)
{
case RecordType.BinaryArray:
case RecordType.ArraySinglePrimitive:
case RecordType.ArraySingleObject:
case RecordType.ArraySingleString:
case SerializationRecordType.BinaryArray:
case SerializationRecordType.ArraySinglePrimitive:
case SerializationRecordType.ArraySingleObject:
case SerializationRecordType.ArraySingleString:
ArrayRecord nestedArrayRecord = (ArrayRecord)record;
Array nestedArray = nestedArrayRecord.GetArray(actualElementType, allowNulls);
array.SetValue(nestedArray, resultIndex++);
break;
case RecordType.ObjectNull:
case RecordType.ObjectNullMultiple256:
case RecordType.ObjectNullMultiple:
case SerializationRecordType.ObjectNull:
case SerializationRecordType.ObjectNullMultiple256:
case SerializationRecordType.ObjectNullMultiple:
if (!allowNulls)
{
ThrowHelper.ThrowArrayContainedNulls();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private BinaryLibraryRecord(SerializationRecordId libraryId, string libraryName)
LibraryName = libraryName;
}

public override RecordType RecordType => RecordType.BinaryLibrary;
public override SerializationRecordType RecordType => SerializationRecordType.BinaryLibrary;

public override TypeName TypeName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private BinaryObjectStringRecord(SerializationRecordId id, string value) : base(
Id = id;
}

public override RecordType RecordType => RecordType.BinaryObjectString;
public override SerializationRecordType RecordType => SerializationRecordType.BinaryObjectString;

/// <inheritdoc />
public override SerializationRecordId Id { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private ClassWithIdRecord(SerializationRecordId id, ClassRecord metadataClass) :
MetadataClass = metadataClass;
}

public override RecordType RecordType => RecordType.ClassWithId;
public override SerializationRecordType RecordType => SerializationRecordType.ClassWithId;

/// <inheritdoc />
public override SerializationRecordId Id { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private ClassWithMembersAndTypesRecord(ClassInfo classInfo, MemberTypeInfo membe
{
}

public override RecordType RecordType => RecordType.ClassWithMembersAndTypes;
public override SerializationRecordType RecordType => SerializationRecordType.ClassWithMembersAndTypes;

internal static ClassWithMembersAndTypesRecord Decode(BinaryReader reader, RecordMap recordMap, PayloadOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class MemberPrimitiveTypedRecord<T> : PrimitiveTypeRecord<T>

internal MemberPrimitiveTypedRecord(T value, SerializationRecordId id) : base(value) => Id = id;

public override RecordType RecordType => RecordType.MemberPrimitiveTyped;
public override SerializationRecordType RecordType => SerializationRecordType.MemberPrimitiveTyped;

/// <inheritdoc />
public override SerializationRecordId Id { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private MemberReferenceRecord(SerializationRecordId reference, RecordMap recordM
RecordMap = recordMap;
}

public override RecordType RecordType => RecordType.MemberReference;
public override SerializationRecordType RecordType => SerializationRecordType.MemberReference;

internal SerializationRecordId Reference { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private MessageEndRecord()
{
}

public override RecordType RecordType => RecordType.MessageEnd;
public override SerializationRecordType RecordType => SerializationRecordType.MessageEnd;

public override SerializationRecordId Id => SerializationRecordId.NoId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static bool StartsWithPayloadHeader(byte[] bytes)
#endif

return bytes.Length >= SerializedStreamHeaderRecord.Size
&& bytes[0] == (byte)RecordType.SerializedStreamHeader
&& bytes[0] == (byte)SerializationRecordType.SerializedStreamHeader
#if NET
&& BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(9)) == SerializedStreamHeaderRecord.MajorVersion
&& BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(13)) == SerializedStreamHeaderRecord.MinorVersion;
Expand Down Expand Up @@ -162,7 +162,7 @@ private static SerializationRecord Decode(BinaryReader reader, PayloadOptions op
const AllowedRecordTypes Allowed = AllowedRecordTypes.AnyObject
| AllowedRecordTypes.BinaryLibrary | AllowedRecordTypes.MessageEnd;

RecordType recordType;
SerializationRecordType recordType;
SerializationRecord nextRecord;
do
{
Expand Down Expand Up @@ -202,39 +202,39 @@ private static SerializationRecord Decode(BinaryReader reader, PayloadOptions op
nextRecord = DecodeNext(reader, recordMap, Allowed, options, out recordType);
PushFirstNestedRecordInfo(nextRecord, readStack);
}
while (recordType != RecordType.MessageEnd);
while (recordType != SerializationRecordType.MessageEnd);

readOnlyRecordMap = recordMap;
return recordMap.GetRootRecord(header);
}

private static SerializationRecord DecodeNext(BinaryReader reader, RecordMap recordMap,
AllowedRecordTypes allowed, PayloadOptions options, out RecordType recordType)
AllowedRecordTypes allowed, PayloadOptions options, out SerializationRecordType recordType)
{
byte nextByte = reader.ReadByte();
if (((uint)allowed & (1u << nextByte)) == 0)
{
ThrowHelper.ThrowForUnexpectedRecordType(nextByte);
}
recordType = (RecordType)nextByte;
recordType = (SerializationRecordType)nextByte;

SerializationRecord record = recordType switch
{
RecordType.ArraySingleObject => ArraySingleObjectRecord.Decode(reader),
RecordType.ArraySinglePrimitive => DecodeArraySinglePrimitiveRecord(reader),
RecordType.ArraySingleString => ArraySingleStringRecord.Decode(reader),
RecordType.BinaryArray => BinaryArrayRecord.Decode(reader, recordMap, options),
RecordType.BinaryLibrary => BinaryLibraryRecord.Decode(reader),
RecordType.BinaryObjectString => BinaryObjectStringRecord.Decode(reader),
RecordType.ClassWithId => ClassWithIdRecord.Decode(reader, recordMap),
RecordType.ClassWithMembersAndTypes => ClassWithMembersAndTypesRecord.Decode(reader, recordMap, options),
RecordType.MemberPrimitiveTyped => DecodeMemberPrimitiveTypedRecord(reader),
RecordType.MemberReference => MemberReferenceRecord.Decode(reader, recordMap),
RecordType.MessageEnd => MessageEndRecord.Singleton,
RecordType.ObjectNull => ObjectNullRecord.Instance,
RecordType.ObjectNullMultiple => ObjectNullMultipleRecord.Decode(reader),
RecordType.ObjectNullMultiple256 => ObjectNullMultiple256Record.Decode(reader),
RecordType.SerializedStreamHeader => SerializedStreamHeaderRecord.Decode(reader),
SerializationRecordType.ArraySingleObject => ArraySingleObjectRecord.Decode(reader),
SerializationRecordType.ArraySinglePrimitive => DecodeArraySinglePrimitiveRecord(reader),
SerializationRecordType.ArraySingleString => ArraySingleStringRecord.Decode(reader),
SerializationRecordType.BinaryArray => BinaryArrayRecord.Decode(reader, recordMap, options),
SerializationRecordType.BinaryLibrary => BinaryLibraryRecord.Decode(reader),
SerializationRecordType.BinaryObjectString => BinaryObjectStringRecord.Decode(reader),
SerializationRecordType.ClassWithId => ClassWithIdRecord.Decode(reader, recordMap),
SerializationRecordType.ClassWithMembersAndTypes => ClassWithMembersAndTypesRecord.Decode(reader, recordMap, options),
SerializationRecordType.MemberPrimitiveTyped => DecodeMemberPrimitiveTypedRecord(reader),
SerializationRecordType.MemberReference => MemberReferenceRecord.Decode(reader, recordMap),
SerializationRecordType.MessageEnd => MessageEndRecord.Singleton,
SerializationRecordType.ObjectNull => ObjectNullRecord.Instance,
SerializationRecordType.ObjectNullMultiple => ObjectNullMultipleRecord.Decode(reader),
SerializationRecordType.ObjectNullMultiple256 => ObjectNullMultiple256Record.Decode(reader),
SerializationRecordType.SerializedStreamHeader => SerializedStreamHeaderRecord.Decode(reader),
_ => SystemClassWithMembersAndTypesRecord.Decode(reader, recordMap, options),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class ObjectNullMultiple256Record : NullsRecord
{
private ObjectNullMultiple256Record(byte count) => NullCount = count;

public override RecordType RecordType => RecordType.ObjectNullMultiple256;
public override SerializationRecordType RecordType => SerializationRecordType.ObjectNullMultiple256;

internal override int NullCount { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class ObjectNullMultipleRecord : NullsRecord
{
private ObjectNullMultipleRecord(int count) => NullCount = count;

public override RecordType RecordType => RecordType.ObjectNullMultiple;
public override SerializationRecordType RecordType => SerializationRecordType.ObjectNullMultiple;

internal override int NullCount { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class ObjectNullRecord : NullsRecord
{
internal static ObjectNullRecord Instance { get; } = new();

public override RecordType RecordType => RecordType.ObjectNull;
public override SerializationRecordType RecordType => SerializationRecordType.ObjectNull;

internal override int NullCount => 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private RectangularArrayRecord(Type elementType, ArrayInfo arrayInfo,

}

public override RecordType RecordType => RecordType.BinaryArray;
public override SerializationRecordType RecordType => SerializationRecordType.BinaryArray;

public override ReadOnlySpan<int> Lengths => _lengths.AsSpan();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal SerializationRecord() // others can't derive from this type
/// Gets the type of the record.
/// </summary>
/// <value>The type of the record.</value>
public abstract RecordType RecordType { get; }
public abstract SerializationRecordType RecordType { get; }

/// <summary>
/// Gets the ID of the record.
Expand Down
Loading

0 comments on commit 7bd4b27

Please sign in to comment.