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 PHP discovery snippetgen for MVVM
- Loading branch information
Showing
14 changed files
with
2,018 additions
and
2,660 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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/google/api/codegen/discovery/config/php/PhpTypeNameGenerator.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,60 @@ | ||
/* 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.collect.ImmutableSet; | ||
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) { | ||
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(); | ||
} | ||
} | ||
nameComponents.add(verb); | ||
return nameComponents; | ||
} | ||
|
||
@Override | ||
public String getMessageTypeName(String messageTypeName) { | ||
// Avoid cases like "DatasetList.Datasets" | ||
String pieces[] = messageTypeName.split("\\."); | ||
return pieces[pieces.length - 1]; | ||
} | ||
} |
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
186 changes: 186 additions & 0 deletions
186
...va/com/google/api/codegen/discovery/transformer/php/PhpSampleMethodToViewTransformer.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,186 @@ | ||
/* 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.transformer.php; | ||
|
||
import com.google.api.codegen.discovery.config.FieldInfo; | ||
import com.google.api.codegen.discovery.config.MethodInfo; | ||
import com.google.api.codegen.discovery.config.SampleConfig; | ||
import com.google.api.codegen.discovery.transformer.SampleMethodToViewTransformer; | ||
import com.google.api.codegen.discovery.transformer.SampleNamer; | ||
import com.google.api.codegen.discovery.transformer.SampleTransformerContext; | ||
import com.google.api.codegen.discovery.transformer.SampleTypeTable; | ||
import com.google.api.codegen.discovery.viewmodel.SampleAuthView; | ||
import com.google.api.codegen.discovery.viewmodel.SampleFieldView; | ||
import com.google.api.codegen.discovery.viewmodel.SamplePageStreamingView; | ||
import com.google.api.codegen.discovery.viewmodel.SampleView; | ||
import com.google.api.codegen.util.Name; | ||
import com.google.api.codegen.util.SymbolTable; | ||
import com.google.api.codegen.util.php.PhpTypeTable; | ||
import com.google.api.codegen.viewmodel.ViewModel; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.protobuf.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PhpSampleMethodToViewTransformer implements SampleMethodToViewTransformer { | ||
|
||
private static final String TEMPLATE_FILENAME = "php/sample.snip"; | ||
|
||
// Map of rename rules for fields. | ||
// Could be done through overrides, but this is a rule implemented in the PHP | ||
// client library generator. | ||
private static final ImmutableMap<String, String> FIELD_RENAMES = | ||
ImmutableMap.of("objectId", "object_id_"); | ||
|
||
public PhpSampleMethodToViewTransformer() {} | ||
|
||
@Override | ||
public ViewModel transform(Method method, SampleConfig config) { | ||
SampleTypeTable typeTable = | ||
new SampleTypeTable( | ||
new PhpTypeTable(""), new PhpSampleTypeNameConverter(config.apiTypeName())); | ||
SampleNamer namer = new PhpSampleNamer(); | ||
SampleTransformerContext context = | ||
SampleTransformerContext.create(config, typeTable, namer, method.getName()); | ||
return createSampleView(context); | ||
} | ||
|
||
private SampleView createSampleView(SampleTransformerContext context) { | ||
SampleConfig config = context.getSampleConfig(); | ||
MethodInfo methodInfo = config.methods().get(context.getMethodName()); | ||
SampleNamer namer = context.getSampleNamer(); | ||
SampleTypeTable typeTable = context.getSampleTypeTable(); | ||
SymbolTable symbolTable = SymbolTable.fromSeed(PhpTypeTable.RESERVED_IDENTIFIER_SET); | ||
|
||
SampleView.Builder builder = SampleView.newBuilder(); | ||
|
||
String serviceVarName = symbolTable.getNewSymbol(namer.getServiceVarName(config.apiTypeName())); | ||
String serviceTypeName = typeTable.getServiceTypeName(config.apiTypeName()).getNickname(); | ||
|
||
if (methodInfo.isPageStreaming()) { | ||
builder.pageStreaming(createSamplePageStreamingView(context, symbolTable)); | ||
} | ||
|
||
// Created before the fields in-case there are naming conflicts in the symbol table. | ||
SampleAuthView sampleAuthView = createSampleAuthView(context); | ||
|
||
List<SampleFieldView> fields = new ArrayList<>(); | ||
List<String> fieldVarNames = new ArrayList<>(); | ||
for (FieldInfo field : methodInfo.fields().values()) { | ||
String name = namer.localVarName(Name.lowerCamel(field.name())); | ||
if (FIELD_RENAMES.containsKey(field.name())) { | ||
name = FIELD_RENAMES.get(field.name()); | ||
} | ||
SampleFieldView sampleFieldView = | ||
SampleFieldView.newBuilder() | ||
.name(name) | ||
.defaultValue(typeTable.getZeroValueAndSaveNicknameFor(field.type())) | ||
.example(field.example()) | ||
.description(field.description()) | ||
.build(); | ||
fields.add(sampleFieldView); | ||
fieldVarNames.add(sampleFieldView.name()); | ||
} | ||
|
||
boolean hasRequestBody = methodInfo.requestBodyType() != null; | ||
if (hasRequestBody) { | ||
String requestBodyVarName = symbolTable.getNewSymbol(namer.getRequestBodyVarName()); | ||
builder.requestBodyVarName(requestBodyVarName); | ||
builder.requestBodyTypeName( | ||
typeTable.getTypeName(methodInfo.requestBodyType()).getNickname()); | ||
fieldVarNames.add(requestBodyVarName); | ||
} | ||
|
||
boolean hasResponse = methodInfo.responseType() != null; | ||
if (hasResponse) { | ||
builder.responseVarName(symbolTable.getNewSymbol(namer.getResponseVarName())); | ||
} | ||
|
||
String optParamsVarName = ""; | ||
if (methodInfo.isPageStreaming() || methodInfo.hasMediaDownload()) { | ||
optParamsVarName = namer.localVarName(Name.lowerCamel("optParams")); | ||
fieldVarNames.add(optParamsVarName); | ||
} | ||
|
||
return builder | ||
.templateFileName(TEMPLATE_FILENAME) | ||
.outputPath(context.getMethodName() + ".frag.php") | ||
.apiTitle(config.apiTitle()) | ||
.apiName(config.apiName()) | ||
.apiVersion(config.apiVersion()) | ||
.appName(namer.getSampleApplicationName(config.apiTypeName())) | ||
.auth(sampleAuthView) | ||
.serviceVarName(serviceVarName) | ||
.serviceTypeName(serviceTypeName) | ||
.methodVerb(methodInfo.verb()) | ||
.methodNameComponents(methodInfo.nameComponents()) | ||
.hasRequestBody(hasRequestBody) | ||
.hasResponse(hasResponse) | ||
.fields(fields) | ||
.fieldVarNames(fieldVarNames) | ||
.isPageStreaming(methodInfo.isPageStreaming()) | ||
.hasMediaUpload(methodInfo.hasMediaUpload()) | ||
.hasMediaDownload(methodInfo.hasMediaDownload()) | ||
.clientVarName(namer.localVarName(Name.lowerCamel("client"))) | ||
.optParamsVarName(optParamsVarName) | ||
.build(); | ||
} | ||
|
||
private SampleAuthView createSampleAuthView(SampleTransformerContext context) { | ||
SampleConfig config = context.getSampleConfig(); | ||
MethodInfo methodInfo = config.methods().get(context.getMethodName()); | ||
|
||
return SampleAuthView.newBuilder() | ||
.type(config.authType()) | ||
.instructionsUrl(config.authInstructionsUrl()) | ||
.scopes(methodInfo.authScopes()) | ||
.isScopesSingular(methodInfo.authScopes().size() == 1) | ||
.build(); | ||
} | ||
|
||
private SamplePageStreamingView createSamplePageStreamingView( | ||
SampleTransformerContext context, SymbolTable symbolTable) { | ||
MethodInfo methodInfo = context.getSampleConfig().methods().get(context.getMethodName()); | ||
FieldInfo fieldInfo = methodInfo.pageStreamingResourceField(); | ||
SampleNamer namer = context.getSampleNamer(); | ||
if (fieldInfo == null) { | ||
throw new IllegalArgumentException("pageStreamingResourceField cannot be null"); | ||
} | ||
|
||
SamplePageStreamingView.Builder builder = SamplePageStreamingView.newBuilder(); | ||
|
||
builder.resourceFieldName(namer.publicFieldName(Name.lowerCamel(fieldInfo.name()))); | ||
if (fieldInfo.type().isMap()) { | ||
// Assume that the value in the map is a message. | ||
if (!fieldInfo.type().mapValue().isMessage()) { | ||
throw new IllegalArgumentException("expected map value to be a message"); | ||
} | ||
builder.resourceKeyVarName( | ||
symbolTable.getNewSymbol(namer.localVarName(Name.lowerCamel("name")))); | ||
String resourceValueVarName = | ||
namer.localVarName(Name.upperCamel(fieldInfo.type().mapValue().message().typeName())); | ||
builder.resourceValueVarName(symbolTable.getNewSymbol(resourceValueVarName)); | ||
} else { | ||
String resourceVarName = | ||
namer.getResourceVarName( | ||
fieldInfo.type().isMessage() ? fieldInfo.type().message().typeName() : ""); | ||
builder.resourceVarName(symbolTable.getNewSymbol(resourceVarName)); | ||
} | ||
builder.isResourceMap(fieldInfo.type().isMap()); | ||
builder.pageVarName( | ||
symbolTable.getNewSymbol(namer.localVarName(Name.lowerCamel(fieldInfo.name())))); | ||
return builder.build(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/google/api/codegen/discovery/transformer/php/PhpSampleNamer.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,37 @@ | ||
/* 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.transformer.php; | ||
|
||
import com.google.api.codegen.discovery.transformer.SampleNamer; | ||
import com.google.api.codegen.util.Name; | ||
import com.google.api.codegen.util.php.PhpNameFormatter; | ||
|
||
public class PhpSampleNamer extends SampleNamer { | ||
|
||
public PhpSampleNamer() { | ||
super(new PhpNameFormatter()); | ||
} | ||
|
||
@Override | ||
public String getSampleApplicationName(String apiTypeName) { | ||
// Unfortunate replacement that's necessary because the API type name is of a special format. | ||
return "Google-" + apiTypeName.replace("Google_Service_", "") + "Sample/0.1"; | ||
} | ||
|
||
@Override | ||
public String getServiceVarName(String apiTypeName) { | ||
return localVarName(Name.lowerCamel("service")); | ||
} | ||
} |
Oops, something went wrong.