Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

既存コードをAPI Guidelineに準拠させる #72

Merged
merged 25 commits into from
May 23, 2024

Conversation

daikiumehara
Copy link
Contributor

@daikiumehara daikiumehara commented Apr 30, 2024

変更点

API Guidelineに準拠

  • Areaのenumをupper camel caseからlower camel casaeに変更
  • callback〇〇のメソッドからcallbackの文言を削除

リファクタ

  • エラーのキャスト処理を修正
  • makeResponseメソッドを統一

@daikiumehara daikiumehara changed the title Feature/apply api guideline API Guidelineに準拠させる Apr 30, 2024
@daikiumehara daikiumehara changed the title API Guidelineに準拠させる 既存コードをAPI Guidelineに準拠させる Apr 30, 2024
@daikiumehara daikiumehara self-assigned this Apr 30, 2024
Copy link
Contributor Author

@daikiumehara daikiumehara left a comment

Choose a reason for hiding this comment

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

個人的に気になった箇所を修正してみました
commit分けてあり、簡単に変更取り消せるので、変更がおかしければバシバシ言っていただけると🙏

@@ -183,14 +185,14 @@ final public class YumemiWeather {
/// - Parameters:
/// - jsonString: 地域と日付を含む JSON 文字列
/// - completion: 完了コールバック
public static func callbackFetchWeather(_ jsonString: String, completion: @escaping (Result<String, YumemiWeatherError>) -> Void) {
public static func fetchWeather(_ jsonString: String, completion: @escaping (Result<String, YumemiWeatherError>) -> Void) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

引数にcompletionが用意されているため、callbackという文字は意味が重なっているように感じたので削除しました👀

Copy link
Contributor

Choose a reason for hiding this comment

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

良好 (good)-badge
もしかすると元々はコールバック版であることを分かりやする狙いがあったのかもしれないですけれど、こちらの方が自然で良い気がします。

Copy link
Contributor

Choose a reason for hiding this comment

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

質問 (ask)-badge

⋯と思ったのですけれど、周囲を見渡してみると現状では次のようにバランスが崩れる印象でした。

  • 同期版 — syncFetchWeather
  • 非同期版 — asyncFetchWeather
  • コールバック版 — fetchWeather(旧 callbackFetchWeather

名前的には callback が付かない方が綺麗に感じるのですけれど、そうなると同期版と非同期版もそのように揃えたくても async メソッドと通常メソッドでは同じ関数シグネチャを持てない問題があり、実現できないところです。

とはいえ callbackFetchWeather に戻すのは微妙な気がします。どのようにするのが良いでしょうね。

Copy link
Contributor

Choose a reason for hiding this comment

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

提案 (suggestion)-badge

こちら、アイデアの方向性は良いものの方針を定めにくいので、今回はいったん名称を callbackFetchWeather に戻して、改めて別の議題に切り出して考えると良いアイデアが見つかるかもしれないです。

Issue として #77 に登録して、この部分はいったん元に戻してみますね。

}
catch let error where error is YumemiWeatherError {
completion(Result.failure(error as! YumemiWeatherError))
catch let error as YumemiWeatherError {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

エラー処理のキャスト処理をコンパクトにしました

Copy link
Contributor

Choose a reason for hiding this comment

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

良好 (good)-badge
上手な型キャストパターンの扱い方です。

Sources/YumemiWeather/YumemiWeatherList.swift Outdated Show resolved Hide resolved
@ykws ykws requested a review from a team April 30, 2024 06:11
@daikiumehara
Copy link
Contributor Author

これPackageしか編集してないからサンプルコード動くように修正しないといけない💦

@daikiumehara
Copy link
Contributor Author

daikiumehara commented Apr 30, 2024

もっと小さいスコープで見ることが重要そうなので、
色々修正入るかもです🙇‍♂️
(細かくPR出した方が良さそうなので、別でPR作るかもです)

@es-kumagai
Copy link
Collaborator

そもそもの Example のテストコードに不備があったので修正しますね。

@yumemi-kumagai
Copy link
Contributor

ここまでで、現状のコードでの Example プロダクトのテストがひとまず通るようになりました。

catch let error where error is YumemiWeatherError {
completion(Result.failure(error as! YumemiWeatherError))
catch let error as YumemiWeatherError {
completion(.failure(error))
}
catch {
fatalError()
Copy link
Contributor

Choose a reason for hiding this comment

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

推奨 (recommend)-badge
せっかくなので、通常は有り得ないとはいえ無言で fatalError するのは控えておくと良いかもしれません。

Suggested change
fatalError()
fatalError("An unexpected error has occurred: \(error.localizedDescription)")

daikiumehara and others added 11 commits May 1, 2024 11:26
現状の WeatherViewController には反映完了を検出する術がなさそうだったため、やや強引かつ不確実なテストコードになりました。
省略推奨についての確かな根拠は見つけられませんでした。以前に The Swift Programming Language で「通常は省略する」という記載があった気がしたのですけれど、探しても見つけられなかったため、省略しない方針もアリかもしれません。その場合はこのコミットを Revert しましょう。
Copy link
Collaborator

@es-kumagai es-kumagai left a comment

Choose a reason for hiding this comment

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

難度の高い次の2項目は別の PR に切り出すことで、この PR はいったん、既存コードの細かな調整のひとつとして収めて良さそうな印象を持ちました。

  • Areaのenumをupper camel caseからlower camel casaeに変更
  • callback〇〇のメソッドからcallbackの文言を削除

@es-kumagai
Copy link
Collaborator

もっと小さいスコープで見ることが重要そうなので、 色々修正入るかもです🙇‍♂️ (細かくPR出した方が良さそうなので、別でPR作るかもです)

こちら賛成で、他の気になる箇所については粒度を小さめに PR を出していくと良いかもしれないですね。

…沿った方法に変更しました。

意図した乱数を作り出すために makeRandomResponse に seed を渡す設計になっていましたが、現在の標準ライブラリーでは some RandomNumberGenerator を渡すのが一般的のため、それに合わせました。

また、seed によって乱数が制御されることは ControllableGenerator 器固有の仕様なため、makeRandomResponse には渡さず、それを ControllableGenerator 自身が制御するようにしました。この際、内部で使われる srand48 関数はグローバルに影響するため、イニシャライザーで呼び出すとほかのインスタンスにも影響します。その性質を考慮して、ControllableGenerator はシングルトンで実装するようにしました。

加えて、seed のリセットも ControllableGenerator の範疇と思われるため、必要な値を受け取って適切にシードをリセットする機能もここに移動しました。

その他として、シングルトンを static var で保持させています。これは乱数生成器を各種 API が inout で受け取る仕様になっているため、そこにそのまま渡せるようにするための措置です。自由に書き換え可能にはなりますが、そもそもインスタンスが1つしか存在せず、そのイニシャライザーはプライベートで保護されているため、予期しないインスタンスの入れ替え操作は起こりません。

let response = try YumemiWeather.decoder.decode([AreaResponse].self, from: responseData)

XCTAssertEqual(response.map(\.area), Area.allCases)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

こちら空の場合にすべて取得されるのが正しい挙動ですかね?
であればDocument commetに追記しておいた方が良さそうと思いました!

Copy link
Contributor

Choose a reason for hiding this comment

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

良好 (good)-badge
たしかに⋯!

地域指定が空の場合にすべての地域が得られることが正しい意図かはわからないものの、課題で使える可能性があるのは TableView の課題で、それが意図されていることが a2fe1a8 から予想できそうです。

TableView の課題新設と併せて List バージョンの API が用意されたのと、そのときに合わせて実装されたテストコードでも空だったときに全てを返すテストが用意されているのを踏まえると、妥当と思って良さそうな気がします。

そうするとたしかに、ドキュメントコメントに明記しておくと良さそうですね。

Copy link
Contributor

Choose a reason for hiding this comment

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

6d986e1 で対応しました。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ちゃんとコード読めてなかったです🙏
対応ありがとうございます!

yumemi-kumagai and others added 3 commits May 9, 2024 22:22
…ました。擬似 API であることの記述を各 API ではなく YumemiWeather 型のドキュメントコメントに記載しました。それに伴い、単体取得用の API でのドキュメントコメントも調整し、全体に統一感を持たせました。
YumemiWeather API が無作為にエラーを返す頻度をコントロール可能にしました。
@es-kumagai es-kumagai merged commit 55bdcfb into main May 23, 2024
2 checks passed
@es-kumagai es-kumagai deleted the feature/apply_api_guideline branch May 23, 2024 06:38
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.

None yet

3 participants