Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(swift): have a codable body for improved ux #2902

Merged
merged 3 commits into from
Mar 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void processOpts() {
"lambda.to-codable",
(Mustache.Lambda) (fragment, writer) -> {
String initialType = fragment.execute();
writer.write(initialType.equalsIgnoreCase("AnyCodable") ? "[String: AnyCodable]" : initialType);
writer.write(initialType.equalsIgnoreCase("AnyCodable") ? "Codable" : initialType);
}
);

Expand Down
13 changes: 6 additions & 7 deletions playground/swift/playground/playground/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@ Task {
.init(firstname: "Warren", lastname: "Speach", followers: 42, company: "Norwalk Crmc")
]

let client = SearchClient(appId: applicationID, apiKey: apiKey)
let client = try SearchClient(appID: applicationID, apiKey: apiKey)

for contact in contacts {
let saveObjRes = try await client.saveObject(indexName: "contacts", body: contact)
// let saveObjRes = try await client.saveObject(indexName: "contacts", body: contact, requestOptions: RequestOptions(headers: ["X-Algolia-Test": "True"], body: ["age": contact.followers]))
let saveObjRes = try await client.saveObject(indexName: "contacts", body: contacts)
while true {
let taskResponse = try await client.getTask(indexName: "contacts", taskID: saveObjRes.taskID)
if taskResponse.status == TaskStatus.published {
if taskResponse.status == SearchTaskStatus.published {
break
}
}
}

let searchParams = SearchParamsObject(query: "Jimmy")
let searchParams = SearchSearchParamsObject(query: "Jimmy")

let res = try await client.searchSingleIndex(
let res: SearchResponse<Contact> = try await client.searchSingleIndex(
indexName: "contacts",
searchParams: .searchParamsObject(searchParams)
searchParams: .searchSearchParamsObject(searchParams)
)

dump(res.hits[0])
Expand Down
2 changes: 1 addition & 1 deletion templates/swift/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import Foundation
throw AlgoliaError.invalidArgument("{{{paramName}}}", "{{{operationId}}}")
}

{{/required}}{{/isString}}{{/queryParams}}{{#bodyParam}}{{#isFreeFormObject}}{{#required}}guard !{{{paramName}}}.isEmpty else {
{{/required}}{{/isString}}{{/queryParams}}{{#bodyParam}}{{#isFreeFormObject}}{{#required}}guard {{{paramName}}} != nil, let {{{paramName}}}Dictionary = {{{paramName}}} as? [String: Codable], !{{{paramName}}}Dictionary.isEmpty else {
throw AlgoliaError.invalidArgument("{{{paramName}}}", "{{{operationId}}}")
}

Expand Down
2 changes: 1 addition & 1 deletion templates/swift/tests/paramValue.mustache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isObject}}{{objectName}}({{^hasAdditionalProperties}}{{#value}}{{> tests/generateParams}}{{^-last}}, {{/-last}}{{/value}}{{/hasAdditionalProperties}}{{#hasAdditionalProperties}}from: [{{#value}}"{{key}}": AnyCodable({{> tests/paramValue }}){{^-last}}, {{/-last}}{{/value}}]{{/hasAdditionalProperties}}){{/isObject}}{{#isString}}{{#isAnyType}}AnyCodable({{/isAnyType}}"{{{value}}}"{{#isAnyType}}){{/isAnyType}}{{/isString}}{{#isNumber}}{{#isLong}}Int64({{/isLong}}{{{value}}}{{#isLong}}){{/isLong}}{{/isNumber}}{{#isBoolean}}{{{value}}}{{/isBoolean}}{{#isEnum}}{{objectName}}.{{#lambda.swiftIdentifier}}{{#lambda.camelcase}}{{valueEscaped}}{{/lambda.camelcase}}{{/lambda.swiftIdentifier}}{{/isEnum}}{{#isArray}}[{{#value}}{{> tests/generateParams}}{{^-last}}, {{/-last}}{{/value}}]{{/isArray}}{{#isMap}}Map[{{{.}}}]{{/isMap}}{{#isFreeFormObject}}{{#isAnyType}}[{{#value}}{{#entrySet}}"{{key}}": "{{{value}}}"{{/entrySet}}{{/value}}]{{/isAnyType}}{{^isAnyType}}[{{^value}}:{{/value}}{{#value}}{{> tests/generateParams}}{{^-last}}, {{/-last}}{{/value}}]{{/isAnyType}}{{/isFreeFormObject}}{{#isNull}}{{#inClientTest}}TestNull{{{objectName}}}(){{/inClientTest}}{{/isNull}}
{{#isObject}}{{objectName}}({{^hasAdditionalProperties}}{{#value}}{{> tests/generateParams}}{{^-last}}, {{/-last}}{{/value}}{{/hasAdditionalProperties}}{{#hasAdditionalProperties}}from: [{{#value}}"{{key}}": AnyCodable({{> tests/paramValue }}){{^-last}}, {{/-last}}{{/value}}]{{/hasAdditionalProperties}}){{/isObject}}{{#isString}}{{#isAnyType}}AnyCodable({{/isAnyType}}"{{{value}}}"{{#isAnyType}}){{/isAnyType}}{{/isString}}{{#isNumber}}{{#isLong}}Int64({{/isLong}}{{{value}}}{{#isLong}}){{/isLong}}{{/isNumber}}{{#isBoolean}}{{{value}}}{{/isBoolean}}{{#isEnum}}{{objectName}}.{{#lambda.swiftIdentifier}}{{#lambda.camelcase}}{{valueEscaped}}{{/lambda.camelcase}}{{/lambda.swiftIdentifier}}{{/isEnum}}{{#isArray}}[{{#value}}{{> tests/generateParams}}{{^-last}}, {{/-last}}{{/value}}]{{/isArray}}{{#isMap}}Map[{{{.}}}]{{/isMap}}{{#isFreeFormObject}}{{#isAnyType}}[{{#value}}{{#entrySet}}"{{key}}": "{{{value}}}"{{/entrySet}}{{/value}}]{{/isAnyType}}{{^isAnyType}}{{^value}}[String: AnyCodable](){{/value}}{{#value}}{{#-first}}[{{/-first}}{{> tests/generateParams}}{{^-last}}, {{/-last}}{{#-last}}]{{/-last}}{{/value}}{{/isAnyType}}{{/isFreeFormObject}}{{#isNull}}{{#inClientTest}}TestNull{{{objectName}}}(){{/inClientTest}}{{/isNull}}
Loading