Skip to content

Commit

Permalink
Merge pull request #84 from yumemi-inc/feature/separate-type-definitions
Browse files Browse the repository at this point in the history
型定義をファイル毎に分割しました。
  • Loading branch information
es-kumagai authored Jun 20, 2024
2 parents 9b2704c + 98c03f8 commit fdb1f41
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 107 deletions.
18 changes: 18 additions & 0 deletions Sources/YumemiWeather/InternalValueRepresentations/Area.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Area.swift
//

enum Area: String, CaseIterable, Codable {
case sapporo = "Sapporo"
case sendai = "Sendai"
case niigata = "Niigata"
case kanazawa = "Kanazawa"
case tokyo = "Tokyo"
case nagoya = "Nagoya"
case osaka = "Osaka"
case hiroshima = "Hiroshima"
case kochi = "Kochi"
case fukuoka = "Fukuoka"
case kagoshima = "Kagoshima"
case naha = "Naha"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// AreaRequest.swift
//

import Foundation

struct AreaRequest: Decodable {
let areas: [String]
let date: Date
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// AreaResponse.swift
//

struct AreaResponse: Codable {
let area: Area
let info: Response
}
10 changes: 10 additions & 0 deletions Sources/YumemiWeather/InternalValueRepresentations/Request.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Request.swift
//

import Foundation

struct Request: Decodable {
let area: String
let date: Date
}
12 changes: 12 additions & 0 deletions Sources/YumemiWeather/InternalValueRepresentations/Response.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Response.swift
//

import Foundation

struct Response: Codable, Equatable {
let weatherCondition: String
let maxTemperature: Int
let minTemperature: Int
let date: Date
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// WeatherCondition.swift
//

enum WeatherCondition: String, CaseIterable {
case sunny
case cloudy
case rainy
}

70 changes: 70 additions & 0 deletions Sources/YumemiWeather/RandomizationFeatures/RandomResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// RandomResponse.swift
//
//
// Created by Tomohiro Kumagai on 2024/06/20.
//

import Foundation

extension YumemiWeather {

/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。
/// - Parameters:
/// - weatherCondition: 天気状況を表すenum
/// - maxTemperature: 最高気温
/// - minTemperature: 最低気温
/// - date: 日付
/// - Returns: Response構造体
static func makeRandomResponse(
weatherCondition: WeatherCondition? = nil,
maxTemperature: Int? = nil,
minTemperature: Int? = nil,
date: Date? = nil
) -> Response {
return makeRandomResponse(using: &ControllableGenerator.shared, weatherCondition: weatherCondition, maxTemperature: maxTemperature, minTemperature: minTemperature)
}

/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。
/// - Parameters:
/// - weatherCondition: 天気状況を表すenum
/// - maxTemperature: 最高気温
/// - minTemperature: 最低気温
/// - date: 日付
/// - Returns: Response構造体
static func makeRandomResponse(
using generator: inout some RandomNumberGenerator,
weatherCondition: WeatherCondition? = nil,
maxTemperature: Int? = nil,
minTemperature: Int? = nil,
date: Date? = nil
) -> Response {
let weatherCondition = weatherCondition ?? .random(using: &generator)
let maxTemperature = maxTemperature ?? .random(in: 10...40, using: &generator)
let minTemperature = minTemperature ?? .random(in: -40..<maxTemperature, using: &generator)
let date = date ?? Date()

return Response(
weatherCondition: weatherCondition.rawValue,
maxTemperature: maxTemperature,
minTemperature: minTemperature,
date: date
)
}
}

extension WeatherCondition {

/// 天候をランダムで取得します。
/// - Returns: なにかしらの天候を返します。
static func random() -> Self {
random(using: &ControllableGenerator.shared)
}

/// 天候をランダムで取得します。
/// - Parameter generator: ランダムで取得するのに使う乱数生成期です。
/// - Returns: なにかしらの天候を返します。
static func random(using generator: inout some RandomNumberGenerator) -> Self {
allCases.randomElement(using: &generator)!
}
}
82 changes: 0 additions & 82 deletions Sources/YumemiWeather/YumemiWeather.swift
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
import Foundation

struct Request: Decodable {
let area: String
let date: Date
}

struct Response: Codable, Equatable {
let weatherCondition: String
let maxTemperature: Int
let minTemperature: Int
let date: Date
}

enum WeatherCondition: String, CaseIterable {
case sunny
case cloudy
case rainy
}

extension WeatherCondition {

/// 天候をランダムで取得します。
/// - Returns: なにかしらの天候を返します。
static func random() -> Self {
random(using: &ControllableGenerator.shared)
}

/// 天候をランダムで取得します。
/// - Parameter generator: ランダムで取得するのに使う乱数生成期です。
/// - Returns: なにかしらの天候を返します。
static func random(using generator: inout some RandomNumberGenerator) -> Self {
allCases.randomElement(using: &generator)!
}
}

public enum YumemiWeatherError: Error {
case invalidParameterError
case unknownError
}

/// 天気予報を取得する擬似天気予報 API です。
///
/// ゆめみ iOS 研修用の実装であり、実際の天気予報ではなくランダムな天気予報が得られます。
Expand Down Expand Up @@ -225,47 +186,4 @@ extension YumemiWeather {
encoder.dateEncodingStrategy = .formatted(dateFormatter)
return encoder
}()

/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。
/// - Parameters:
/// - weatherCondition: 天気状況を表すenum
/// - maxTemperature: 最高気温
/// - minTemperature: 最低気温
/// - date: 日付
/// - Returns: Response構造体
static func makeRandomResponse(
weatherCondition: WeatherCondition? = nil,
maxTemperature: Int? = nil,
minTemperature: Int? = nil,
date: Date? = nil
) -> Response {
return makeRandomResponse(using: &ControllableGenerator.shared, weatherCondition: weatherCondition, maxTemperature: maxTemperature, minTemperature: minTemperature)
}

/// 引数の値でResponse構造体を作成する。引数がnilの場合はランダムに値を作成する。
/// - Parameters:
/// - weatherCondition: 天気状況を表すenum
/// - maxTemperature: 最高気温
/// - minTemperature: 最低気温
/// - date: 日付
/// - Returns: Response構造体
static func makeRandomResponse(
using generator: inout some RandomNumberGenerator,
weatherCondition: WeatherCondition? = nil,
maxTemperature: Int? = nil,
minTemperature: Int? = nil,
date: Date? = nil
) -> Response {
let weatherCondition = weatherCondition ?? .random(using: &generator)
let maxTemperature = maxTemperature ?? .random(in: 10...40, using: &generator)
let minTemperature = minTemperature ?? .random(in: -40..<maxTemperature, using: &generator)
let date = date ?? Date()

return Response(
weatherCondition: weatherCondition.rawValue,
maxTemperature: maxTemperature,
minTemperature: minTemperature,
date: date
)
}
}
8 changes: 8 additions & 0 deletions Sources/YumemiWeather/YumemiWeatherError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// YumemiWeatherError.swift
//

public enum YumemiWeatherError: Error {
case invalidParameterError
case unknownError
}
25 changes: 0 additions & 25 deletions Sources/YumemiWeather/YumemiWeatherList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@

import Foundation

struct AreaRequest: Decodable {
let areas: [String]
let date: Date
}

struct AreaResponse: Codable {
let area: Area
let info: Response
}

public enum Area: String, CaseIterable, Codable {
case sapporo = "Sapporo"
case sendai = "Sendai"
case niigata = "Niigata"
case kanazawa = "Kanazawa"
case tokyo = "Tokyo"
case nagoya = "Nagoya"
case osaka = "Osaka"
case hiroshima = "Hiroshima"
case kochi = "Kochi"
case fukuoka = "Fukuoka"
case kagoshima = "Kagoshima"
case naha = "Naha"
}

public extension YumemiWeather {

/// 天気予報一覧を読み込む API の JSON Version です。
Expand Down

0 comments on commit fdb1f41

Please sign in to comment.