dbt-labs / hologram

A library for automatically generating Draft 7 JSON Schemas from Python dataclasses
MIT License
9 stars 13 forks source link

support polymorphism #3

Open cmcarthur opened 5 years ago

cmcarthur commented 5 years ago

given dataclasses like:

@dataclass
class A:
  literal: StrLiteral('a')

@dataclass
class B:
  literal: StrLiteral('b')

@dataclass
class C:
  a_or_b: Union[A, B]

C.to_dict({'literal': 'a'}) and C.to_dict({'literal': 'b'}) should instantiate the right subclass for a_or_b.

voluptuous does this by checking all of the possible subschemas and picking the one that matches, or raises an exception if none of them match. we may want to implement something similar in to_dict

another approach would be to leverage json schema's if/else construct to specify different subschemas based on a switch field.