private String getParamTypeNoCardinality(ImportTypeTable typeTable, TypeModel type) { if (type.isMessage()) { return "Object"; } else if (type.isEnum()) { return "number"; } else { return typeTable.getFullNameForElementType(type); } }
private List<ImportFileView> generateProtoImports( GapicMethodContext context, Iterable<InitCodeNode> specItemNodes) { Set<ImportFileView> protoImports = new TreeSet<>(importFileViewComparator()); for (InitCodeNode item : specItemNodes) { TypeModel type = item.getType(); // Exclude map entry types // Exclude message types since the samples show the use of dicts rather than protobufs. if (type.isEnum()) { protoImports.add( createImport(context.getNamer().getVersionedDirectoryNamespace(), "enums")); } } return new ArrayList<>(protoImports); }
private String getParamTypeNameForElementType(TypeModel type) { String typeName = getTypeFormatter().getFullNameForElementType(type); if (type.isMessage() || type.isEnum()) { typeName = PythonDocstringUtil.napoleonType(typeName, getVersionedDirectoryNamespace()); } if (type.isMessage()) { return "Union[dict, " + typeName + "]"; } if (type.isEnum()) { return typeName; } return typeName; }
private static void addEnumImports( ImmutableSet.Builder<ImportFileView> imports, MethodContext context, OutputView.PrintView view) { boolean addEnumImports = view.args() .stream() .flatMap(arg -> arg.segments().stream()) .filter(seg -> seg.kind() == PrintArgView.ArgSegmentView.Kind.VARIABLE) .map(seg -> ((PrintArgView.VariableSegmentView) seg).variable().type()) .anyMatch(type -> type != null && type.isEnum()); if (addEnumImports) { ImportTypeView importTypeView = ImportTypeView.newBuilder() .fullName("enums") .type(ImportType.SimpleImport) .nickname("") .build(); imports.add( ImportFileView.newBuilder() .moduleName(context.getNamer().getVersionedDirectoryNamespace()) .types(Collections.singletonList(importTypeView)) .build()); } } }
/** * Converts the {@code VariableView} to a {@link com.google.api.codegen.viewmodel.PrintArgView} * that knows how to print proto types nicely. Has effects only when the variable is an enum. * * <p>In python, calling {@code print(x)} or {@code print(''.format(x))} would only get an int * when {@code x} is an enum. To print the enum name of the value, we need to convert the enum * value to it's name. * * <p>When the type of the enum value {@code x} is a top-level enum {@code EnumType}, we can call * {@code print(enums.EnumType(x).name)} to print the name of {@code x}. * * <p>When the type of the enum value {@code x} is a nested enum {@code MessageType.EnumType}, we * can call {@code print(enums.MessageType.EnumType(x).name)} to print the name of {@code x}. */ @Override public PrintArgView generatePrintArg( MethodContext context, OutputView.VariableView variableView) { if (variableView.type() == null || !variableView.type().isEnum()) { return PrintArgView.newBuilder() .segments(ImmutableList.<ArgSegmentView>of(VariableSegmentView.of(variableView))) .build(); } ImmutableList<ArgSegmentView> segments = ImmutableList.of( TextSegmentView.of(getEnumTypeClassName(context, variableView.type()) + "("), VariableSegmentView.of(variableView), TextSegmentView.of(").name")); return PrintArgView.newBuilder().segments(segments).build(); }
switch (initValueConfig.getInitialValue().getType()) { case Literal: if (item.getType().isEnum()) { value = context.getTypeTable().getEnumValue(item.getType(), value); } else {