Skip to content

Commit

Permalink
fix(swift): have a codable body for improved ux (#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 authored Mar 25, 2024
1 parent 846e9d3 commit cdac5ff
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
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}}

1 comment on commit cdac5ff

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please # to comment.