Skip to content

Commit

Permalink
add swift lint and fix some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fumito-ito committed Mar 19, 2024
1 parent 3db70f7 commit e4b1c29
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 23 deletions.
112 changes: 112 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
excluded:
- ${PWD}/DerivedData
- ${PWD}/.build
- Tests
- Example.swiftpm

disabled_rules:
- discarded_notification_center_observer
- notification_center_detachment
- orphaned_doc_comment
- todo
- unused_capture_list
- opening_brace
- trailing_comma

analyzer_rules:
- unused_import

opt_in_rules:
- array_init
- attributes
- closure_end_indentation
- closure_spacing
- collection_alignment
- colon # promote to error
- convenience_type
- discouraged_object_literal
- empty_collection_literal
- empty_count
- empty_string
- enum_case_associated_values_count
- fatal_error_message
- first_where
- force_unwrapping
- implicitly_unwrapped_optional
- indentation_width
- last_where
- legacy_random
- literal_expression_end_indentation
- multiline_arguments
- multiline_function_chains
- multiline_literal_brackets
- multiline_parameters
- multiline_parameters_brackets
- operator_usage_whitespace
- overridden_super_call
- pattern_matching_keywords
- prefer_self_type_over_type_of_self
- redundant_nil_coalescing
- redundant_type_annotation
- strict_fileprivate
- toggle_bool
- trailing_closure
- unneeded_parentheses_in_closure_argument
- vertical_whitespace_closing_braces
- vertical_whitespace_opening_braces
- yoda_condition


custom_rules:
array_constructor:
name: "Array/Dictionary initializer"
regex: '[let,var] .+ = (\[.+\]\(\))'
capture_group: 1
message: "Use explicit type annotation when initializing empty arrays and dictionaries"
severity: warning


attributes:
always_on_same_line:
- "@IBSegueAction"
- "@IBAction"
- "@NSManaged"
- "@objc"

force_cast: warning
force_try: warning
function_body_length:
warning: 100

legacy_hashing: error

identifier_name:
excluded:
- i
- id
- x
- y
- z

indentation_width:
indentation_width: 4

line_length:
warning: 150
ignores_urls: true
ignores_function_declarations: true
ignores_comments: true

multiline_arguments:
first_argument_location: next_line
only_enforce_after_first_closure_on_first_line: true

private_over_fileprivate:
validate_extensions: true

trailing_whitespace:
ignores_empty_lines: false
ignores_comments: true

vertical_whitespace:
max_empty_lines: 2
86 changes: 86 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"pins" : [
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit.git",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "7892a123f7e8d0fe62f9f03728b17bbd4f94df5c",
"version" : "1.8.1"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/SourceKitten.git",
"state" : {
"revision" : "b6dc09ee51dfb0c66e042d2328c017483a1a5d56",
"version" : "0.34.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531",
"version" : "1.2.3"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version" : "509.0.2"
}
},
{
"identity" : "swiftlint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/realm/SwiftLint",
"state" : {
"revision" : "f17a4f9dfb6a6afb0408426354e4180daaf49cee",
"version" : "0.54.0"
}
},
{
"identity" : "swiftytexttable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scottrhoyt/SwiftyTextTable.git",
"state" : {
"revision" : "c6df6cf533d120716bff38f8ff9885e1ce2a4ac3",
"version" : "0.9.0"
}
},
{
"identity" : "swxmlhash",
"kind" : "remoteSourceControl",
"location" : "https://github.com/drmohundro/SWXMLHash.git",
"state" : {
"revision" : "a853604c9e9a83ad9954c7e3d2a565273982471f",
"version" : "7.0.2"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "0d9ee7ea8c4ebd4a489ad7a73d5c6cad55d6fed3",
"version" : "5.0.6"
}
}
],
"version" : 2
}
7 changes: 6 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ let package = Package(
name: "AnthropicSwiftSDK",
targets: ["AnthropicSwiftSDK"]),
],
dependencies: [
.package(url: "https://github.com/realm/SwiftLint", .upToNextMajor(from: "0.54.0")),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "AnthropicSwiftSDK"),
name: "AnthropicSwiftSDK",
plugins: [.plugin(name: "SwiftLintPlugin", package: "SwiftLint")]
),
.testTarget(
name: "AnthropicSwiftSDKTests",
dependencies: ["AnthropicSwiftSDK"]),
Expand Down
2 changes: 1 addition & 1 deletion Sources/AnthropicSwiftSDK/Anthropic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
public final class Anthropic {
/// Messages API Interface
public let messages: Messages

/// Construction of SDK
/// - Parameter apiKey: API key to access Anthropic API.
public init(apiKey: String) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AnthropicSwiftSDK/AnthropicAPIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum AnthropicAPIError: String, Decodable, Error {
case apiError = "api_error"
case overloadedError = "overloaded_error"
case unknown

/// Description of Anthropic API errors.
///
/// for more detail, see https://docs.anthropic.com/claude/reference/errors
Expand Down
1 change: 1 addition & 0 deletions Sources/AnthropicSwiftSDK/AnthropicVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation

enum AnthropicVersion {
// swiftlint:disable:next identifier_name
case v2023_06_01
case custom(String)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/AnthropicSwiftSDK/Entity/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension Content: Decodable {
let image = try container.decode(ImageContent.self, forKey: .source)
self = .image(image)
default:
fatalError()
fatalError("Unknown content type detected")
}
}
}
3 changes: 3 additions & 0 deletions Sources/AnthropicSwiftSDK/Entity/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import Foundation
/// See [models](https://docs.anthropic.com/claude/docs/models-overview) for additional details and options.
public enum Model {
/// Most powerful model for highly complex tasks
// swiftlint:disable:next identifier_name
case claude_3_Opus
/// Ideal balance of intelligence and speed for enterprise workloads
// swiftlint:disable:next identifier_name
case claude_3_Sonnet
/// Fastest and most compact model for near-instant responsiveness
// swiftlint:disable:next identifier_name
case claude_3_Haiku
/// Custom Model
case custom(String)
Expand Down
5 changes: 3 additions & 2 deletions Sources/AnthropicSwiftSDK/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public struct Messages {
throw AnthropicAPIError(fromHttpStatusCode: httpResponse.statusCode)
}

return try AnthropicJSONDecoder.decode(MessagesResponse.self, from: data)
return try anthropicJSONDecoder.decode(MessagesResponse.self, from: data)
}

public func streamMessage(
Expand Down Expand Up @@ -115,6 +115,7 @@ public struct Messages {
)
}

// swiftlint:disable:next cyclomatic_complexity
public func streamMessage(
_ messages: [Message],
model: Model = .claude_3_Opus,
Expand Down Expand Up @@ -159,7 +160,7 @@ public struct Messages {

return AsyncThrowingStream.init { continuation in
let task = Task {
var currentEvent: StreamingEvent? = nil
var currentEvent: StreamingEvent?
for try await line in data.lines {
do {
let lineType = try StreamingResponseParser.parse(line: line)
Expand Down
8 changes: 4 additions & 4 deletions Sources/AnthropicSwiftSDK/Network/AnthropicAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct AnthropicAPIClient {
}()

private var headers: [String: String] {
var headers = [String: String]()
var headers: [String: String] = [:]

anthropicHeaderProvider.getAnthropicAPIHeaders().forEach { key, value in
headers.updateValue(value, forKey: key)
Expand All @@ -70,7 +70,7 @@ struct AnthropicAPIClient {

private var urlRequest: URLRequest {
guard let url = requestURL else {
fatalError()
fatalError("APIClient must have requestURL")
}

return URLRequest(url: url)
Expand All @@ -87,7 +87,7 @@ struct AnthropicAPIClient {
self.authenticationHeaderProvider = authenticationHeaderProvider
self.session = session
}

/// Send messages API request. This method receives HTTP response from API.
///
/// For more detail, see https://docs.anthropic.com/claude/reference/messages_post.
Expand All @@ -103,7 +103,7 @@ struct AnthropicAPIClient {

return try await session.data(for: request)
}

/// Send messages API request. This method read the messages api response sequentially.
///
/// For more detail, see https://docs.anthropic.com/claude/reference/messages-streaming.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct DefaultAnthropicHeaderProvider: AnthropicHeaderProvider {

private let betaDescription = "messages-2023-12-15"

func getAnthropicAPIHeaders() -> [String : String] {
func getAnthropicAPIHeaders() -> [String: String] {
var headers: [String: String] = [
"anthropic-version": version.stringfy,
"content-type": contentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public protocol AuthenticationHeaderProvider {
}

struct APIKeyAuthenticationHeaderProvider: AuthenticationHeaderProvider {
func getAuthenticationHeaders() -> [String : String] {
func getAuthenticationHeaders() -> [String: String] {
["x-api-key": apiKey]
}

private let apiKey: String

init(apiKey: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import Foundation

struct StreamingDataLineParser {
enum StreamingDataLineParser {
static func parse<T>(dataLine line: String) throws -> T where T: StreamingResponse {
// data line has `data: json` structure and json value might have `data: ` string
let jsonString = String(line.dropFirst(6))
guard let data = jsonString.data(using: .utf8) else {
throw ClientError.cannotHandleDataOfDataLine(jsonString)
}

return try AnthropicJSONDecoder.decode(T.self, from: data)
return try anthropicJSONDecoder.decode(T.self, from: data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

struct StreamingEventLineParser {
enum StreamingEventLineParser {
static func parse(eventLine line: String) throws -> StreamingEvent {
// event line has `event: event_name` structure
let eventName = line.replacingOccurrences(of: "event: ", with: "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum StreamingResponseLineType {
case data
}

struct StreamingResponseParser {
enum StreamingResponseParser {
/// line is these kinds
/// - `event: event_name`
/// - `data: json`
Expand Down
Loading

0 comments on commit e4b1c29

Please sign in to comment.