Fokko commented on code in PR #6525: URL: https://github.com/apache/iceberg/pull/6525#discussion_r1067878461
########## python/pyiceberg/typedef.py: ########## @@ -126,30 +135,41 @@ def json(self, exclude_none: bool = True, exclude: Optional[Set[str]] = None, by class PydanticStruct(IcebergBaseModel): + _position_to_field_name: Dict[int, str] = PrivateAttr() + _field_name_to_pydantic_field: Dict[str, Field] = PrivateAttr() + class Config: frozen = False - def __setitem__(self, pos: int, value: Any) -> None: - positions = list(self.__fields__.values()) - field = positions[pos] - if value is None: - if field.default is not None: - value = field.default - elif field.default_factory is not None: - value = field.default_factory() + @staticmethod + def _get_default_field_value(field: Field) -> Optional[Any]: + if field.default is not None: + return field.default + elif field.default_factory is not None: + return field.default_factory() + else: + return None + + def set_record_schema(self, record_schema: StructType) -> None: + self._field_name_to_pydantic_field = {field.name: field for field in self.__fields__.values()} + self._position_to_field_name = {idx: field.name for idx, field in enumerate(record_schema.fields)} + for name, field in self.__fields__.items(): + setattr(self, name, PydanticStruct._get_default_field_value(field)) - self.__setattr__(field.name, value) + def __setitem__(self, pos: int, value: Any) -> None: + field_name = self._position_to_field_name[pos] Review Comment: Indeed 👍🏻 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org