@Test public void testAccessorNewVariableWithIndex() { Scanner scanner = new Scanner("old_var.property[0]"); when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar"); TypeModel oldVarTypeModel = mock(TypeModel.class); assertThat(parent.put("old_var", oldVarTypeModel, "OldVarType")).isTrue(); when(oldVarTypeModel.isMessage()).thenReturn(true); when(oldVarTypeModel.isRepeated()).thenReturn(false); when(oldVarTypeModel.isMap()).thenReturn(false); FieldModel propertyFieldModel = mock(FieldModel.class); when(oldVarTypeModel.getField("property")).thenReturn(propertyFieldModel); TypeModel propertyTypeModel = mock(TypeModel.class); when(namer.getFieldGetFunctionName(propertyFieldModel)).thenReturn("getProperty"); when(propertyTypeModel.isRepeated()).thenReturn(true); TypeModel optionalPropertyTypeModel = mock(TypeModel.class); when(propertyTypeModel.makeOptional()).thenReturn(optionalPropertyTypeModel); when(namer.getAndSaveTypeName(typeTable, optionalPropertyTypeModel)) .thenReturn("OptionalPropertyTypeName"); when(propertyFieldModel.getType()).thenReturn(propertyTypeModel); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("oldVar"); assertThat(variableView.accessors()) .containsExactly( AccessorView.FieldView.newBuilder().field("getProperty").build(), AccessorView.IndexView.newBuilder().index("0").build()) .inOrder(); assertThat(parent.getTypeName("newVar")).isEqualTo("OptionalPropertyTypeName"); assertThat(parent.getTypeModel("newVar")).isEqualTo(optionalPropertyTypeModel); }
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()); } } }
boolean scalarTypeForCollection) { OutputView.VariableView.Builder view = OutputView.VariableView.newBuilder();
/** * 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(); }
@Test public void testAccessorNewVariableWithAccessors() { Scanner scanner = new Scanner("old_var.property"); when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar"); TypeModel oldVarTypeModel = mock(TypeModel.class); assertThat(parent.put("old_var", oldVarTypeModel, "OldVarType")).isTrue(); when(oldVarTypeModel.isMessage()).thenReturn(true); when(oldVarTypeModel.isRepeated()).thenReturn(false); when(oldVarTypeModel.isMap()).thenReturn(false); FieldModel propertyFieldModel = mock(FieldModel.class); when(oldVarTypeModel.getField("property")).thenReturn(propertyFieldModel); TypeModel propertyTypeModel = mock(TypeModel.class); when(namer.getFieldGetFunctionName(propertyFieldModel)).thenReturn("getProperty"); when(namer.getAndSaveTypeName(typeTable, propertyTypeModel)).thenReturn("PropertyTypeName"); when(propertyFieldModel.getType()).thenReturn(propertyTypeModel); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("oldVar"); assertThat(variableView.accessors()) .containsExactly(AccessorView.FieldView.newBuilder().field("getProperty").build()) .inOrder(); assertThat(parent.getTypeName("newVar")).isEqualTo("PropertyTypeName"); assertThat(parent.getTypeModel("newVar")).isEqualTo(propertyTypeModel); }
@Test public void testAccessorNewVariablePageStreamingResponse() { Scanner scanner = new Scanner("$resp"); when(config.getPageStreaming()).thenReturn(pageStreamingConfig); when(pageStreamingConfig.getResourcesFieldConfig()).thenReturn(resourceFieldConfig); when(featureConfig.useResourceNameFormatOption(resourceFieldConfig)).thenReturn(false); FieldModel fieldModel = mock(FieldModel.class); when(resourceFieldConfig.getField()).thenReturn(fieldModel); TypeModel typeModel = mock(TypeModel.class); when(fieldModel.getType()).thenReturn(typeModel); when(typeModel.makeOptional()).thenReturn(typeModel); when(namer.getAndSaveTypeName(typeTable, typeModel)).thenReturn("TypeName"); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("sampleResponseVarName"); assertThat(variableView.accessors()).isEmpty(); assertThat(parent.getTypeName("newVar")).isEqualTo("TypeName"); assertThat(parent.getTypeModel("newVar")).isEqualTo(typeModel); }
@Test public void testAccessorNewVariablePageStreamingResourceNameResponse() { Scanner scanner = new Scanner("$resp"); when(config.getPageStreaming()).thenReturn(pageStreamingConfig); when(pageStreamingConfig.getResourcesFieldConfig()).thenReturn(resourceFieldConfig); when(namer.getAndSaveElementResourceTypeName(typeTable, resourceFieldConfig)) .thenReturn("ShelfBookName"); when(featureConfig.useResourceNameFormatOption(resourceFieldConfig)).thenReturn(true); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("sampleResponseVarName"); assertThat(variableView.accessors()).isEmpty(); assertThat(parent.getTypeName("newVar")).isEqualTo("ShelfBookName"); assertThat(parent.getTypeModel("newVar")).isNull(); }
@Test public void testAccessorNewVariableFromScopeTable() { TypeModel oldVarTypeModel = mock(TypeModel.class); assertThat(parent.put("old_var", oldVarTypeModel, "OldVarTypeName")).isTrue(); Scanner scanner = new Scanner("old_var"); when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar"); when(namer.getAndSaveTypeName(typeTable, oldVarTypeModel)).thenReturn("OldVarTypeName"); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("oldVar"); assertThat(variableView.accessors()).isEmpty(); assertThat(parent.getTypeName("newVar")).isEqualTo("OldVarTypeName"); assertThat(parent.getTypeModel("newVar")).isEqualTo(oldVarTypeModel); }
@Test public void testAccessorNewVariableResponse() { Scanner scanner = new Scanner("$resp"); when(config.getPageStreaming()).thenReturn(null); TypeModel typeModel = mock(TypeModel.class); when(namer.getAndSaveTypeName(typeTable, typeModel)).thenReturn("TypeName"); when(model.getOutputType()).thenReturn(typeModel); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("sampleResponseVarName"); assertThat(variableView.accessors()).isEmpty(); assertThat(parent.getTypeName("newVar")).isEqualTo("TypeName"); assertThat(parent.getTypeModel("newVar")).isEqualTo(typeModel); }
@Test public void testAccessorNewVariableResourceNameFromScopeTable() { assertThat(parent.put("old_var", null, "ShelfBookName")).isTrue(); Scanner scanner = new Scanner("old_var"); when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar"); OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", false); assertThat(variableView.variable()).isEqualTo("oldVar"); assertThat(variableView.accessors()).isEmpty(); assertThat(parent.getTypeName("newVar")).isEqualTo("ShelfBookName"); assertThat(parent.getTypeModel("newVar")).isEqualTo(null); }