Skip to content

Commit

Permalink
docs: update schema customization document (#47)
Browse files Browse the repository at this point in the history
Signed-off-by: ktro2828 <[email protected]>
  • Loading branch information
ktro2828 authored Nov 21, 2024
1 parent 6a966c2 commit 33923b7
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions docs/tutorials/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ You can customize schema classes on your own code, if you need for some reasons.
For example, you can make `Attribute` allow `attribute.json` not to require `description` field as follows:

```python title="custom_attribute.py"
from dataclasses import dataclass
from attrs import define, field
from typing import Any

from typing_extensions import Self
Expand All @@ -16,27 +16,16 @@ from t4_devkit.schema import SCHEMAS, SchemaName, SchemaBase
from t4_devkit.common.io import load_json


@dataclass
@define
@SCHEMAS.register(SchemaName.ATTRIBUTE, force=True)
class CustomAttribute(SchemaBase):
"""Custom Attribute class ignoring if there is no description field.
Note that `description` field is mandatory by the original definition.
"""Custom Attribute class ignoring if there is no `description` field.
Note that `description` field is mandatory in the original `Attribute` class.
`@SCHEMAS.register(SchemaName.ATTRIBUTE, force=True)` performs that
it forces to update the attribute table in the schema registry.
"""

token: str
name: str
description: str | None

@classmehod
def from_json(cls, filepath: str) -> list[Self]:
objs: list[Self] = []

record_list: list[dict[str, Any]] = load_json(filepath)
for record in record_list:
token: str = record["token"]
name: str = record["name"]
# Return None if record does not have description field
description: str | None = record.get("description")
objs.append(cls(token=token, name=name, description=description))
return objs
description: str | None = field(default=None)
```

0 comments on commit 33923b7

Please sign in to comment.