How to set default values in a SEQUENCE OF object

236 views Asked by At

I'm starting to use the pyasn1 library and I have a question on how to set default values in a SEQUENCE OF object. My ASN1 structure is the following:

Asn1Def DEFINITIONS AUTOMATIC TAGS ::= 
BEGIN
  CasinoPlayer ::= SEQUENCE       
  {                                                     
     name      UTF8String (SIZE(1..16)),
     luckyNumbers SEQUENCE  (SIZE(3)) OF INTEGER DEFAULT {7,7,7}
  }                                                     
END

I understood how to create a DEFAULT field in the CasinoPlayer SEQUENCE using namedtype.DefaultedNamedType objects and using subtype to add SIZE constraint but how shall I initialize the default value {7,7,7}?

Thank you

1

There are 1 answers

5
Ilya Etingof On

I am thinking it should look like this:

class CasinoPlayer(Sequence):
    componentType = NamedTypes(
        NamedType(
            'name',
            UTF8String(
                ConstraintsIntersection(
                    ValueSizeConstraint(1, 16)
                )
            )
        ),
        DefaultedNamedType(
            'luckyNumbers',
            SequenceOf(
                componentType=Integer(),
                sizeSpec=ConstraintsIntersection(
                    ValueSizeConstraint(3, 3)
                )
            ).setComponentByPosition(0, 7)
             .setComponentByPosition(1, 7)
             .setComponentByPosition(2, 7)
        )
    )

Plus you probably need to assign the tags to each ASN.1 type (as it's implied by the AUTOMATIC TAGS clause).

UPDATE:

Actually, this should work, but it does not! Luckily, there is the fix which should make defaulted SequenceOf propagating to the Sequence field for as long as it's a DefaultedNamedType.