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

Commit

Permalink
Refactor PHP discovery snippetgen for MVVM (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
saicheems authored Nov 7, 2016
1 parent c543c0d commit f77724f
Show file tree
Hide file tree
Showing 46 changed files with 5,248 additions and 8,312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
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.php.PhpTypeNameGenerator;
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.php.PhpSampleMethodToViewTransformer;
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;
Expand Down Expand Up @@ -63,12 +63,14 @@ public class MainDiscoveryProviderFactory implements DiscoveryProviderFactory {
GO, GoSampleMethodToViewTransformer.class,
JAVA, JavaSampleMethodToViewTransformer.class,
NODEJS, NodeJSSampleMethodToViewTransformer.class,
PHP, PhpSampleMethodToViewTransformer.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,
PHP, PhpTypeNameGenerator.class,
RUBY, RubyTypeNameGenerator.class);

public static DiscoveryProvider defaultCreate(
Expand All @@ -90,14 +92,6 @@ public static DiscoveryProvider defaultCreate(
.setSnippetFileName(id + "/" + DEFAULT_SNIPPET_FILE)
.build();

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

} else if (id.equals(PYTHON)) {
return CommonDiscoveryProvider.newBuilder()
.setContext(new PythonDiscoveryContext(service, apiaryConfig))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public SampleConfig convert() {
String apiTypeName = typeNameGenerator.getApiTypeName(apiaryConfig.getServiceCanonicalName());
return SampleConfig.newBuilder()
.apiTitle(apiaryConfig.getApiTitle())
.apiCanonicalName(apiaryConfig.getServiceCanonicalName())
.apiName(apiName)
.apiVersion(apiVersion)
.apiTypeName(apiTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public abstract class SampleConfig {
@JsonProperty("apiTitle")
public abstract String apiTitle();

/**
* Returns the API's canonical name.
*
* <p>For example: "Ad Exchange Buyer"
*/
@JsonProperty("apiCanonicalName")
public abstract String apiCanonicalName();

/**
* Returns the API's name.
*
Expand Down Expand Up @@ -109,6 +117,9 @@ public abstract static class Builder {
@JsonProperty("apiTitle")
public abstract Builder apiTitle(String val);

@JsonProperty("apiCanonicalName")
public abstract Builder apiCanonicalName(String val);

@JsonProperty("apiName")
public abstract Builder apiName(String val);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* 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.php;

import com.google.api.codegen.discovery.config.TypeNameGenerator;
import com.google.api.codegen.util.Name;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.List;

public class PhpTypeNameGenerator extends TypeNameGenerator {

/**
* Set of method names that must have the previous name components as upper-camel suffixes.
*
* <p>For example: "foo.bar.list" to "foo.bar.listFooBar"
*/
ImmutableSet<String> RENAMED_METHODS = ImmutableSet.of("list", "clone");

@Override
public String stringDelimiter() {
return "'";
}

@Override
public String getApiTypeName(String apiName) {
return "Google_Service_" + apiName.replace(" ", "");
}

@Override
public List<String> getMethodNameComponents(List<String> nameComponents) {
ArrayList<String> out = new ArrayList<>();

nameComponents = super.getMethodNameComponents(nameComponents);
String verb = nameComponents.remove(nameComponents.size() - 1); // Pop the last element.
if (RENAMED_METHODS.contains(verb)) {
for (String s : nameComponents) {
verb += Name.lowerCamel(s).toUpperCamel();
}
}
// If there are multiple resources before the verb, they're joined on '_'.
// Ex: "$service->billingAccounts_projects->listBillingAccountsProjects"
if (nameComponents.size() > 1) {
out.add(Joiner.on('_').join(nameComponents));
} else if (nameComponents.size() == 1) {
out.add(nameComponents.get(0));
}
out.add(verb);
return out;
}

@Override
public String getMessageTypeName(String messageTypeName) {
// Avoid cases like "DatasetList.Datasets"
String pieces[] = messageTypeName.split("\\.");
return pieces[pieces.length - 1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
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;
Expand Down Expand Up @@ -89,15 +88,6 @@ 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public SampleNamer(NameFormatter nameFormatter) {
}

/** Returns the application name of the sample. */
public String getSampleApplicationName(String apiTypeName) {
return "Google-" + apiTypeName + "Sample/0.1";
public String getSampleApplicationName(String apiCanonicalName) {
return "Google-" + apiCanonicalName.replace(" ", "") + "Sample/0.1";
}

/** Returns the class name of the sample. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private SampleView createSampleView(SampleTransformerContext context) {
.apiTitle(config.apiTitle())
.apiName(config.apiName())
.apiVersion(config.apiVersion())
.appName(namer.getSampleApplicationName(config.apiTypeName()))
.appName(namer.getSampleApplicationName(config.apiCanonicalName()))
.className(namer.getSampleClassName(config.apiTypeName()))
.imports(imports)
.auth(createSampleAuthView(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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 @@ -41,7 +40,6 @@ 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 Expand Up @@ -80,6 +78,9 @@ public TypeName getTypeName(TypeInfo typeInfo) {

@Override
public TypeName getTypeNameForElementType(TypeInfo typeInfo) {
if (typeInfo.kind() == Field.Kind.TYPE_UNKNOWN) {
return new TypeName("Object");
}
String primitiveTypeName = PRIMITIVE_TYPE_MAP.get(typeInfo.kind());
if (primitiveTypeName != null) {
return new TypeName(primitiveTypeName);
Expand All @@ -90,7 +91,7 @@ public TypeName getTypeNameForElementType(TypeInfo typeInfo) {
@Override
public TypedValue getZeroValue(TypeInfo typeInfo) {
// Don't call getTypeName; we don't need to import these.
if (typeInfo.isMap()) {
if (typeInfo.isMap() || typeInfo.kind() == Field.Kind.TYPE_UNKNOWN) {
return TypedValue.create(new TypeName("Object"), "{}");
}
if (typeInfo.isArray()) {
Expand Down
Loading

0 comments on commit f77724f

Please sign in to comment.