Search in sources :

Example 1 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class CSharpModelTypeNameConverter method getSnippetZeroValue.

@Override
public TypedValue getSnippetZeroValue(TypeRef type) {
    if (type.isMap()) {
        TypeName keyTypeName = getTypeNameForElementType(type.getMapKeyField().getType());
        TypeName valueTypeName = getTypeNameForElementType(type.getMapValueField().getType());
        TypeName mapTypeName = typeNameConverter.getTypeName("System.Collections.Generic.Dictionary");
        TypeName genericMapTypeName = new TypeName(mapTypeName.getFullName(), mapTypeName.getNickname(), "%s<%i, %i>", keyTypeName, valueTypeName);
        return TypedValue.create(genericMapTypeName, "new %s()");
    } else if (type.isRepeated()) {
        TypeName elementTypeName = getTypeNameForElementType(type);
        TypeName listTypeName = typeNameConverter.getTypeName("System.Collections.Generic.List");
        TypeName genericListTypeName = new TypeName(listTypeName.getFullName(), listTypeName.getNickname(), "%s<%i>", elementTypeName);
        return TypedValue.create(genericListTypeName, "new %s()");
    } else if (type.isMessage()) {
        return TypedValue.create(getTypeName(type), "new %s()");
    } else if (type.isEnum()) {
        return getEnumValue(type, type.getEnumType().getValues().get(0));
    } else {
        return TypedValue.create(getTypeName(type), PRIMITIVE_ZERO_VALUE.get(type.getKind()));
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 2 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class JavaModelTypeNameConverter method getTypeName.

@Override
public TypeName getTypeName(TypeRef type) {
    if (type.isMap()) {
        TypeName mapTypeName = typeNameConverter.getTypeName("java.util.Map");
        TypeName keyTypeName = getTypeNameForElementType(type.getMapKeyField().getType(), true);
        TypeName valueTypeName = getTypeNameForElementType(type.getMapValueField().getType(), true);
        return new TypeName(mapTypeName.getFullName(), mapTypeName.getNickname(), "%s<%i, %i>", keyTypeName, valueTypeName);
    } else if (type.isRepeated()) {
        TypeName listTypeName = typeNameConverter.getTypeName("java.util.List");
        TypeName elementTypeName = getTypeNameForElementType(type, true);
        return new TypeName(listTypeName.getFullName(), listTypeName.getNickname(), "%s<%i>", elementTypeName);
    } else {
        return getTypeNameForElementType(type, false);
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 3 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class JavaModelTypeNameConverter method getTypeNameForTypedResourceName.

private TypeName getTypeNameForTypedResourceName(ResourceNameConfig resourceNameConfig, TypeRef type, String typedResourceShortName) {
    String packageName = getResourceNamePackage(resourceNameConfig);
    String longName = packageName + "." + typedResourceShortName;
    TypeName simpleTypeName = new TypeName(longName, typedResourceShortName);
    if (type.isMap()) {
        throw new IllegalArgumentException("Map type not supported for typed resource name");
    } else if (type.isRepeated()) {
        TypeName listTypeName = typeNameConverter.getTypeName("java.util.List");
        return new TypeName(listTypeName.getFullName(), listTypeName.getNickname(), "%s<%i>", simpleTypeName);
    } else {
        return simpleTypeName;
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 4 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class JavaSchemaTypeNameConverter method getTypeNameForElementType.

/**
 * Returns the Java representation of a type, without cardinality. If the type is a Java
 * primitive, basicTypeName returns it in unboxed form.
 *
 * @param schema The Schema to generate a TypeName from.
 *     <p>This method will be recursively called on the given schema's children.
 */
private TypeName getTypeNameForElementType(Schema schema, BoxingBehavior boxingBehavior) {
    if (schema == null) {
        return new TypeName("java.lang.Void", "Void");
    }
    String primitiveTypeName = getPrimitiveTypeName(schema);
    if (primitiveTypeName != null) {
        if (primitiveTypeName.contains(".")) {
            // because those types are already boxed.
            return typeNameConverter.getTypeName(primitiveTypeName);
        } else {
            switch(boxingBehavior) {
                case BOX_PRIMITIVES:
                    return new TypeName(JavaTypeTable.getBoxedTypeName(primitiveTypeName));
                case NO_BOX_PRIMITIVES:
                    return new TypeName(primitiveTypeName);
                default:
                    throw new IllegalArgumentException(String.format("Unhandled boxing behavior: %s", boxingBehavior.name()));
            }
        }
    }
    if (schema.type().equals(Type.ARRAY)) {
        String packageName = implicitPackageName;
        Schema element = schema.items();
        if (!Strings.isNullOrEmpty(element.reference())) {
            String shortName = element.reference() != null ? element.reference() : element.getIdentifier();
            String longName = packageName + "." + shortName;
            return new TypeName(longName, shortName);
        } else {
            return getTypeName(schema.items(), BoxingBehavior.BOX_PRIMITIVES);
        }
    } else {
        String packageName = !implicitPackageName.isEmpty() ? implicitPackageName : DEFAULT_JAVA_PACKAGE_PREFIX;
        String shortName = "";
        if (!schema.id().isEmpty()) {
            shortName = schema.id();
        } else if (!schema.reference().isEmpty()) {
            shortName = schema.reference();
        } else if (schema.additionalProperties() != null && !schema.additionalProperties().reference().isEmpty()) {
            shortName = schema.additionalProperties().reference();
        } else {
            // This schema has a parent Schema.
            shortName = nameFormatter.publicClassName(Name.anyCamel(schema.key()));
        }
        String longName = packageName + "." + shortName;
        return new TypeName(longName, shortName);
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName) Schema(com.google.api.codegen.discovery.Schema)

Example 5 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class JavaSurfaceTestTransformer method addUnitTestImports.

// ///////////////////////////////// Imports //////////////////////////////////////
private void addUnitTestImports(InterfaceContext context) {
    ImportTypeTable typeTable = context.getImportTypeTable();
    typeTable.saveNicknameFor("com.google.api.gax.rpc.InvalidArgumentException");
    typeTable.saveNicknameFor("com.google.common.collect.Lists");
    typeTable.saveNicknameFor("java.io.IOException");
    typeTable.saveNicknameFor("java.util.Arrays");
    typeTable.saveNicknameFor("java.util.List");
    typeTable.saveNicknameFor("org.junit.After");
    typeTable.saveNicknameFor("org.junit.AfterClass");
    typeTable.saveNicknameFor("org.junit.Assert");
    typeTable.saveNicknameFor("org.junit.BeforeClass");
    typeTable.saveNicknameFor("org.junit.Test");
    if (context.getInterfaceConfig().hasLongRunningOperations()) {
        typeTable.saveNicknameFor("com.google.protobuf.Any");
    }
    switch(context.getProductConfig().getTransportProtocol()) {
        case GRPC:
            typeTable.saveNicknameFor("com.google.api.gax.core.NoCredentialsProvider");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiClientHeaderProvider");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.StatusCode");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.GaxGrpcProperties");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.GrpcStatusCode");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.LocalChannelProvider");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockGrpcService");
            typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockServiceHelper");
            typeTable.saveNicknameFor("com.google.protobuf.GeneratedMessageV3");
            typeTable.saveNicknameFor("io.grpc.Status");
            typeTable.saveNicknameFor("io.grpc.StatusRuntimeException");
            typeTable.saveNicknameFor("java.util.ArrayList");
            typeTable.saveNicknameFor("java.util.Objects");
            typeTable.saveNicknameFor("java.util.concurrent.ExecutionException");
            typeTable.saveNicknameFor("org.junit.Before");
            break;
        case HTTP:
            typeTable.saveNicknameFor("com.google.api.gax.httpjson.ApiMethodDescriptor");
            typeTable.saveNicknameFor("com.google.api.gax.httpjson.GaxHttpJsonProperties");
            typeTable.saveNicknameFor("com.google.api.gax.httpjson.testing.MockHttpService");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiClientHeaderProvider");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiException");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.ApiExceptionFactory");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.StatusCode.Code");
            typeTable.saveNicknameFor("com.google.api.gax.rpc.testing.FakeStatusCode");
            typeTable.saveNicknameFor("com.google.common.collect.ImmutableList");
            // Import stub settings class in unit test file.
            SurfaceNamer stubNamer = context.getNamer().cloneWithPackageName(context.getNamer().getStubPackageName());
            TypeName rpcStubClassName = stubNamer.getTypeNameConverter().getTypeNameInImplicitPackage(stubNamer.getApiStubSettingsClassName(context.getInterfaceConfig()));
            rpcStubClassName.getAndSaveNicknameIn(typeTable.getTypeTable());
            break;
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName) ImportTypeTable(com.google.api.codegen.transformer.ImportTypeTable) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer)

Aggregations

TypeName (com.google.api.codegen.util.TypeName)19 MessageType (com.google.api.tools.framework.model.MessageType)2 ProtoElement (com.google.api.tools.framework.model.ProtoElement)2 FieldModel (com.google.api.codegen.config.FieldModel)1 Schema (com.google.api.codegen.discovery.Schema)1 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 TypeNameConverter (com.google.api.codegen.util.TypeNameConverter)1 ProtoFile (com.google.api.tools.framework.model.ProtoFile)1