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 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Ruby discovery snippetgen for MVVM (#696)
- Loading branch information
Showing
49 changed files
with
5,715 additions
and
8,824 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
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
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
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
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
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
114 changes: 114 additions & 0 deletions
114
src/main/java/com/google/api/codegen/discovery/config/ruby/RubyTypeNameGenerator.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,114 @@ | ||
/* 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.discovery.config.ruby; | ||
|
||
import com.google.api.codegen.DiscoveryImporter; | ||
import com.google.api.codegen.discovery.config.TypeNameGenerator; | ||
import com.google.api.codegen.ruby.RubyApiaryNameMap; | ||
import com.google.api.codegen.util.Name; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.io.Resources; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
public class RubyTypeNameGenerator extends TypeNameGenerator { | ||
|
||
private String apiName; | ||
private String apiVersion; | ||
private final ImmutableMap<String, String> NAME_MAP; | ||
|
||
public RubyTypeNameGenerator() { | ||
try { | ||
NAME_MAP = getMethodNameMap(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private ImmutableMap<String, String> getMethodNameMap() throws IOException { | ||
String data = | ||
Resources.toString( | ||
Resources.getResource(RubyApiaryNameMap.class, "apiary_names.yaml"), | ||
StandardCharsets.UTF_8); | ||
// Unchecked cast here. | ||
return ImmutableMap.copyOf((Map<String, String>) (new Yaml().load(data))); | ||
} | ||
|
||
@Override | ||
public String stringDelimiter() { | ||
return "'"; | ||
} | ||
|
||
@Override | ||
public void setApiNameAndVersion(String apiName, String apiVersion) { | ||
this.apiName = apiName; | ||
this.apiVersion = apiVersion; | ||
} | ||
|
||
@Override | ||
public List<String> getMethodNameComponents(List<String> nameComponents) { | ||
// Generate the key by joining apiName, apiVersion and nameComponents on '.' | ||
// Ex: "/admin:directory_v1/admin.channels.stop" | ||
String key = "/" + apiName + ":" + apiVersion + "/" + Joiner.on('.').join(nameComponents); | ||
if (!NAME_MAP.containsKey(key)) { | ||
throw new IllegalArgumentException("\"" + key + "\"" + " not in method name map"); | ||
} | ||
return ImmutableList.of(NAME_MAP.get(key)); | ||
} | ||
|
||
@Override | ||
public String getApiVersion(String apiVersion) { | ||
return apiVersion.replace('.', '_'); // v1.4 to v1_4 | ||
} | ||
|
||
@Override | ||
public String getPackagePrefix(String apiName, String apiVersion) { | ||
return "google/apis/" + Name.from(apiName, apiVersion).toLowerUnderscore(); | ||
} | ||
|
||
@Override | ||
public String getApiTypeName(String apiName) { | ||
return Name.upperCamel(apiName.replace(" ", ""), "Service").toUpperCamel(); | ||
} | ||
|
||
@Override | ||
public String getResponseTypeUrl(String responseTypeUrl) { | ||
if (responseTypeUrl.equals(DiscoveryImporter.EMPTY_TYPE_NAME) | ||
|| responseTypeUrl.equals(DiscoveryImporter.EMPTY_TYPE_URL)) { | ||
return ""; | ||
} | ||
return responseTypeUrl; | ||
} | ||
|
||
@Override | ||
public String getMessageTypeName(String messageTypeName) { | ||
// Avoid cases like "DatasetList.Datasets" | ||
String pieces[] = messageTypeName.split("\\."); | ||
messageTypeName = pieces[0]; | ||
// Generate the key by joining apiName, apiVersion and messageTypeName. | ||
// Ex: "/bigquery:v2/DatasetList" | ||
String key = "/" + apiName + ":" + apiVersion + "/" + messageTypeName; | ||
if (!NAME_MAP.containsKey(key)) { | ||
throw new IllegalArgumentException("\"" + key + "\"" + " not in method name map"); | ||
} | ||
return Name.from(NAME_MAP.get(key)).toUpperCamel(); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.