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

fix: add explicit conformance for Equatable and Hashable on Header, Comparable on URLQueryItem #451

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions Packages/ClientRuntime/Sources/Networking/Http/Headers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import AwsCommonRuntimeKit

public struct Headers: Equatable, Hashable {
public struct Headers: Hashable {
public var headers: [Header] = []

/// Creates an empty instance.
Expand Down Expand Up @@ -161,6 +161,17 @@ public struct Headers: Equatable, Hashable {
}
}

extension Headers: Equatable {
/// Returns a boolean value indicating whether two values are equal irrespective of order.
/// - Parameters:
/// - lhs: The first `Headers` to compare.
/// - rhs: The second `Headers` to compare.
/// - Returns: `true` if the two values are equal irrespective of order, otherwise `false`.
public static func == (lhs: Headers, rhs: Headers) -> Bool {
return lhs.headers.sorted() == rhs.headers.sorted()
}
}

extension Array where Element == Header {
/// Case-insensitively finds the index of an `Header` with the provided name, if it exists.
func index(of name: String) -> Int? {
Expand All @@ -175,7 +186,7 @@ extension Array where Element == Header {
}
}

public struct Header: Equatable, Hashable {
public struct Header: Hashable {
public var name: String
public var value: [String]

Expand All @@ -190,6 +201,23 @@ public struct Header: Equatable, Hashable {
}
}

extension Header: Equatable {
public static func == (lhs: Header, rhs: Header) -> Bool {
return lhs.name == rhs.name && lhs.value.sorted() == rhs.value.sorted()
}
}

extension Header: Comparable {
/// Compares two `Header` instances by name.
/// - Parameters:
/// - lhs: The first `Header` to compare.
/// - rhs: The second `Header` to compare.
/// - Returns: `true` if the first `Header`'s name is less than the second `Header`'s name, otherwise `false`.
public static func < (lhs: Header, rhs: Header) -> Bool {
return lhs.name < rhs.name
}
}

extension Headers {
func toHttpHeaders() -> HttpHeaders {
let httpHeaders = HttpHeaders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@

import struct Foundation.URLQueryItem
public typealias URLQueryItem = Foundation.URLQueryItem

extension URLQueryItem: Comparable {
/// Compares two `URLQueryItem` instances by their `name` property.
/// - Parameters:
/// - lhs: The first `URLQueryItem` to compare.
/// - rhs: The second `URLQueryItem` to compare.
/// - Returns: `true` if the `name` property of `lhs` is less than the `name` property of `rhs`.
public static func < (lhs: URLQueryItem, rhs: URLQueryItem) -> Bool {
lhs.name < rhs.name
}
}