Skip to content

Commit

Permalink
feat(swift): add snippets (#2705)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 authored and millotp committed Feb 14, 2024
1 parent cd2940a commit c034dc9
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config/clients.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@
"tests": {
"extension": "Tests.swift",
"outputFolder": "Tests"
},
"snippets": {
"extension": ".swift",
"outputFolder": "Sources"
}
}
}
3 changes: 3 additions & 0 deletions config/generation.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export const patterns = [
'!clients/algoliasearch-client-scala/src/main/scala/algoliasearch/config/**',

// Swift
'!snippets/swift/**',
'snippets/swift/Package.swift',
'snippets/swift/Sources/**',
'clients/algoliasearch-client-swift/**',
'!clients/algoliasearch-client-swift/*',
'clients/algoliasearch-client-swift/AlgoliaSearchClient.podspec',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public void addDataToBundle(Map<String, Object> bundle) throws GeneratorExceptio
.collect(Collectors.toList())
);
}

@Override
public void addSnippetsSupportingFiles(List<SupportingFile> supportingFiles) {
supportingFiles.add(new SupportingFile("snippets/Package.mustache", "snippets/swift", "Package.swift"));
}
}
2 changes: 1 addition & 1 deletion scripts/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function formatter(language: string, cwd: string): Promise<void> {
await run('sbt -Dsbt.server.forcestart=true scalafmtAll scalafmtSbt', { cwd, language });
break;
case 'swift':
if (cwd.includes('tests') || cwd.includes('snippets')) {
if (cwd.includes('tests')) {
await run(`swiftformat Tests`, { cwd, language });
} else {
await run(`swiftformat Sources`, { cwd, language });
Expand Down
9 changes: 9 additions & 0 deletions snippets/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Package.resolved
34 changes: 34 additions & 0 deletions templates/swift/snippets/Package.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "AlgoliaSearchClientSnippets",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6),
],
products: [
.library(
name: "AlgoliaSearchClientSnippets",
targets: ["AlgoliaSearchClientSnippets"]),
],
dependencies: [
.package(path: "../../clients/algoliasearch-client-swift"),
.package(url: "https://github.com/Flight-School/AnyCodable", .upToNextMajor(from: "0.6.1"))
],
targets: [
.target(
name: "AlgoliaSearchClientSnippets",
dependencies: [
.product(name: "AnyCodable", package: "AnyCodable"),{{#packageList}}
.product(
name: "{{{.}}}",
package: "algoliasearch-client-swift"
),{{/packageList}}
]
)
]
)
34 changes: 34 additions & 0 deletions templates/swift/snippets/method.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if canImport(AnyCodable)
import AnyCodable
#endif

import Core
import {{import}}

final class {{client}}Snippet {
{{#blocksRequests}}
{{#snippet}}
// Snippet for the {{method}} method.
//
// {{{description}}}
func snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascalcase}}() async throws {
// Initialize the client
let client = try {{client}}(appId: "YOUR_APP_ID", apiKey: "YOUR_API_KEY"{{#hasRegionalHost}}, region: .{{defaultRegion}}{{/hasRegionalHost}})

{{#hasRequestOptions}}
let requestOptions = RequestOptions({{#requestOptions.headers.parametersWithDataType}}{{#-first}}
headers: [{{/-first}}
"{{key}}": {{> tests/paramValue }}{{^-last}}, {{/-last}}{{#-last}}
]{{/-last}}{{/requestOptions.headers.parametersWithDataType}}{{#requestOptions.headers.parametersWithDataType}}{{#requestOptions.queryParameters.parametersWithDataType}},{{/requestOptions.queryParameters.parametersWithDataType}}{{/requestOptions.headers.parametersWithDataType}}{{#requestOptions.queryParameters.parametersWithDataType}}{{#-first}}
queryItems: APIHelper.mapValuesToQueryItems([{{/-first}}
"{{key}}": {{> tests/paramValue }}{{^-last}}, {{/-last}}{{#-last}}
]){{/-last}}{{/requestOptions.queryParameters.parametersWithDataType}}
)
{{/hasRequestOptions}}

// Call the API
let _ = try await client.{{method}}({{#parametersWithDataType}}{{> tests/generateParams}}{{^-last}},{{/-last}}{{/parametersWithDataType}}{{#hasRequestOptions}}, requestOptions: requestOptions{{/hasRequestOptions}})
}
{{/snippet}}
{{/blocksRequests}}
}
1 change: 0 additions & 1 deletion templates/swift/tests/Package.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// swift-tools-version: 5.9
// {{{generationBanner}}}

import PackageDescription

Expand Down

0 comments on commit c034dc9

Please sign in to comment.