Skip to content

refactor: data modelを分離#256

Merged
Hiroshiba merged 12 commits intoVOICEVOX:masterfrom
qwerty2501:refacter/data-model
Jan 2, 2022
Merged

refactor: data modelを分離#256
Hiroshiba merged 12 commits intoVOICEVOX:masterfrom
qwerty2501:refacter/data-model

Conversation

@qwerty2501
Copy link
Contributor

@qwerty2501 qwerty2501 commented Dec 29, 2021

内容

APIで返す型と内部で使う型が同じだとフィールドをおいそれと変えることが難しいので、型を分離する

Pros

APIのデータ構造を変えることなく内部で型を好きに変えることができる

Cons

  • 初見の人になんで似たような型があるのか疑問を持たれる
  • 初見の人になんで詰替え処理してるのか疑問を持たれる

その他

full_context_label.pyにある型もmodel.pyに統合してしまいたいが、変更量次第?

@qwerty2501
Copy link
Contributor Author

とりあえず分離したけど、model.pyの方の型はBaseModelとかFieldとかなくして、dataclassにしたほうが良さそう

@coveralls
Copy link

coveralls commented Dec 30, 2021

Pull Request Test Coverage Report for Build 1642267046

  • 102 of 109 (93.58%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.9%) to 87.5%

Changes Missing Coverage Covered Lines Changed/Added Lines %
voicevox_engine/webapi/fastapi_model.py 102 109 93.58%
Totals Coverage Status
Change from base Build 1633757091: 0.9%
Covered Lines: 784
Relevant Lines: 896

💛 - Coveralls

@github-actions
Copy link

github-actions bot commented Dec 30, 2021

Coverage Result

Resultを開く
Name Stmts Miss Cover
voicevox_engine/init.py 0 0 coverage-100%
voicevox_engine/acoustic_feature_extractor.py 85 0 coverage-100%
voicevox_engine/dev/synthesis_engine/init.py 2 0 coverage-100%
voicevox_engine/dev/synthesis_engine/mock.py 30 1 coverage-97%
voicevox_engine/full_context_label.py 162 3 coverage-98%
voicevox_engine/kana_parser.py 87 1 coverage-99%
voicevox_engine/model.py 64 6 coverage-91%
voicevox_engine/mora_list.py 4 0 coverage-100%
voicevox_engine/preset/Preset.py 12 0 coverage-100%
voicevox_engine/preset/PresetLoader.py 34 1 coverage-97%
voicevox_engine/preset/init.py 3 0 coverage-100%
voicevox_engine/synthesis_engine/init.py 5 0 coverage-100%
voicevox_engine/synthesis_engine/forwarder.py 76 66 coverage-13%
voicevox_engine/synthesis_engine/make_synthesis_engine.py 23 18 coverage-22%
voicevox_engine/synthesis_engine/synthesis_engine.py 108 0 coverage-100%
voicevox_engine/synthesis_engine/synthesis_engine_base.py 55 6 coverage-89%
voicevox_engine/utility/init.py 2 0 coverage-100%
voicevox_engine/utility/connect_base64_waves.py 35 3 coverage-91%
voicevox_engine/webapi/init.py 0 0 coverage-100%
voicevox_engine/webapi/fastapi_model.py 109 7 coverage-94%
TOTAL 896 112 coverage-88%

@qwerty2501 qwerty2501 marked this pull request as ready for review December 30, 2021 17:36
@qwerty2501
Copy link
Contributor Author

とりあえずデータ型を分離して、変換関数を実装しました
以下判断お願いします

  • この変更を取り込むべきか
  • model.pyの方の型はBaseModelとかFieldとかなくして、dataclassにするべきか
  • full_context_label.pyにある型もmodel.pyに統合するべきか(PR分けたほうが良さそう)

@Hiroshiba
Copy link
Member

この変更を取り込むべきか

たしかに変更量は結構大きめですが、取り込んじゃいましょう!!

ただもうちょっと実装の負担を減らせると嬉しいかなと思いました。
たとえばmora_modelを変換するためにfrom_mora_modelやto_mora_modelのglobal関数を実装すると、使用者が対応するコンバータを探す手間が増えることに気づきました。
なので各fastapi modelクラスにto_engineとfrom_engineのようなメソッドを付けちゃっても良いかもと感じました。
(汎用convert関数を作って中で型を見て変換、ということもできるのですが、こうするとpythonの型推論が壊れそうなので。。)

model.pyの方の型はBaseModelとかFieldとかなくして、dataclassにするべきか

dataclassにしちゃったほうが良いと思います。
ただ、Modelのプロパティ値とdataclassのプロパティ値は微妙に型が合わなそうなので、一旦ModelのままにしてFIXMEコメントを残すと良いのかなと感じました。

full_context_label.pyにある型もmodel.pyに統合するべきか(PR分けたほうが良さそう)

full_context_labelはOpenJTalkに引っ張られる仕様なため、エンジン用とはまた変わってきそうな気もします。
なのでそのままの方が良いかもと思いました。

@qwerty2501
Copy link
Contributor Author

qwerty2501 commented Dec 30, 2021

なので各fastapi modelクラスにto_engineとfrom_engineのようなメソッドを付けちゃっても良いかもと感じました。

~engineだと何の型にするのか想像しにくいのではないでしょうか? model.py にあるのだからto_model,from_modelにするか、modelが紛らわしいのならmodel.pyの名前を例えばdomain.pyにし、to_domain,from_domainにしたほうが良さそうに感じました。
また、List系だとクラスに変換関数を生やすのはちょっと違和感があるんですがどうしましょうか。

ただ、Modelのプロパティ値とdataclassのプロパティ値は微妙に型が合わなそうなので、一旦ModelのままにしてFIXMEコメントを残すと良いのかなと感じました。

👍

full_context_labelはOpenJTalkに引っ張られる仕様なため、エンジン用とはまた変わってきそうな気もします。
なのでそのままの方が良いかもと思いました。

このPRではやりませんが、残すのであればそれらの型名にプレフィックスでOpenJTalkをつけたほうが良さそうですね。似たような型があるので混同しそうです

@Hiroshiba
Copy link
Member

Hiroshiba commented Dec 30, 2021

~engineだと何の型にするのか想像しにくいのではないでしょうか?

例えばMoraクラスにto_engine()メソッド(Mora.to_engine())を作って、エンジン用のMora型に変えるというのを想像していました!

qwerty2501 added a commit to qwerty2501/voicevox_engine that referenced this pull request Dec 31, 2021
…o_engineとして実装した

若干名前に違和感はあるものの、特段こだわりがあったわけではないのでとりあえず修正
List系はあってもなくても書く量が変わらないのとクラスメソッドとして生えていたら不自然だったので削除した
…o_engineとして実装した

若干名前に違和感はあるものの、特段こだわりがあったわけではないのでとりあえず修正
cyclic importになったのでもとの実装は削除した
List系はあってもなくても書く量が変わらないのとクラスメソッドとして生えていたら不自然だったので削除した
@qwerty2501
Copy link
Contributor Author

from_engine,to_engineメソッドを各クラスに実装しましたcyclic importになったのでもとの実装は消しました

qwerty2501 added a commit to qwerty2501/voicevox_engine that referenced this pull request Dec 31, 2021
…o_engineとして実装した

cyclic importになったのでもとの実装は削除した
List系はあってもなくても書く量が変わらないのとクラスメソッドとして生えていたら不自然だったので削除した
@qwerty2501 qwerty2501 force-pushed the refacter/data-model branch 2 times, most recently from 17f9c31 to 8cf983d Compare December 31, 2021 08:37
@qwerty2501 qwerty2501 changed the title refacter: data modelを分離 refactor: data modelを分離 Dec 31, 2021
Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大丈夫そうです!
後世に伝えるにあたってちょっと変えれそうなところをコメントしてみました。

これでわからなければコメントを追加する必要あり
Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

@Hiroshiba Hiroshiba requested review from aoirint and y-chan January 1, 2022 17:18
Copy link
Member

@aoirint aoirint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

@Hiroshiba Hiroshiba merged commit ff00ad6 into VOICEVOX:master Jan 2, 2022
@qwerty2501 qwerty2501 deleted the refacter/data-model branch January 2, 2022 06:59
@qwerty2501 qwerty2501 restored the refacter/data-model branch January 4, 2022 01:18
takana-v added a commit to takana-v/voicevox_engine that referenced this pull request Jan 4, 2022
Hiroshiba pushed a commit that referenced this pull request Jan 4, 2022
@takana-v takana-v mentioned this pull request May 5, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants