|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 |
| -from dataclasses import dataclass |
| 5 | +from typing import Annotated, Literal |
| 6 | + |
| 7 | +from pydantic import BaseModel, Field, RootModel |
6 | 8 |
|
7 | 9 | __all__ = [
|
8 | 10 | "Link",
|
9 | 11 | "LinksCollection",
|
10 | 12 | "SdmColumnLink",
|
11 | 13 | "SdmColumnLinksCollection",
|
| 14 | + "SdmLinksCollection", |
12 | 15 | "SdmSchemaLink",
|
13 | 16 | "SdmSchemaLinksCollection",
|
14 | 17 | "SdmTableLink",
|
15 | 18 | "SdmTableLinksCollection",
|
16 | 19 | ]
|
17 | 20 |
|
18 | 21 |
|
19 |
| -@dataclass(slots=True, kw_only=True) |
20 |
| -class Link: |
21 |
| - """A link to documentation.""" |
| 22 | +class Link(BaseModel): |
| 23 | + """A link to documentation. |
| 24 | +
|
| 25 | + This is a Pydantic model to facilitate the parsing of SQLAlchemy queries |
| 26 | + directly into the domain. |
| 27 | + """ |
22 | 28 |
|
23 |
| - html_url: str |
24 |
| - """The URL to the documentation page.""" |
| 29 | + html_url: str = Field(description="The URL to the documentation page.") |
25 | 30 |
|
26 |
| - title: str |
27 |
| - """The title of the documentation.""" |
| 31 | + title: str = Field(description="The title of the documentation.") |
28 | 32 |
|
29 |
| - type: str |
30 |
| - """The type of documentation.""" |
| 33 | + type: str = Field(description="The type of documentation.") |
31 | 34 |
|
32 |
| - collection_title: str | None = None |
33 |
| - """The title of the collection of documentation this link refers to.""" |
| 35 | + collection_title: str | None = Field( |
| 36 | + None, |
| 37 | + description=( |
| 38 | + "The title of the collection of documentation this link refers to." |
| 39 | + ), |
| 40 | + ) |
34 | 41 |
|
35 | 42 |
|
36 |
| -@dataclass(slots=True, kw_only=True) |
37 | 43 | class SdmSchemaLink(Link):
|
38 | 44 | """A link to an SDM schema's documentation."""
|
39 | 45 |
|
40 |
| - name: str |
41 |
| - """The name of the schema.""" |
| 46 | + schema_name: str = Field(description="The name of the schema.") |
42 | 47 |
|
43 | 48 |
|
44 |
| -@dataclass(slots=True, kw_only=True) |
45 | 49 | class SdmTableLink(Link):
|
46 | 50 | """A link to an SDM table's documentation."""
|
47 | 51 |
|
48 |
| - schema_name: str |
49 |
| - """The name of the schema.""" |
| 52 | + schema_name: str = Field(description="The name of the schema.") |
50 | 53 |
|
51 |
| - name: str |
52 |
| - """The name of the table.""" |
| 54 | + table_name: str = Field(description="The name of the table.") |
53 | 55 |
|
54 | 56 |
|
55 |
| -@dataclass(slots=True, kw_only=True) |
56 | 57 | class SdmColumnLink(Link):
|
57 | 58 | """A link to an SDM column's documentation."""
|
58 | 59 |
|
59 |
| - schema_name: str |
60 |
| - """The name of the schema.""" |
| 60 | + schema_name: str = Field(description="The name of the schema.") |
61 | 61 |
|
62 |
| - table_name: str |
63 |
| - """The name of the table.""" |
| 62 | + table_name: str = Field(description="The name of the table.") |
64 | 63 |
|
65 |
| - name: str |
66 |
| - """The name of the column.""" |
| 64 | + column_name: str = Field(description="The name of the column.") |
67 | 65 |
|
68 | 66 |
|
69 |
| -@dataclass(slots=True, kw_only=True) |
70 |
| -class LinksCollection[T: Link]: |
71 |
| - """A collection of links to documentation of a specific entity.""" |
| 67 | +class LinksCollection[T: Link](BaseModel): |
| 68 | + """A collection of links to documentation of a specific entity. |
72 | 69 |
|
73 |
| - links: list[T] |
74 |
| - """The documentation links.""" |
| 70 | + This is a Pydantic model to facilitate the parsing of SQLAlchemy queries |
| 71 | + directly into the domain. |
| 72 | + """ |
| 73 | + |
| 74 | + links: list[T] = Field(description="The documentation links.") |
75 | 75 |
|
76 | 76 |
|
77 |
| -@dataclass(slots=True, kw_only=True) |
78 | 77 | class SdmSchemaLinksCollection(LinksCollection[SdmSchemaLink]):
|
79 | 78 | """A collection of links to an SDM schema."""
|
80 | 79 |
|
81 |
| - schema_name: str |
82 |
| - """The name of the schema.""" |
| 80 | + domain: Literal["sdm"] = Field( |
| 81 | + description="The links domain of the entity." |
| 82 | + ) |
| 83 | + |
| 84 | + entity_type: Literal["schema"] = Field( |
| 85 | + description="The type of the entity in the domain." |
| 86 | + ) |
| 87 | + |
| 88 | + schema_name: str = Field(description="The name of the schema.") |
83 | 89 |
|
84 | 90 |
|
85 |
| -@dataclass(slots=True, kw_only=True) |
86 | 91 | class SdmTableLinksCollection(LinksCollection[SdmTableLink]):
|
87 | 92 | """A collection of links to an SDM table."""
|
88 | 93 |
|
89 |
| - schema_name: str |
90 |
| - """The name of the schema.""" |
| 94 | + domain: Literal["sdm"] = Field( |
| 95 | + description="The links domain of the entity." |
| 96 | + ) |
| 97 | + |
| 98 | + entity_type: Literal["table"] = Field( |
| 99 | + description="The type of the entity in the domain." |
| 100 | + ) |
| 101 | + |
| 102 | + schema_name: str = Field(description="The name of the schema.") |
| 103 | + |
| 104 | + table_name: str = Field(description="The name of the table.") |
91 | 105 |
|
92 |
| - table_name: str |
93 |
| - """The name of the table.""" |
| 106 | + tap_table_index: int | None = Field( |
| 107 | + description="The sorting index of the table in the TAP schema." |
| 108 | + ) |
94 | 109 |
|
95 | 110 |
|
96 |
| -@dataclass(slots=True, kw_only=True) |
97 | 111 | class SdmColumnLinksCollection(LinksCollection[SdmColumnLink]):
|
98 | 112 | """A collection of links to SDM columns."""
|
99 | 113 |
|
100 |
| - schema_name: str |
101 |
| - """The name of the schema.""" |
| 114 | + domain: Literal["sdm"] = Field( |
| 115 | + description="The links domain of the entity." |
| 116 | + ) |
| 117 | + |
| 118 | + entity_type: Literal["column"] = Field( |
| 119 | + description="The type of the entity in the domain." |
| 120 | + ) |
| 121 | + |
| 122 | + schema_name: str = Field(description="The name of the schema.") |
| 123 | + |
| 124 | + table_name: str = Field(description="The name of the table.") |
| 125 | + |
| 126 | + column_name: str = Field(description="The name of the column.") |
| 127 | + |
| 128 | + tap_column_index: int | None = Field( |
| 129 | + description="The sorting index of the column in the TAP schema." |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +SdmLinksCollection = RootModel[ |
| 134 | + Annotated[ |
| 135 | + SdmSchemaLinksCollection |
| 136 | + | SdmTableLinksCollection |
| 137 | + | SdmColumnLinksCollection, |
| 138 | + Field(discriminator="entity_type"), |
| 139 | + ] |
| 140 | +] |
| 141 | +"""A generic collection of SDM links for any SDM entity type. |
102 | 142 |
|
103 |
| - table_name: str |
104 |
| - """The name of the table.""" |
| 143 | +Use this root model to parse SQLAlchemy query results when the domain type |
| 144 | +can be any of the three SDM entity types: schema, table, or column. |
105 | 145 |
|
106 |
| - column_name: str |
107 |
| - """The name of the column.""" |
| 146 | +Access the underlying collection using the ``root`` attribute. |
| 147 | +""" |
0 commit comments