Refactor magic numbers into CslaKnownTypes

This commit is contained in:
RunasSudo 2025-04-21 16:17:51 +10:00
parent 8e4fd963ad
commit ab3526cb96
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 68 additions and 24 deletions

View File

@ -17,6 +17,7 @@
import io import io
import struct import struct
from .known_types import CslaKnownTypes
from .serialization_info import ChildData, FieldData, SerializationInfo from .serialization_info import ChildData, FieldData, SerializationInfo
class CslaBinaryReader: class CslaBinaryReader:
@ -109,79 +110,79 @@ class CslaBinaryReader:
# CslaBinaryReader.ReadObject # CslaBinaryReader.ReadObject
known_type = self.stream.read(1)[0] known_type = self.stream.read(1)[0]
if known_type == 1: # CslaKnownTypes.Boolean if known_type == CslaKnownTypes.Boolean.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 2: # CslaKnownTypes.Char if known_type == CslaKnownTypes.Char.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 3: # CslaKnownTypes.SByte if known_type == CslaKnownTypes.SByte.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 4: # CslaKnownTypes.Byte if known_type == CslaKnownTypes.Byte.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 5: # CslaKnownTypes.Int16 if known_type == CslaKnownTypes.Int16.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 6: # CslaKnownTypes.UInt16 if known_type == CslaKnownTypes.UInt16.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 7: # CslaKnownTypes.Int32 if known_type == CslaKnownTypes.Int32.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 8: # CslaKnownTypes.UInt32 if known_type == CslaKnownTypes.UInt32.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 9: # CslaKnownTypes.Int64 if known_type == CslaKnownTypes.Int64.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 10: # CslaKnownTypes.UInt64 if known_type == CslaKnownTypes.UInt16.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 11: # CslaKnownTypes.Single if known_type == CslaKnownTypes.Single.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 12: # CslaKnownTypes.Double if known_type == CslaKnownTypes.Double.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 13: # CslaKnownTypes.Decimal if known_type == CslaKnownTypes.Decimal.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 14: # CslaKnownTypes.DateTime if known_type == CslaKnownTypes.DateTime.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 15: # CslaKnownTypes.String if known_type == CslaKnownTypes.String.value:
return self.read_string() return self.read_string()
if known_type == 16: # CslaKnownTypes.TimeSpan if known_type == CslaKnownTypes.TimeSpan.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 17: # CslaKnownTypes.DateTimeOffset if known_type == CslaKnownTypes.DateTimeOffset.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 18: # CslaKnownTypes.Guid if known_type == CslaKnownTypes.Guid.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 19: # CslaKnownTypes.ByteArray if known_type == CslaKnownTypes.ByteArray.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 20: # CslaKnownTypes.CharArray if known_type == CslaKnownTypes.CharArray.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 21: # CslaKnownTypes.ListOfInt if known_type == CslaKnownTypes.ListOfInt.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 22: # CslaKnownTypes.Null if known_type == CslaKnownTypes.Null.value:
raise NotImplementedError() raise NotImplementedError()
if known_type == 23: # CslaKnownTypes.StringWithDictionaryKey if known_type == CslaKnownTypes.StringWithDictionaryKey.value:
system_string = self.read_string() system_string = self.read_string()
dictionary_key = self.read_int32() dictionary_key = self.read_int32()
self.keywords_dictionary[dictionary_key] = system_string self.keywords_dictionary[dictionary_key] = system_string
return system_string return system_string
if known_type == 24: # CslaKnownTypes.StringDictionaryKey if known_type == CslaKnownTypes.StringDictionaryKey.value:
raise NotImplementedError() raise NotImplementedError()
raise ValueError('Unexpected object tag {}'.format(known_type)) raise ValueError('Unexpected object tag {}'.format(known_type))

View File

@ -0,0 +1,43 @@
# pycsla-binary: Python implementation of CSLA .NET binary serialisation
# Copyright (C) 2025 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from enum import Enum
class CslaKnownTypes(Enum):
Boolean = 1
Char = 2
SByte = 3
Byte = 4
Int16 = 5
UInt16 = 6
Int32 = 7
UInt32 = 8
Int64 = 9
UInt64 = 10
Single = 11
Double = 12
Decimal = 13
DateTime = 14
String = 15
TimeSpan = 16
DateTimeOffset = 17
Guid = 18
ByteArray = 19
CharArray = 20
ListOfInt = 21
Null = 22
StringWithDictionaryKey = 23
StringDictionaryKey = 24