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

Let uniffi add Sendable conformation to endpoint response types #490

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WpRequestExecutor(
request.method().toString(),
request.body()?.contents()?.toRequestBody()
)
request.headerMap().toMap().forEach { (key, values) ->
request.headerMap().toMultiMap().forEach { (key, values) ->
values.forEach { value ->
requestBuilder.addHeader(key, value)
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class WpRequestExecutor(
method = mediaUploadRequest.method().toString(),
body = multipartBodyBuilder.build()
)
mediaUploadRequest.headerMap().toMap().forEach { (key, values) ->
mediaUploadRequest.headerMap().toMultiMap().forEach { (key, values) ->
values.forEach { value ->
requestBuilder.addHeader(key, value)
}
Expand Down
20 changes: 10 additions & 10 deletions native/swift/Sources/wordpress-api/Pagination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol PaginatableResponse: Sendable {

var data: [DataType] { get }

init(data: [DataType], headerMap: WpNetworkHeaderMap, nextPageParams: ParamsType?, prevPageParams: ParamsType?)
init(data: [DataType], headerMap: [String: [String]], nextPageParams: ParamsType?, prevPageParams: ParamsType?)
}

public protocol PaginationAwareExecutor: Sendable {
Expand Down Expand Up @@ -187,17 +187,17 @@ public struct PaginationSequence<ResponseType: PaginatableResponse>: AsyncSequen
}

// MARK: - Posts
extension PostsRequestListWithEditContextResponse: PaginatableResponse, @unchecked Sendable {
extension PostsRequestListWithEditContextResponse: PaginatableResponse {
public typealias ParamsType = PostListParams
public typealias DataType = PostWithEditContext
}

extension PostsRequestListWithViewContextResponse: PaginatableResponse, @unchecked Sendable {
extension PostsRequestListWithViewContextResponse: PaginatableResponse {
public typealias ParamsType = PostListParams
public typealias DataType = PostWithViewContext
}

extension PostsRequestListWithEmbedContextResponse: PaginatableResponse, @unchecked Sendable {
extension PostsRequestListWithEmbedContextResponse: PaginatableResponse {
public typealias ParamsType = PostListParams
public typealias DataType = PostWithEmbedContext
}
Expand All @@ -209,17 +209,17 @@ extension PostsRequestExecutor: PaginationAwareExecutor {
}

// MARK: - Media
extension MediaRequestListWithEditContextResponse: PaginatableResponse, @unchecked Sendable {
extension MediaRequestListWithEditContextResponse: PaginatableResponse {
public typealias ParamsType = MediaListParams
public typealias DataType = MediaWithEditContext
}

extension MediaRequestListWithViewContextResponse: PaginatableResponse, @unchecked Sendable {
extension MediaRequestListWithViewContextResponse: PaginatableResponse {
public typealias ParamsType = MediaListParams
public typealias DataType = MediaWithViewContext
}

extension MediaRequestListWithEmbedContextResponse: PaginatableResponse, @unchecked Sendable {
extension MediaRequestListWithEmbedContextResponse: PaginatableResponse {
public typealias ParamsType = MediaListParams
public typealias DataType = MediaWithEmbedContext
}
Expand All @@ -231,17 +231,17 @@ extension MediaRequestExecutor: PaginationAwareExecutor {
}

// MARK: - Users
extension UsersRequestListWithEditContextResponse: PaginatableResponse, @unchecked Sendable {
extension UsersRequestListWithEditContextResponse: PaginatableResponse {
public typealias ParamsType = UserListParams
public typealias DataType = UserWithEditContext
}

extension UsersRequestListWithViewContextResponse: PaginatableResponse, @unchecked Sendable {
extension UsersRequestListWithViewContextResponse: PaginatableResponse {
public typealias ParamsType = UserListParams
public typealias DataType = UserWithViewContext
}

extension UsersRequestListWithEmbedContextResponse: PaginatableResponse, @unchecked Sendable {
extension UsersRequestListWithEmbedContextResponse: PaginatableResponse {
public typealias ParamsType = UserListParams
public typealias DataType = UserWithEmbedContext
}
Expand Down
4 changes: 2 additions & 2 deletions native/swift/Sources/wordpress-api/WordPressAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public actor WordPressAPI {

public extension WpNetworkHeaderMap {
func toFlatMap() -> [String: String] {
self.toMap().mapValues { $0.joined(separator: ",") }
self.toMultiMap().mapValues { $0.joined(separator: ",") }
}
}

Expand All @@ -138,7 +138,7 @@ public extension WpNetworkRequest {
#if DEBUG
func debugPrint() {
print("\(method().rawValue) \(self.url())")
for (name, value) in self.headerMap().toMap() {
for (name, value) in self.headerMap().toMultiMap() {
print("\(name): \(value)")
}

Expand Down
2 changes: 1 addition & 1 deletion native/swift/Tests/wordpress-api/Support/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension WpNetworkHeaderMap {

extension PaginatableResponse {
static var empty: Self {
Self(data: [], headerMap: .empty, nextPageParams: nil, prevPageParams: nil)
Self(data: [], headerMap: [:], nextPageParams: nil, prevPageParams: nil)
}
}

Expand Down
14 changes: 7 additions & 7 deletions wp_api/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ impl WpNetworkHeaderMap {

// Splits the `header_value` by `,` then parses name & values into `HeaderName` & `HeaderValue`
fn build_header_name_value(
header_name: String,
header_value: String,
header_name: &str,
header_value: &str,
) -> Vec<Result<(HeaderName, HeaderValue), WpNetworkHeaderMapError>> {
header_value
.split(',')
Expand All @@ -274,7 +274,7 @@ impl WpNetworkHeaderMap {
}
} else {
Err(WpNetworkHeaderMapError::InvalidHeaderName {
header_name: header_name.clone(),
header_name: header_name.to_string(),
})
}
})
Expand All @@ -289,14 +289,14 @@ impl WpNetworkHeaderMap {
#[uniffi::export]
impl WpNetworkHeaderMap {
#[uniffi::constructor]
fn from_multi_map(
pub fn from_multi_map(
hash_map: HashMap<String, Vec<String>>,
) -> Result<Self, WpNetworkHeaderMapError> {
let inner = hash_map
.into_iter()
.flat_map(|(header_name, values)| {
values.into_iter().flat_map(move |header_value| {
Self::build_header_name_value(header_name.clone(), header_value)
Self::build_header_name_value(&header_name, &header_value)
})
})
.collect::<Result<HeaderMap, WpNetworkHeaderMapError>>()?;
Expand All @@ -308,13 +308,13 @@ impl WpNetworkHeaderMap {
let inner = hash_map
.into_iter()
.flat_map(|(header_name, header_value)| {
Self::build_header_name_value(header_name, header_value)
Self::build_header_name_value(&header_name, &header_value)
})
.collect::<Result<HeaderMap, WpNetworkHeaderMapError>>()?;
Ok(Self { inner })
}

fn to_map(&self) -> HashMap<String, Vec<String>> {
pub fn to_multi_map(&self) -> HashMap<String, Vec<String>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rename to match from_multi_map

let mut header_hashmap = HashMap::new();
self.inner.iter().for_each(|(k, v)| {
let v = String::from_utf8_lossy(v.as_bytes()).into_owned();
Expand Down
6 changes: 4 additions & 2 deletions wp_api_integration_tests/tests/test_posts_immut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use wp_api::posts::{
SparsePostFieldWithEmbedContext, SparsePostFieldWithViewContext, WpApiParamPostsOrderBy,
WpApiParamPostsSearchColumn, WpApiParamPostsTaxRelation,
};
use wp_api::request::WpNetworkHeaderMap;
use wp_api::tags::TagId;
use wp_api::{generate, WpApiParamOrder};
use wp_api_integration_tests::{
Expand All @@ -21,8 +22,9 @@ async fn list_with_edit_context_number_of_pages() {
.list_with_edit_context(&PostListParams::default())
.await
.assert_response();
assert_eq!(p.header_map.wp_total(), Some(57));
assert_eq!(p.header_map.wp_total_pages(), Some(6));
let header_map = WpNetworkHeaderMap::from_multi_map(p.header_map).unwrap();
assert_eq!(header_map.wp_total(), Some(57));
assert_eq!(header_map.wp_total_pages(), Some(6));
}

#[tokio::test]
Expand Down
6 changes: 3 additions & 3 deletions wp_derive_request_builder/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ fn generate_async_request_executor(
pub struct #response_type_ident {
pub data: #output_type,
#[serde(skip)]
pub header_map: std::sync::Arc<#crate_ident::request::WpNetworkHeaderMap>,
pub header_map: std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>,
#response_pagination_params_fields
}
impl From<#response_type_ident> for #crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type> {
fn from(value: #response_type_ident) -> Self {
Self {
data: value.data,
header_map: value.header_map,
header_map: crate::request::WpNetworkHeaderMap::from_multi_map(value.header_map).unwrap().into(),
#from_concrete_response_impl_for_pagination_params
}
}
Expand All @@ -144,7 +144,7 @@ fn generate_async_request_executor(
fn from(value: #crate_ident::request::ParsedResponse<#output_type, #parsed_response_params_type>) -> Self {
Self {
data: value.data,
header_map: value.header_map,
header_map: value.header_map.to_multi_map(),
#from_parsed_response_impl_for_pagination_params
}
}
Expand Down
Loading