Skip to content

Commit

Permalink
Updated PHP generated surface (googleapis#225)
Browse files Browse the repository at this point in the history
- Altered surface to match php-cs-fixer
- Added sample generation
  • Loading branch information
michaelbausor committed Jun 13, 2016
1 parent b136e07 commit d5d75df
Show file tree
Hide file tree
Showing 5 changed files with 628 additions and 124 deletions.
67 changes: 67 additions & 0 deletions src/main/java/com/google/api/codegen/php/PhpDocConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* 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.php;

import com.google.api.codegen.DocConfig;
import com.google.api.codegen.metacode.InitCode;
import com.google.api.codegen.metacode.InputParameter;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;

import javax.annotation.Nullable;

/**
* Represents the Php documentation settings for an Api method.
*/
@AutoValue
abstract class PhpDocConfig extends DocConfig {
public static PhpDocConfig.Builder newBuilder() {
return new AutoValue_PhpDocConfig.Builder();
}

public abstract boolean isPagedVariant();

@Override
@Nullable
public abstract ImmutableList<InputParameter> getParams();

@AutoValue.Builder
abstract static class Builder extends DocConfig.Builder<Builder> {

public abstract PhpDocConfig build();

public abstract Builder setApiName(String serviceName);

public abstract Builder setMethodName(String methodName);

public abstract Builder setPagedVariant(boolean paged);

public abstract Builder setReturnType(String returnType);

public abstract Builder setInitCode(InitCode initCode);

public abstract Builder setParams(ImmutableList<InputParameter> params);

@Override
protected Builder setInitCodeProxy(InitCode initCode) {
return setInitCode(initCode);
}

@Override
protected Builder setParamsProxy(ImmutableList<InputParameter> params) {
return setParams(params);
}
}
}
88 changes: 73 additions & 15 deletions src/main/java/com/google/api/codegen/php/PhpGapicContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import com.google.api.codegen.ApiConfig;
import com.google.api.codegen.GapicContext;
import com.google.api.tools.framework.aspects.documentation.model.DocumentationUtil;
import com.google.api.tools.framework.model.Interface;
import com.google.api.tools.framework.model.Method;
import com.google.api.tools.framework.model.Model;
import com.google.api.tools.framework.model.ProtoElement;
import com.google.api.tools.framework.model.ProtoFile;
import com.google.api.tools.framework.model.TypeRef;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type;
Expand All @@ -35,21 +36,43 @@ public class PhpGapicContext extends GapicContext implements PhpContext {
*/
private static final ImmutableMap<Type, String> PRIMITIVE_TYPE_MAP =
ImmutableMap.<Type, String>builder()
.put(Type.TYPE_BOOL, "boolean")
.put(Type.TYPE_BOOL, "bool")
.put(Type.TYPE_DOUBLE, "float")
.put(Type.TYPE_FLOAT, "float")
.put(Type.TYPE_INT64, "integer")
.put(Type.TYPE_UINT64, "integer")
.put(Type.TYPE_SINT64, "integer")
.put(Type.TYPE_FIXED64, "integer")
.put(Type.TYPE_SFIXED64, "integer")
.put(Type.TYPE_INT32, "integer")
.put(Type.TYPE_UINT32, "integer")
.put(Type.TYPE_SINT32, "integer")
.put(Type.TYPE_FIXED32, "integer")
.put(Type.TYPE_SFIXED32, "integer")
.put(Type.TYPE_INT64, "int")
.put(Type.TYPE_UINT64, "int")
.put(Type.TYPE_SINT64, "int")
.put(Type.TYPE_FIXED64, "int")
.put(Type.TYPE_SFIXED64, "int")
.put(Type.TYPE_INT32, "int")
.put(Type.TYPE_UINT32, "int")
.put(Type.TYPE_SINT32, "int")
.put(Type.TYPE_FIXED32, "int")
.put(Type.TYPE_SFIXED32, "int")
.put(Type.TYPE_STRING, "string")
.put(Type.TYPE_BYTES, "com\\google\\protobuf\\ByteString")
.put(Type.TYPE_BYTES, "string")
.build();

/**
* A map from primitive types in proto to zero value in PHP
*/
private static final ImmutableMap<Type, String> PRIMITIVE_ZERO_VALUE =
ImmutableMap.<Type, String>builder()
.put(Type.TYPE_BOOL, "false")
.put(Type.TYPE_DOUBLE, "0.0")
.put(Type.TYPE_FLOAT, "0.0")
.put(Type.TYPE_INT64, "0")
.put(Type.TYPE_UINT64, "0")
.put(Type.TYPE_SINT64, "0")
.put(Type.TYPE_FIXED64, "0")
.put(Type.TYPE_SFIXED64, "0")
.put(Type.TYPE_INT32, "0")
.put(Type.TYPE_UINT32, "0")
.put(Type.TYPE_SINT32, "0")
.put(Type.TYPE_FIXED32, "0")
.put(Type.TYPE_SFIXED32, "0")
.put(Type.TYPE_STRING, "\"\"")
.put(Type.TYPE_BYTES, "\"\"")
.build();

private PhpContextCommon phpCommon;
Expand Down Expand Up @@ -140,6 +163,29 @@ public String basicTypeName(TypeRef type) {
}
}

/**
* Returns the PHP representation of a zero value for that type, to be used in code sample doc.
*
* Parametric types may use the diamond operator, since the return value will be used only in
* initialization.
*/
public String zeroValue(TypeRef type) {
// Don't call getTypeName; we don't need to import these.
if (type.isMap()) {
return "[]";
}
if (type.isRepeated()) {
return "[]";
}
if (PRIMITIVE_ZERO_VALUE.containsKey(type.getKind())) {
return PRIMITIVE_ZERO_VALUE.get(type.getKind());
}
if (type.isMessage()) {
return "new " + typeName(type) + "()";
}
return "null";
}

public String fullyQualifiedName(TypeRef type) {
return type.getMessageType().getFullName().replaceAll("\\.", "\\\\");
}
Expand Down Expand Up @@ -167,8 +213,20 @@ public String getPhpPackage(ProtoFile file) {
return file.getProto().getPackage().replaceAll("\\.", "\\\\");
}

public String getDescription(ProtoElement element) {
return DocumentationUtil.getDescription(element);
public Method getFirstMethod(Interface service) {
ImmutableList<Method> methods = service.getMethods();
if (methods.size() > 0) {
return methods.get(0);
}
throw new RuntimeException("No methods available.");
}

public String returnTypeOrEmpty(TypeRef returnType) {
return messages().isEmptyType(returnType) ? "" : typeName(returnType);
}

public PhpDocConfig.Builder newPhpDocConfigBuilder() {
return PhpDocConfig.newBuilder();
}

// Constants
Expand Down
Loading

0 comments on commit d5d75df

Please sign in to comment.