imgui_pydantic.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from imgui_bundle import ImVec4, ImVec2, ImColor
  2. from typing_extensions import Annotated
  3. from pydantic_core import core_schema
  4. from pydantic import (
  5. GetCoreSchemaHandler,
  6. GetJsonSchemaHandler,
  7. )
  8. from pydantic.json_schema import JsonSchemaValue
  9. from typing import Any
  10. class _ImVec4PydanticAnnotation:
  11. @classmethod
  12. def __get_pydantic_core_schema__(
  13. cls,
  14. _source_type: Any,
  15. _handler: GetCoreSchemaHandler,
  16. ) -> core_schema.CoreSchema:
  17. def validate_from_tuple(value: tuple[float, float, float, float]) -> ImVec4:
  18. result = ImVec4()
  19. result.x = value[0]
  20. result.y = value[1]
  21. result.z = value[2]
  22. result.w = value[3]
  23. return result
  24. from_tuple_schema = core_schema.chain_schema(
  25. [
  26. core_schema.tuple_schema([core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema()]),
  27. core_schema.no_info_plain_validator_function(validate_from_tuple),
  28. ]
  29. )
  30. return core_schema.json_or_python_schema(
  31. json_schema=from_tuple_schema,
  32. python_schema=core_schema.union_schema(
  33. [
  34. core_schema.is_instance_schema(ImVec4),
  35. from_tuple_schema,
  36. ]
  37. ),
  38. serialization=core_schema.plain_serializer_function_ser_schema(
  39. lambda instance: [instance.x, instance.y, instance.z, instance.w]
  40. ),
  41. )
  42. @classmethod
  43. def __get_pydantic_json_schema__(
  44. cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
  45. ) -> JsonSchemaValue:
  46. return handler(core_schema.tuple_schema([core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema()]))
  47. # ImVec4_Pydantic is a synonym for ImVec4, which is compatible with Pydantic
  48. ImVec4_Pydantic = Annotated[
  49. ImVec4, _ImVec4PydanticAnnotation
  50. ]
  51. class _ImVec2PydanticAnnotation:
  52. @classmethod
  53. def __get_pydantic_core_schema__(
  54. cls,
  55. _source_type: Any,
  56. _handler: GetCoreSchemaHandler,
  57. ) -> core_schema.CoreSchema:
  58. def validate_from_tuple(value: tuple[float, float]) -> ImVec2:
  59. result = ImVec2()
  60. result.x = value[0]
  61. result.y = value[1]
  62. return result
  63. from_tuple_schema = core_schema.chain_schema(
  64. [
  65. core_schema.tuple_schema([core_schema.float_schema(), core_schema.float_schema()]),
  66. core_schema.no_info_plain_validator_function(validate_from_tuple),
  67. ]
  68. )
  69. return core_schema.json_or_python_schema(
  70. json_schema=from_tuple_schema,
  71. python_schema=core_schema.union_schema(
  72. [
  73. core_schema.is_instance_schema(ImVec2),
  74. from_tuple_schema,
  75. ]
  76. ),
  77. serialization=core_schema.plain_serializer_function_ser_schema(
  78. lambda instance: [instance.x, instance.y]
  79. ),
  80. )
  81. @classmethod
  82. def __get_pydantic_json_schema__(
  83. cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
  84. ) -> JsonSchemaValue:
  85. return handler(core_schema.tuple_schema([core_schema.float_schema(), core_schema.float_schema()]))
  86. # ImVec2_Pydantic is a synonym for ImVec2, which is compatible with Pydantic
  87. ImVec2_Pydantic = Annotated[
  88. ImVec2, _ImVec2PydanticAnnotation
  89. ]
  90. class _ImColorPydanticAnnotation:
  91. @classmethod
  92. def __get_pydantic_core_schema__(
  93. cls,
  94. _source_type: Any,
  95. _handler: GetCoreSchemaHandler,
  96. ) -> core_schema.CoreSchema:
  97. def validate_from_tuple(value: tuple[float, float, float, float]) -> ImColor:
  98. result = ImColor()
  99. result.value.x = value[0]
  100. result.value.y = value[1]
  101. result.value.z = value[2]
  102. result.value.w = value[3]
  103. return result
  104. from_tuple_schema = core_schema.chain_schema(
  105. [
  106. core_schema.tuple_schema([core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema()]),
  107. core_schema.no_info_plain_validator_function(validate_from_tuple),
  108. ]
  109. )
  110. return core_schema.json_or_python_schema(
  111. json_schema=from_tuple_schema,
  112. python_schema=core_schema.union_schema(
  113. [
  114. core_schema.is_instance_schema(ImVec4),
  115. from_tuple_schema,
  116. ]
  117. ),
  118. serialization=core_schema.plain_serializer_function_ser_schema(
  119. lambda instance: [instance.value.x, instance.value.y, instance.value.z, instance.value.w]
  120. ),
  121. )
  122. @classmethod
  123. def __get_pydantic_json_schema__(
  124. cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
  125. ) -> JsonSchemaValue:
  126. return handler(core_schema.tuple_schema([core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema(), core_schema.float_schema()]))
  127. # ImColor_Pydantic is a synonym for ImColor, which is compatible with Pydantic
  128. ImColor_Pydantic = Annotated[
  129. ImColor, _ImColorPydanticAnnotation
  130. ]