Search in sources :

Example 11 with TypeName

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

the class CSharpModelTypeNameConverter method getImplZeroValue.

@Override
public TypedValue getImplZeroValue(TypeRef type) {
    if (type.isMap()) {
        TypeName keyTypeName = getTypeNameForElementType(type.getMapKeyField().getType());
        TypeName valueTypeName = getTypeNameForElementType(type.getMapValueField().getType());
        TypeName emptyMapTypeName = typeNameConverter.getTypeName("Google.Api.Gax.EmptyDictionary");
        TypeName genericEmptyMapTypeName = new TypeName(emptyMapTypeName.getFullName(), emptyMapTypeName.getNickname(), "%s<%i, %i>", keyTypeName, valueTypeName);
        return TypedValue.create(genericEmptyMapTypeName, "%s.Instance");
    } else if (type.isRepeated()) {
        TypeName elementTypeName = getTypeNameForElementType(type);
        TypeName enumerableTypeName = typeNameConverter.getTypeName("System.Linq.Enumerable");
        TypeName emptyTypeName = new TypeName(enumerableTypeName.getFullName(), enumerableTypeName.getNickname(), "%s.Empty<%i>", elementTypeName);
        return TypedValue.create(emptyTypeName, "%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 {
        if (type.getKind() == Type.TYPE_BYTES) {
            return TypedValue.create(getTypeName(type), "ByteString.Empty");
        } else {
            return TypedValue.create(getTypeName(type), PRIMITIVE_ZERO_VALUE.get(type.getKind()));
        }
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 12 with TypeName

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

the class CSharpModelTypeNameConverter method getEnumValue.

@Override
public TypedValue getEnumValue(TypeRef type, EnumValue value) {
    TypeName enumTypeName = getTypeName(type);
    List<String> enumTypeNameParts = Splitter.on('+').splitToList(enumTypeName.getNickname());
    String enumShortTypeName = enumTypeNameParts.get(enumTypeNameParts.size() - 1);
    String enumValueName = enumNamer.getEnumValueName(enumShortTypeName, value.getSimpleName());
    return TypedValue.create(enumTypeName, "%s." + enumValueName);
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 13 with TypeName

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

the class CSharpSurfaceNamer method getOptionalFieldDefaultValue.

@Override
public String getOptionalFieldDefaultValue(FieldConfig fieldConfig, MethodContext context) {
    // Need to provide defaults for primitives, enums, strings, and repeated (including maps)
    FieldModel type = fieldConfig.getField();
    if (context.getFeatureConfig().useResourceNameFormatOption(fieldConfig)) {
        if (type.isRepeated()) {
            TypeName elementTypeName = new TypeName(getResourceTypeNameObject(fieldConfig.getResourceNameConfig()).toUpperCamel());
            TypeNameConverter typeNameConverter = getTypeNameConverter();
            TypeName enumerableTypeName = typeNameConverter.getTypeName("System.Linq.Enumerable");
            TypeName emptyTypeName = new TypeName(enumerableTypeName.getFullName(), enumerableTypeName.getNickname(), "%s.Empty<%i>", elementTypeName);
            return TypedValue.create(emptyTypeName, "%s()").getValueAndSaveTypeNicknameIn((CSharpTypeTable) typeNameConverter);
        } else {
            return null;
        }
    } else {
        if (type.isPrimitive() || type.isEnum() || type.isRepeated()) {
            return context.getTypeTable().getImplZeroValueAndSaveNicknameFor(type);
        } else {
            return null;
        }
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName) TypeNameConverter(com.google.api.codegen.util.TypeNameConverter) FieldModel(com.google.api.codegen.config.FieldModel)

Example 14 with TypeName

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

the class GoModelTypeNameConverter method getTypeName.

@Override
public TypeName getTypeName(TypeRef type) {
    if (type.isMap()) {
        TypeName keyTypeName = getTypeNameForElementType(type.getMapKeyField().getType());
        TypeName valueTypeName = getTypeNameForElementType(type.getMapValueField().getType());
        return new TypeName("", "", "map[%i]%i", keyTypeName, valueTypeName);
    } else if (type.isRepeated()) {
        TypeName elementTypeName = getTypeNameForElementType(type);
        return new TypeName("", "", "[]%i", elementTypeName);
    } else {
        return getTypeNameForElementType(type);
    }
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 15 with TypeName

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

the class JavaSchemaTypeNameConverter method getTypeNameForTypedResourceName.

private TypeName getTypeNameForTypedResourceName(ResourceNameConfig resourceNameConfig, FieldModel type, String typedResourceShortName) {
    String packageName = implicitPackageName;
    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)

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