Skip to content

Commit

Permalink
refactor Fieldage to FieldDom so can support optional fields in Serders
Browse files Browse the repository at this point in the history
clean up naming.md
  • Loading branch information
SmithSamuelM committed Mar 8, 2024
1 parent fa936a2 commit 201f66f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 97 deletions.
20 changes: 10 additions & 10 deletions docs/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ est highest of comparison cleanest, hardest, softest

ful full of graceful, restful, faithful

hood state of being boyhood, knighthood, womanhood
-hood noun from noun group state of being boyhood, knighthood, womanhood

ible, ile, il capable of being digestible, responsible, docile, civil

Expand All @@ -400,7 +400,7 @@ ic like, made of metallic, toxic, poetic

ing action of running, wishing

ion act or state of confusion, correction, protection
ion, sion, tion act or state of being confusion, correction, protection

ism fact of being communism, socialism

Expand Down Expand Up @@ -433,21 +433,21 @@ ology study of geology, zoology, archaeology

ous, ious full of joyous, marvelous, furious

ship quality of or state of rank of friendship, leadership lordship
-ship noun from noun quality of state of rank of friendship, leadership lordship

scope instrument for seeing telescope, microscope
-scope instrument for seeing telescope, microscope

some like tiresome, lonesome
-some like tiresome, lonesome

tion, sion action, state of being condition, attention, fusion
-tude noun from adjective state of altitude, latitude

ty quality or state of liberty, majesty
-ty quality or state of liberty, majesty

ward toward southward, forward
-ward toward southward, forward

y like, full of, diminutive: noisy, sooty, kitty
-y like, full of, diminutive: noisy, sooty, kitty

ure noun from verv indicating act or office seizure prefecture
-ure noun from verb indicating act or office seizure prefecture



159 changes: 109 additions & 50 deletions src/keri/core/serdering.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import copy
import json
from collections import namedtuple
from dataclasses import dataclass, asdict, field

import cbor2 as cbor
import msgpack
Expand All @@ -30,17 +31,63 @@

logger = help.ogler.getLogger()

"""
Fieldage
saids (dict): keyed by saidive field labels with values as default codes
alls (dict): keyed by all field labels including saidive ones
with values as default codes
Example:
Fields = Labelage(saids={'d': DigDex.Blake3_256},
alls={'v': '','d':''})
"""
Fieldage = namedtuple("Fieldage", "saids alls") #values are dicts



@dataclass
class FieldDom:
"""
Field configuration dataclass for Serder messages. Provides field labels
and default field values for a given ilk (message type).
Attributes:
alls (dict): Ordered set of allowed fields (not extra)
alls must not be empty since at least version string
or protocol version filed is always required.
Fields in alls that appear must appear in order.
opts (dict): Optional fields within alls.
opts defaults to empty.
When opts is empty than all alls are required.
Any fields in alls but not in opts are required.
opts is a subset of alls
saids (dict): are saidive fields whose value may be computed as a said of the message.
saids defaults to empty
when provided a field in saids indicates the field value is saidive.
A simple SAID field value is always computed.
An AID SAID field value is only computed when its code indicates.
saids is a subset of alls
strict (bool): determines if alls is strict, no extra fields are allowed
strict defaults to True.
True means no extra fields are allowed, only those in alls.
False means extra fields are allowed besides thos in alls.
Extra fields are fields not in alls.
Extra fields may appear in any order after the last field
in alls.
When strict: no extras allowed
Any fields not in alls raise error
If opts is empty then all alls are required in order
If opts is not empty then fields in opts are optional but the rest of
the fields in alls are required
When not strict: extras allowed
Any fields not in alls must appear after all fields in alls
If opts is empty then all alls are required in order
If opts is not empty then fields in opts are optional but the rest of
the fields in alls are required
"""
alls: dict # all allowed fields when strict
opts: dict = field(default_factory=dict) # optional fields
saids: dict = field(default_factory=dict) # saidive fields
strict: bool = True # only alls allowed no extras

def __iter__(self):
return iter(asdict(self))


class Serdery:
Expand Down Expand Up @@ -114,9 +161,11 @@ class Serder:
generation and verification in addition to the required fields.
Class Attributes:
Fields (dict): Protocol specific dict of field labels keyed by ilk
(packet type string value). None is default key when no ilk needed.
Each entry is a
Fields (dict): nested dict of field labels keyed by protocol, version,
and message type (ilk). Felds labels are provided with a Fieldage
named tuple (saids, reqs, alls) that governs field type and presence.
None is default message type (ilk) when no ilk needed in a message.
See below for detailed logic associated with Fields class attribute
Properties:
raw (bytes): of serialized event only
Expand Down Expand Up @@ -158,6 +207,16 @@ class Serder:
Note:
loads and jumps of json use str whereas cbor and msgpack use bytes
Fields:
Each element of Fields is a FieldDom dataclass instance with four attributes:
alls (dict):
opts (dict):
saids (dict):
strict (bool):
"""
Dummy = "#" # dummy spaceholder char for said. Must not be a valid Base64 char

Expand Down Expand Up @@ -192,94 +251,94 @@ class Serder:
{
Vrsn_1_0:
{
Ilks.icp: Fieldage(saids={Saids.d: DigDex.Blake3_256,
Ilks.icp: FieldDom(saids={Saids.d: DigDex.Blake3_256,
Saids.i: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', kt='0',
k=[], nt='0', n=[], bt='0', b=[], c=[], a=[])),
Ilks.rot: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.rot: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', s='0', p='',
kt='0',k=[], nt='0', n=[], bt='0', br=[],
ba=[], a=[])),
Ilks.ixn: Fieldage({Saids.d: DigDex.Blake3_256},
Ilks.ixn: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', s='0', p='', a=[])),
Ilks.dip: Fieldage(saids={Saids.d: DigDex.Blake3_256,
Ilks.dip: FieldDom(saids={Saids.d: DigDex.Blake3_256,
Saids.i: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', kt='0',
k=[], nt='0', n=[], bt='0', b=[], c=[], a=[],
di='')),
Ilks.drt: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.drt: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', s='0', p='',
kt='0',k=[], nt='0', n=[], bt='0', br=[],
ba=[], a=[])),
Ilks.rct: Fieldage(saids={},
Ilks.rct: FieldDom(saids={},
alls=dict(v='', t='',d='', i='', s='0')),
Ilks.qry: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.qry: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', dt='', r='', rr='',
q={})),
Ilks.rpy: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.rpy: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', dt='', r='',a=[])),
Ilks.pro: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.pro: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', dt='', r='', rr='',
q={})),
Ilks.bar: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.bar: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', dt='', r='',a=[])),
Ilks.exn: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.exn: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='', d='', i="", p="", dt='', r='',q={},
a=[], e={})),
Ilks.vcp: Fieldage(saids={Saids.d: DigDex.Blake3_256,
Ilks.vcp: FieldDom(saids={Saids.d: DigDex.Blake3_256,
Saids.i: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', ii='', s='0', c=[],
bt='0', b=[], n='')),
Ilks.vrt: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.vrt: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', p='', s='0',
bt='0', br=[], ba=[])),
Ilks.iss: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.iss: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', ri='',
dt='')),
Ilks.rev: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.rev: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', ri='',
p='', dt='')),
Ilks.bis: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.bis: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', ii='', s='0', ra={},
dt='')),
Ilks.brv: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.brv: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', p='', ra={},
dt='')),
},
Vrsn_2_0:
{
Ilks.icp: Fieldage(saids={Saids.d: DigDex.Blake3_256,
Ilks.icp: FieldDom(saids={Saids.d: DigDex.Blake3_256,
Saids.i: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', kt='0',
k=[], nt='0', n=[], bt='0', b=[], c=[], a=[])),
Ilks.rot: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.rot: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', s='0', p='',
kt='0',k=[], nt='0', n=[], bt='0', br=[],
ba=[], c=[], a=[])),
Ilks.ixn: Fieldage({Saids.d: DigDex.Blake3_256},
Ilks.ixn: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', s='0', p='', a=[])),
Ilks.dip: Fieldage(saids={Saids.d: DigDex.Blake3_256,
Ilks.dip: FieldDom(saids={Saids.d: DigDex.Blake3_256,
Saids.i: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', kt='0',
k=[], nt='0', n=[], bt='0', b=[], c=[], a=[],
di='')),
Ilks.drt: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.drt: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', s='0', p='',
kt='0',k=[], nt='0', n=[], bt='0', br=[],
ba=[], c=[], a=[])),
Ilks.rct: Fieldage(saids={},
Ilks.rct: FieldDom(saids={},
alls=dict(v='', t='',d='', i='', s='0')),
Ilks.qry: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.qry: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', dt='', r='', rr='',
q={})),
Ilks.rpy: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.rpy: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', dt='', r='',a=[])),
Ilks.pro: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.pro: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', dt='', r='', rr='',
q={})),
Ilks.bar: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.bar: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='',d='', i='', dt='', r='',a=[])),
Ilks.exn: Fieldage(saids={Saids.d: DigDex.Blake3_256},
Ilks.exn: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', t='', d='', i="", p="", dt='', r='', q={},
a=[], e={})),
},
Expand All @@ -288,28 +347,28 @@ class Serder:
{
Vrsn_1_0:
{
None: Fieldage(saids={Saids.d: DigDex.Blake3_256},
None: FieldDom(saids={Saids.d: DigDex.Blake3_256},
alls=dict(v='', d='', i='', s='')),
},
Vrsn_2_0:
{
Ilks.vcp: Fieldage(saids={Saids.d: DigDex.Blake3_256,
Ilks.vcp: FieldDom(saids={Saids.d: DigDex.Blake3_256,
Saids.i: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', ii='', s='0', c=[],
bt='0', b=[], u='')),
Ilks.vrt: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.vrt: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', p='', s='0',
bt='0', br=[], ba=[])),
Ilks.iss: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.iss: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', ri='',
dt='')),
Ilks.rev: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.rev: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', ri='',
p='', dt='')),
Ilks.bis: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.bis: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', ii='', s='0', ra={},
dt='')),
Ilks.brv: Fieldage(saids={Saids.d: DigDex.Blake3_256,},
Ilks.brv: FieldDom(saids={Saids.d: DigDex.Blake3_256,},
alls=dict(v='', t='',d='', i='', s='0', p='', ra={},
dt='')),
},
Expand Down Expand Up @@ -634,7 +693,7 @@ def makify(self, sad, *, version=None,
value = copy.copy(value)
sad[label] = value

if 't' in sad: # packet type (ilk) requried so set value to ilk
if 't' in sad: # packet type (ilk) required so set value to ilk
sad['t'] = ilk

# ensure all required fields in alls are in sad
Expand Down
Loading

0 comments on commit 201f66f

Please sign in to comment.