def DictToTLVWithWriter(self, debugPath: str, tag, data: Mapping, writer: tlv.TLVWriter):
     writer.startStructure(tag)
     for field in self.Fields:
         val = data.get(field.Label, None)
         field.PutFieldToTLV(field.Tag, val, writer,
                             debugPath + f'.{field.Label}')
     writer.endContainer()
Esempio n. 2
0
 def DictToTLVWithWriter(self, debugPath: str, tag, data: Mapping,
                         writer: tlv.TLVWriter):
     writer.startStructure(tag)
     for field in self.Fields:
         val = data.get(field.Label, None)
         if val is None:
             raise ValueError(
                 f"Field {debugPath}.{field.Label} is missing in the given dict"
             )
         field.PutFieldToTLV(field.Tag, val, writer,
                             debugPath + f'.{field.Label}')
     writer.endContainer()
Esempio n. 3
0
 def PutFieldToTLV(self,
                   tag,
                   val,
                   writer: tlv.TLVWriter,
                   debugPath: str = '?'):
     if not self.IsArray:
         self._PutSingleElementToTLV(tag, val, writer, debugPath)
         return
     if not isinstance(val, List):
         raise ValueError(
             f"Field {debugPath}.{self.Label} expected List[{self.Type}], but got {type(val)}"
         )
     writer.startArray(tag)
     for i, v in enumerate(val):
         self._PutSingleElementToTLV(None, v, writer, debugPath + f'[{i}]')
     writer.endContainer()
Esempio n. 4
0
    def PutFieldToTLV(self,
                      tag,
                      val,
                      writer: tlv.TLVWriter,
                      debugPath: str = '?'):
        if (val == NullValue):
            if (GetUnionUnderlyingType(self.Type, Nullable) is None):
                raise ValueError(
                    f"Field {debugPath}.{self.Label} was not nullable, but got a null"
                )

            writer.put(tag, None)
        elif (val is None):
            if (GetUnionUnderlyingType(self.Type, type(None)) is None):
                raise ValueError(
                    f"Field {debugPath}.{self.Label} was not optional, but encountered None"
                )
        else:
            #
            # If it is an optional or nullable type, it's going to be a union.
            # So, let's get at the 'real' type within that union before proceeding,
            # since at this point, we're guarenteed to not get None or Null as values.
            #
            elementType = GetUnionUnderlyingType(self.Type)
            if (elementType is None):
                elementType = self.Type

            if not isinstance(val, List):
                self._PutSingleElementToTLV(tag, val, elementType, writer,
                                            debugPath)
                return

            writer.startArray(tag)

            # Get the type of the list. This is a generic, which has its sub-type information of the list element
            # inside its type argument.
            (elementType, ) = typing.get_args(elementType)

            for i, v in enumerate(val):
                self._PutSingleElementToTLV(None, v, elementType, writer,
                                            debugPath + f'[{i}]')
            writer.endContainer()