Skip to content

Rewrite encoding in dataclass #97

@zjkmxy

Description

@zjkmxy

Homemade classes for TLV data structures are not compatible with Python's internal data structures. Now we can move to Python 3.11, so let's use dataclass.

Example

OLD:

class SignatureInfo(TlvModel):
    signature_type = UintField(0x1b, fixed_len=1)
    key_locator = ModelField(0x1c, KeyLocator)
    signature_nonce = UintField(0x26)
    signature_time = UintField(0x28)
    signature_seq_num = UintField(0x2a)


SignatureInfo().encode()
SignatureInfo.parse(wire)

NEW:

from dataclasses import dataclass, field
from ndn.encoding import tlv_encode, tlv_parse


@dataclass
class SignatureInfo:
    signature_type: int = field(metadata = {'tlv_type': 0x1b, 'fixed_len': 1})
    key_locator: KeyLocator = field(metadata = {'tlv_type': 0x1c})
    signature_nonce: int = field(metadata = {'tlv_type': 0x26})
    signature_time: int = field(metadata = {'tlv_type': 0x28})
    signature_seq_num: int = field(metadata = {'tlv_type': 0x2a})


tlv_encode(SignatureInfo())
tlv_parse(SignatureInfo, wire)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions