Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Refactor Ruby discovery snippetgen for MVVM (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
saicheems authored Nov 2, 2016
1 parent 8b1ef40 commit 39e3541
Show file tree
Hide file tree
Showing 49 changed files with 5,715 additions and 8,824 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (System.env.TRAVIS == 'true') {
groovyOptions.fork = false
}
tasks.withType(Test) {
maxParallelForks = 2
maxParallelForks = 1
minHeapSize = '128m'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
import com.google.api.codegen.discovery.config.go.GoTypeNameGenerator;
import com.google.api.codegen.discovery.config.java.JavaTypeNameGenerator;
import com.google.api.codegen.discovery.config.nodejs.NodeJSTypeNameGenerator;
import com.google.api.codegen.discovery.config.ruby.RubyTypeNameGenerator;
import com.google.api.codegen.discovery.transformer.SampleMethodToViewTransformer;
import com.google.api.codegen.discovery.transformer.go.GoSampleMethodToViewTransformer;
import com.google.api.codegen.discovery.transformer.java.JavaSampleMethodToViewTransformer;
import com.google.api.codegen.discovery.transformer.nodejs.NodeJSSampleMethodToViewTransformer;
import com.google.api.codegen.discovery.transformer.ruby.RubySampleMethodToViewTransformer;
import com.google.api.codegen.php.PhpDiscoveryContext;
import com.google.api.codegen.php.PhpSnippetSetRunner;
import com.google.api.codegen.py.PythonDiscoveryContext;
import com.google.api.codegen.py.PythonDiscoveryInitializer;
import com.google.api.codegen.py.PythonSnippetSetRunner;
import com.google.api.codegen.rendering.CommonSnippetSetRunner;
import com.google.api.codegen.ruby.RubyDiscoveryContext;
import com.google.api.codegen.ruby.RubySnippetSetRunner;
import com.google.api.codegen.util.CommonRenderingUtil;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Method;
Expand All @@ -62,12 +62,14 @@ public class MainDiscoveryProviderFactory implements DiscoveryProviderFactory {
ImmutableMap.of(
GO, GoSampleMethodToViewTransformer.class,
JAVA, JavaSampleMethodToViewTransformer.class,
NODEJS, NodeJSSampleMethodToViewTransformer.class);
NODEJS, NodeJSSampleMethodToViewTransformer.class,
RUBY, RubySampleMethodToViewTransformer.class);
private static final Map<String, Class<? extends TypeNameGenerator>> TYPE_NAME_GENERATOR_MAP =
ImmutableMap.of(
GO, GoTypeNameGenerator.class,
JAVA, JavaTypeNameGenerator.class,
NODEJS, NodeJSTypeNameGenerator.class);
NODEJS, NodeJSTypeNameGenerator.class,
RUBY, RubyTypeNameGenerator.class);

public static DiscoveryProvider defaultCreate(
Service service, ApiaryConfig apiaryConfig, JsonNode sampleConfigOverrides, String id) {
Expand Down Expand Up @@ -104,14 +106,6 @@ public static DiscoveryProvider defaultCreate(
new PythonDiscoveryInitializer(), SnippetSetRunner.SNIPPET_RESOURCE_ROOT))
.setSnippetFileName("py/" + DEFAULT_SNIPPET_FILE)
.build();

} else if (id.equals(RUBY)) {
return CommonDiscoveryProvider.newBuilder()
.setContext(new RubyDiscoveryContext(service, apiaryConfig))
.setSnippetSetRunner(
new RubySnippetSetRunner<Method>(SnippetSetRunner.SNIPPET_RESOURCE_ROOT))
.setSnippetFileName(id + "/" + DEFAULT_SNIPPET_FILE)
.build();
}

// Below is the MVVM pathway.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ public ApiaryConfigToSampleConfigConverter(
this.methods = methods;
this.apiaryConfig = apiaryConfig;
this.typeNameGenerator = typeNameGenerator;
typeNameGenerator.setApiNameAndVersion(apiaryConfig.getApiName(), apiaryConfig.getApiVersion());

methodNameComponents = new HashMap<String, List<String>>();
// Since methodNameComponents are used to generate the request type name, we
// produce them here for ease of access.
for (Method method : methods) {
String methodName = method.getName();
LinkedList<String> nameComponents = new LinkedList<>(Arrays.asList(methodName.split("\\.")));
nameComponents.removeFirst(); // Removes the API name.
methodNameComponents.put(method.getName(), nameComponents);
LinkedList<String> split = new LinkedList<>(Arrays.asList(method.getName().split("\\.")));
methodNameComponents.put(method.getName(), split);
}
}

Expand Down Expand Up @@ -117,7 +116,9 @@ private MethodInfo createMethod(Method method) {
MethodInfo methodInfo =
MethodInfo.newBuilder()
.verb(apiaryConfig.getHttpMethod(method.getName()))
.nameComponents(methodNameComponents.get(method.getName()))
.nameComponents(
typeNameGenerator.getMethodNameComponents(
methodNameComponents.get(method.getName())))
.fields(fields.build())
.requestType(requestType)
.requestBodyType(requestBodyType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,34 @@
*/
package com.google.api.codegen.discovery.config;

import com.google.api.client.util.Strings;
import com.google.api.codegen.DiscoveryImporter;
import com.google.api.codegen.discovery.DefaultString;
import com.google.common.base.Strings;
import java.util.LinkedList;
import java.util.List;

/** Generates language-specific names for types and package paths. */
public class TypeNameGenerator {

/**
* Returns the language-formatted method name components.
*
* <p>For example: "myapi.foo.get" to ["Foo", "Get"]
*/
public List<String> getMethodNameComponents(List<String> nameComponents) {
LinkedList<String> copy = new LinkedList<>(nameComponents);
// Don't edit the original object.
copy.removeFirst();
return copy;
}

/**
* Sets the apiName and apiVersion used for name lookups.
*
* <p>Only used in Ruby to filter method and type names from apiary_names.yaml
*/
public void setApiNameAndVersion(String apiName, String apiVersion) {}

/** Returns language-specific delimiter used for string literals in samples. */
public String stringDelimiter() {
return "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.api.codegen.DiscoveryImporter;
import com.google.api.codegen.discovery.config.TypeNameGenerator;
import com.google.api.codegen.util.Name;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -26,6 +27,17 @@ public class GoTypeNameGenerator extends TypeNameGenerator {
// Pattern used to rename some Go package versions.
private static final Pattern SUB_VERSION = Pattern.compile("^(.+)_(v[0-9.]+)$");

@Override
public List<String> getMethodNameComponents(List<String> nameComponents) {
LinkedList<String> copy = new LinkedList<String>(nameComponents);
// Don't edit the original object.
copy.removeFirst();
for (int i = 0; i < copy.size(); i++) {
copy.set(i, Name.lowerCamel(copy.get(i)).toUpperCamel());
}
return copy;
}

@Override
public String getApiVersion(String apiVersion) {
if (apiVersion.equals("alpha") || apiVersion.equals("beta")) {
Expand All @@ -45,9 +57,11 @@ public String getPackagePrefix(String apiName, String apiVersion) {

@Override
public String getRequestTypeName(List<String> methodNameComponents) {
String copy[] = methodNameComponents.toArray(new String[methodNameComponents.size() + 1]);
copy[copy.length - 1] = "call";
return Name.lowerCamel(copy).toUpperCamel();
LinkedList<String> copy = new LinkedList<String>(methodNameComponents);
copy.removeFirst();
String arr[] = copy.toArray(new String[copy.size() + 1]);
arr[arr.length - 1] = "call";
return Name.lowerCamel(arr).toUpperCamel();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.google.api.codegen.discovery.config.TypeNameGenerator;
import com.google.api.codegen.util.Name;
import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class JavaTypeNameGenerator extends TypeNameGenerator {
Expand All @@ -33,7 +33,8 @@ public String getPackagePrefix(String apiName, String apiVersion) {

@Override
public String getRequestTypeName(List<String> methodNameComponents) {
List<String> copy = new ArrayList<>(methodNameComponents);
LinkedList<String> copy = new LinkedList<>(methodNameComponents);
copy.removeFirst();
for (int i = 0; i < copy.size(); i++) {
copy.set(i, Name.lowerCamel(copy.get(i)).toUpperCamel());
}
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ private SampleView createSampleView(SampleTransformerContext context) {
// isn't Go specific logic in the transformer.
String servicePackageName = GoSampleNamer.getServicePackageName(config.packagePrefix());
String serviceVarName = symbolTable.getNewSymbol(namer.getServiceVarName(servicePackageName));
List<String> methodNameComponents = new ArrayList<String>();
for (String nameComponent : methodInfo.nameComponents()) {
methodNameComponents.add(namer.publicFieldName(Name.lowerCamel(nameComponent)));
}
String requestVarName = symbolTable.getNewSymbol(namer.localVarName(Name.lowerCamel("req")));
// For this and other type name assignments, we don't use TypeTable logic to
// add to the import list. The main issue is that the TypeTable returns
Expand Down Expand Up @@ -128,7 +124,7 @@ private SampleView createSampleView(SampleTransformerContext context) {
.auth(createSampleAuthView(context))
.serviceVarName(serviceVarName)
.methodVerb(methodInfo.verb())
.methodNameComponents(methodNameComponents)
.methodNameComponents(methodInfo.nameComponents())
.requestVarName(requestVarName)
.requestTypeName(requestTypeName)
.hasRequestBody(hasRequestBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class GoSampleTypeNameConverter implements SampleTypeNameConverter {
/** A map from primitive types in proto to Go counterparts. */
private static final ImmutableMap<Field.Kind, String> PRIMITIVE_TYPE_MAP =
ImmutableMap.<Field.Kind, String>builder()
.put(Field.Kind.TYPE_UNKNOWN, "interface")
.put(Field.Kind.TYPE_BOOL, "bool")
.put(Field.Kind.TYPE_INT32, "int32")
.put(Field.Kind.TYPE_INT64, "int64")
Expand All @@ -40,6 +41,7 @@ public class GoSampleTypeNameConverter implements SampleTypeNameConverter {
/** A map from primitive types in proto to zero value in Go. */
private static final ImmutableMap<Field.Kind, String> PRIMITIVE_ZERO_VALUE =
ImmutableMap.<Field.Kind, String>builder()
.put(Field.Kind.TYPE_UNKNOWN, "interface{}")
.put(Field.Kind.TYPE_BOOL, "false")
.put(Field.Kind.TYPE_INT32, "int64(0)")
.put(Field.Kind.TYPE_INT64, "int64(0)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class JavaSampleTypeNameConverter implements SampleTypeNameConverter {
/** A map from primitive types in proto to Java counterparts. */
private static final ImmutableMap<Field.Kind, String> PRIMIVITVE_TYPE_MAP =
ImmutableMap.<Field.Kind, String>builder()
.put(Field.Kind.TYPE_UNKNOWN, "java.lang.Object")
.put(Field.Kind.TYPE_BOOL, "boolean")
.put(Field.Kind.TYPE_INT32, "int")
.put(Field.Kind.TYPE_INT64, "long")
Expand All @@ -46,6 +47,7 @@ class JavaSampleTypeNameConverter implements SampleTypeNameConverter {
/** A map from primitive types in proto to zero value in Java. */
private static final ImmutableMap<Field.Kind, String> PRIMITIVE_ZERO_VALUE =
ImmutableMap.<Field.Kind, String>builder()
.put(Field.Kind.TYPE_UNKNOWN, "new Object()")
.put(Field.Kind.TYPE_BOOL, "false")
.put(Field.Kind.TYPE_INT32, "0")
.put(Field.Kind.TYPE_INT64, "0L")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class NodeJSSampleTypeNameConverter implements SampleTypeNameConverter {
/** A map from primitive types in proto to NodeJS counterparts. */
private static final ImmutableMap<Field.Kind, String> PRIMITIVE_TYPE_MAP =
ImmutableMap.<Field.Kind, String>builder()
.put(Field.Kind.TYPE_UNKNOWN, "Object")
.put(Field.Kind.TYPE_BOOL, "boolean")
.put(Field.Kind.TYPE_INT32, "number")
.put(Field.Kind.TYPE_INT64, "number")
Expand All @@ -40,6 +41,7 @@ public class NodeJSSampleTypeNameConverter implements SampleTypeNameConverter {
/** A map from primitive types in proto to zero value in NodeJS */
private static final ImmutableMap<Field.Kind, String> PRIMITIVE_ZERO_VALUE =
ImmutableMap.<Field.Kind, String>builder()
.put(Field.Kind.TYPE_UNKNOWN, "{}")
.put(Field.Kind.TYPE_BOOL, "false")
.put(Field.Kind.TYPE_INT32, "0")
.put(Field.Kind.TYPE_INT64, "''")
Expand Down
Loading

0 comments on commit 39e3541

Please sign in to comment.