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); } }
@Override public String getParamTypeName(ImportTypeTable typeTable, TypeModel type) { String cardinalityComment = ""; if (type.isRepeated()) { if (type.isMap()) { String keyType = getParamTypeName(typeTable, type.getMapKeyType()); String valueType = getParamTypeName(typeTable, type.getMapValueType()); return String.format("Object.<%s, %s>", keyType, valueType); } else { cardinalityComment = "[]"; } } return String.format("%s%s", getParamTypeNoCardinality(typeTable, type), cardinalityComment); }
@Override public String produce(TypeModel typeRef, Name identifier) { if (typeRef.isStringType()) { return identifier.toLowerCamel() + Integer.toString(identifier.hashCode()); } else if (typeRef.isBooleanType()) { return identifier.hashCode() % 2 == 0 ? "true" : "false"; } else if (typeRef.isBytesType()) { byte lowByte = (byte) (identifier.hashCode()); return Byte.toString(lowByte); } else if (typeRef.getPrimitiveTypeName().contains("int") || typeRef.getPrimitiveTypeName().contains("fixed")) { int value = identifier.hashCode(); if (value == Integer.MIN_VALUE) { value = 0; } return "" + Math.abs(value); } else if (typeRef.isDoubleType() || typeRef.isFloatType()) { return Double.toString(identifier.hashCode() / 10); } else { throw new RuntimeException("Unknown type in ValueProducer: " + typeRef.getTypeName()); } } }
private static void validateKeyValue(TypeModel parentType, String key) { if (parentType.isMap()) { TypeModel keyType = parentType.getMapKeyType(); validateValue(keyType, key); } else if (parentType.isRepeated()) { validateValue(INT_TYPE, key); } else { // Don't validate message types, field will be missing for a bad key } }
/** The function name to get a field having the given type and name. */ public String getFieldGetFunctionName(TypeModel type, Name identifier) { return getFieldGetFunctionName( identifier, MapType.ofMap(type.isMap()), Cardinality.ofRepeated(type.isRepeated())); }
switch (lineType) { case StructureInitLine: if (!typeRef.isMessage() || typeRef.isRepeated()) { throw new IllegalArgumentException( "typeRef " + typeRef + " not compatible with " + lineType); if (typeRef.isMap() || !typeRef.isRepeated()) { throw new IllegalArgumentException( "typeRef " + typeRef + " not compatible with " + lineType); if (!typeRef.isMap()) { throw new IllegalArgumentException( "typeRef " + typeRef + " not compatible with " + lineType);
getChildSuggestedName(suggestedName, lineType, child), getChildFieldConfig(context.fieldConfigMap(), fieldConfig, type, child.key)); if (type.isMessage()) { child.oneofConfig = type.getOneOfConfig(child.getKey()); validateValue(type, initValueConfig.getInitialValue().getValue()); } else if (initValueConfig.isEmpty() && type.isPrimitive() && !type.isRepeated() && valueGenerator != null) { String newValue = valueGenerator.getAndStoreValue(type, identifier);
boolean isMessageField = field.isMessage() && !field.isMap(); boolean isMapContainingMessage = field.isMap() && field.getType().getMapValueType().isMessage(); if (isMessageField || isMapContainingMessage) { String messageType; if (isMapContainingMessage) { messageType = context.getTypeTable().getFullNameForElementType(field.getType().getMapValueType()); } else { messageType = context.getTypeTable().getFullNameForElementType(field);
@Override public TypeName getTypeNameForResourceNameElementType( FieldConfig fieldConfig, String typedResourceShortName) { return getTypeNameForTypedResourceName( fieldConfig, fieldConfig.getField().getType().makeOptional(), typedResourceShortName); }
if (item.getType().isRepeated()) { initValue = RepeatedResourceNameInitValueView.newBuilder() } else if (initValueConfig.hasFormattingConfig() && !item.getType().isRepeated()) { if (context.getFeatureConfig().enableStringFormatFunctions() || fieldConfig.getResourceNameConfig() == null) { switch (initValueConfig.getInitialValue().getType()) { case Literal: if (item.getType().isEnum()) { value = context.getTypeTable().getEnumValue(item.getType(), value); } else { simpleInitValue.initialValue( context.getTypeTable().getSnippetZeroValueAndSaveNicknameFor(item.getType())); simpleInitValue.isRepeated(item.getType().isRepeated()); if (isRequired(item.getFieldConfig(), context)) { String name = getVariableName(context, item);
private String getResponseTypeNameForElementType(TypeModel type) { if (type.isMessage()) { String typeName = getTypeFormatter().getFullNameForElementType(type); return PythonDocstringUtil.napoleonType(typeName, getVersionedDirectoryNamespace()); } return getParamTypeNameForElementType(type); }
@Test public void testAccessorNewVariableScalarTypeForCollectionFail() { TypeModel oldVarTypeModel = mock(TypeModel.class); assertThat(parent.put("old_var", oldVarTypeModel, "OldVarTypeName")).isTrue(); Scanner scanner = new Scanner("old_var"); when(oldVarTypeModel.isRepeated()).thenReturn(false); when(namer.localVarName(Name.from("old_var"))).thenReturn("oldVar"); when(namer.getAndSaveTypeName(typeTable, oldVarTypeModel)).thenReturn("OldVarTypeName"); try { OutputView.VariableView variableView = accessorNewVariable(scanner, context, valueSet, parent, "newVar", true); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains("is not a repeated field"); } }
private InitCodeLineView generateMapInitCodeLine(MethodContext context, InitCodeNode item) { MapInitCodeLineView.Builder surfaceLine = MapInitCodeLineView.newBuilder(); SurfaceNamer namer = context.getNamer(); ImportTypeTable typeTable = context.getTypeTable(); surfaceLine.lineType(InitCodeLineType.MapInitLine); surfaceLine.identifier(namer.localVarName(item.getIdentifier())); surfaceLine.keyTypeName(typeTable.getAndSaveNicknameFor(item.getType().getMapKeyType())); surfaceLine.valueTypeName(typeTable.getAndSaveNicknameFor(item.getType().getMapValueType())); List<MapEntryView> entries = new ArrayList<>(); for (Map.Entry<String, InitCodeNode> entry : item.getChildren().entrySet()) { MapEntryView.Builder mapEntry = MapEntryView.newBuilder(); mapEntry.key(typeTable.renderPrimitiveValue(item.getType().getMapKeyType(), entry.getKey())); mapEntry.valueString(context.getNamer().localVarName(entry.getValue().getIdentifier())); mapEntry.value( generateSurfaceInitCodeLine(context, entry.getValue(), entries.isEmpty(), false)); entries.add(mapEntry.build()); } surfaceLine.initEntries(entries); return surfaceLine.build(); }
public String getAndStoreValue(TypeModel type, Name identifier) { if (!valueTable.containsKey(identifier)) { String value = producer.produce(type, identifier); while (type.getPrimitiveTypeName() != "bool" && valueTable.containsValue(value)) { // If the value already exists regenerate using a deterministically different identifier. identifier = identifier.join("1"); value = producer.produce(type, identifier); } valueTable.put(identifier, value); } return valueTable.get(identifier); } }
private InitCodeContext createResponseInitCodeContext( MethodContext context, SymbolTable symbolTable) { TypeModel outputType = context.getMethodModel().getOutputType(); if (context.getMethodConfig().isLongRunningOperation()) { outputType = context.getMethodConfig().getLongRunningConfig().getReturnType(); } return InitCodeContext.newBuilder() .initObjectType(outputType) .symbolTable(symbolTable) .suggestedName(Name.from("expected_response")) .initFieldConfigStrings(ImmutableList.of()) .initValueConfigMap(ImmutableMap.of()) .initFields(responseInitFields(outputType.getFields())) .fieldConfigMap(context.getProductConfig().getDefaultResourceNameFieldConfigMap()) .valueGenerator(valueGenerator) .additionalInitCodeNodes(createMockResponseAdditionalSubTrees(context)) .build(); }
/** The function name to set a field having the given type and name. */ public String getFieldSetFunctionName(TypeModel type, Name identifier) { return getFieldSetFunctionName( identifier, MapType.ofMap(type.isMap()), Cardinality.ofRepeated(type.isRepeated())); } /** The function name to get a field having the given name. */
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; }
boolean isMessageField = field.isMessage() && !field.isMap(); boolean isMapContainingMessage = field.isMap() && field.getType().getMapValueType().isMessage(); if (isMessageField || isMapContainingMessage) { String messageType; if (isMapContainingMessage) { messageType = context.getTypeTable().getFullNameForElementType(field.getType().getMapValueType()); } else { messageType = context.getTypeTable().getFullNameForElementType(field);
@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); }