Skip to content

Commit

Permalink
snippets(go): add snippets (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jan 19, 2024
1 parent 9451c82 commit 1557175
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/clients.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
"tests": {
"extension": "_test.go",
"outputFolder": "tests"
},
"snippets": {
"extension": ".go",
"outputFolder": "src"
}
},
"java": {
Expand Down
7 changes: 7 additions & 0 deletions scripts/snippets/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export async function snippetsGenerateMany(generators: Generator[]): Promise<voi
});
}

if (lang === 'go') {
await run('go mod tidy', {
cwd: 'snippets/go',
language: 'go',
});
}

const snippetsPath = `snippets/${lang}`;
if (await exists(toAbsolutePath(snippetsPath))) {
await formatter(lang, snippetsPath);
Expand Down
10 changes: 10 additions & 0 deletions snippets/go/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
linters:
disable:
- ineffassign
- staticcheck
service:
golangci-lint-version: 1.55.2

run:
concurrency: 2
timeout: 10m
20 changes: 20 additions & 0 deletions snippets/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module snippets

go 1.21

replace github.com/algolia/algoliasearch-client-go/v4 v4.0.0 => ../../clients/algoliasearch-client-go

require github.com/algolia/algoliasearch-client-go/v4 v4.0.0

require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
40 changes: 40 additions & 0 deletions templates/go/snippets/method.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package snippets

import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/{{clientImport}}"
)

{{#blocksRequests}}
{{#snippet}}
func SnippetFor{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Of{{#lambda.pascalcase}}{{clientPrefix}}{{/lambda.pascalcase}}() {
/*
Snippet for the {{method}} method.

{{{description}}}
*/

// Initialize the client{{#hasRegionalHost}} with your application region, eg. {{clientPrefix}}.YOUR_APP_ID_REGION{{/hasRegionalHost}}
client, err := {{clientPrefix}}.NewClient("YOUR_APP_ID", "YOUR_API_KEY"{{#hasRegionalHost}}, {{clientPrefix}}.{{#lambda.uppercase}}{{defaultRegion}}{{/lambda.uppercase}}{{/hasRegionalHost}})
if err != nil {
// The client can fail to initialize if you pass an invalid parameter.
panic(err)
}

// Call the API
{{#hasResponsePayload}}resp, err :={{/hasResponsePayload}}{{^hasResponsePayload}}err ={{/hasResponsePayload}} client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{#hasOperationParams}}client.NewApi{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Request(
{{#parametersWithDataType}}{{#required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}
){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{/hasOperationParams}}{{#requestOptions}}{{#hasOperationParams}},{{/hasOperationParams}}
{{#queryParameters.parametersWithDataType}}{{clientPrefix}}.QueryParamOption("{{{key}}}", {{> tests/generateInnerParams}}),{{/queryParameters.parametersWithDataType}}{{#headers.parametersWithDataType}}{{clientPrefix}}.HeaderParamOption("{{{key}}}", {{> tests/generateInnerParams}}),{{/headers.parametersWithDataType}}
{{/requestOptions}})
if err != nil {
// handle the eventual error
panic(err)
}

{{#hasResponsePayload}}
// use the model directly
print(resp)
{{/hasResponsePayload}}
}
{{/snippet}}
{{/blocksRequests}}

0 comments on commit 1557175

Please sign in to comment.