@Override public TypeName getTypeNameForResourceNameElementType( FieldConfig fieldConfig, String typedResourceShortName) { return getTypeNameForTypedResourceName( fieldConfig, fieldConfig.getField().getType().makeOptional(), typedResourceShortName); }
private static TypeModel getChildType(TypeModel parentType, String key) { if (parentType.isMap()) { return parentType.getMapValueType(); } else if (parentType.isRepeated()) { // Using the Optional cardinality replaces the Repeated cardinality return parentType.makeOptional(); } else if (parentType.isMessage()) { FieldModel childField = parentType.getField(key); if (childField == null) { throw new IllegalArgumentException( "Message type " + parentType + " does not have field " + key); } return childField.getType(); } else { throw new IllegalArgumentException( "Primitive type " + parentType + " cannot have children. Child key: " + key); } }
@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); }
private InitCodeLineView generateListInitCodeLine(MethodContext context, InitCodeNode item) { ListInitCodeLineView.Builder surfaceLine = ListInitCodeLineView.newBuilder(); FieldConfig fieldConfig = item.getFieldConfig(); SurfaceNamer namer = context.getNamer(); ImportTypeTable typeTable = context.getTypeTable(); surfaceLine.lineType(InitCodeLineType.ListInitLine); surfaceLine.identifier(namer.localVarName(item.getIdentifier())); if (context.getFeatureConfig().useResourceNameFormatOptionInSample(context, fieldConfig)) { surfaceLine.elementTypeName(namer.getAndSaveElementResourceTypeName(typeTable, fieldConfig)); } else { surfaceLine.elementTypeName( typeTable.getAndSaveNicknameForElementType(item.getType().makeOptional())); } List<String> entries = new ArrayList<>(); List<InitCodeLineView> elements = new ArrayList<>(); for (InitCodeNode child : item.getChildren().values()) { entries.add(namer.localVarName(child.getIdentifier())); elements.add(generateSurfaceInitCodeLine(context, child, elements.isEmpty(), false)); } surfaceLine.elementIdentifiers(entries); surfaceLine.elements(elements); return surfaceLine.build(); }
context.getTypeTable().getFullNameFor(field.getType().makeOptional()); if (field.isRepeated()) { paramDoc.typeName("int[]");
@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); }
.getField() .getType() .makeOptional(); } else if (context.getMethodConfig().isLongRunningOperation()) { type = context.getMethodConfig().getLongRunningConfig().getReturnType(); config.input()); type = type.makeOptional(); accessors.add(AccessorView.IndexView.newBuilder().index(config.tokenStr()).build()); valueSet.getId(), config.input()); type = type.makeOptional(); // "optional" is how protobuf defines singular fields