This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify NodeJS codegen to fit with gcloud-node design. (#392)
* Modify NodeJS codegen to fit with gcloud-node design. - The new style exports a function instead of the class. The exported function will take the auth context as a parameter and returns builder function. - Files will be under a sub directory with versioned name, so that it can be exposed as a sub package of the API package. - A few misc changes and style changes made to pass lint checker in gcloud. * Rebase and fix test baselines. * Fix doc comments. - constructor parameters are not actually useful now. They are parameters to the builder function actually, so I moved there. - build() is not a function, now it's a class. This way, JSDoc can generate documentations for its methods (like builder function). - add an example clause in the constructor doc comments, so that documentation says the details of how to create an instance of the API client. * Modify comments.
- Loading branch information
Showing
8 changed files
with
494 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/google/api/codegen/nodejs/NodeJSCodePathMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* Copyright 2016 Google Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.api.codegen.nodejs; | ||
|
||
import com.google.api.codegen.ApiConfig; | ||
import com.google.api.codegen.gapic.GapicCodePathMapper; | ||
import com.google.api.tools.framework.model.ProtoElement; | ||
|
||
import com.google.common.base.Splitter; | ||
|
||
import java.util.List; | ||
|
||
public class NodeJSCodePathMapper implements GapicCodePathMapper { | ||
@Override | ||
public String getOutputPath(ProtoElement element, ApiConfig config) { | ||
String apiVersion = ""; | ||
// For the gcloud package, the generated file would be a sub part of | ||
// the package, under the versioned directory. For example, Speech V1 API | ||
// would be a part of "@google-cloud/speech" package, and loaded as | ||
// var speechV1 = require("@google-cloud/speech").v1(); | ||
// To do this, we fetch the version number from the service full name. | ||
if (NodeJSUtils.isGcloud(config)) { | ||
List<String> packages = Splitter.on(".").splitToList(element.getFullName()); | ||
if (packages.size() > 2) { | ||
String parentName = packages.get(packages.size() - 2); | ||
if (parentName.matches("v[0-9]+((alpha|beta)[0-9]+)?")) { | ||
apiVersion = parentName; | ||
} | ||
} | ||
} | ||
return apiVersion.isEmpty() ? "src" : ("src/" + apiVersion); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/google/api/codegen/nodejs/NodeJSUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright 2016 Google Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.api.codegen.nodejs; | ||
|
||
import com.google.api.codegen.ApiConfig; | ||
import com.google.common.base.Strings; | ||
|
||
public class NodeJSUtils { | ||
/** | ||
* Returns true if the current API is a part of gcloud (i.e. cloud API). | ||
* This can be known if the package name configuration is in the pattern | ||
* of "@google-cloud/(API_NAME)". | ||
*/ | ||
public static boolean isGcloud(ApiConfig config) { | ||
String packageName = config.getPackageName(); | ||
return !Strings.isNullOrEmpty(packageName) && packageName.startsWith("@google-cloud/"); | ||
} | ||
} |
Oops, something went wrong.