Skip to content

Commit

Permalink
Added expected data type for setString method which takes a primitive…
Browse files Browse the repository at this point in the history
… function that returns an array of mixed types
  • Loading branch information
Microsoft Graph DevX Tooling authored and Microsoft Graph DevX Tooling committed Dec 4, 2024
1 parent 994b96c commit 6e0bcc6
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,35 +475,42 @@ _ when conventions.PrimitiveTypes.Contains(lowerCaseProp) => $"write{lowerCasePr
}

private const string ParseNodeVarName = "$parseNode";
private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod method)
private (string, string) GetDeserializationMethodName(CodeTypeBase propType, CodeMethod method)
{
var isCollection = propType.CollectionKind != CodeTypeBase.CodeTypeCollectionKind.None;
var propertyType = conventions.GetTypeString(propType, method, false);
var parseNodeMethod = string.Empty;
var parseNodeMethod = (string.Empty, string.Empty);
if (propType is CodeType currentType)
{
if (isCollection)
parseNodeMethod = currentType.TypeDefinition switch
{
CodeEnum enumType => $"getCollectionOfEnumValues({enumType.Name.ToFirstCharacterUpperCase()}::class)",
_ => $"getCollectionOfObjectValues([{conventions.TranslateType(propType)}::class, '{CreateDiscriminatorMethodName}'])"
CodeEnum enumType => (string.Empty, $"getCollectionOfEnumValues({enumType.Name.ToFirstCharacterUpperCase()}::class)"),
CodeClass => (string.Empty, $"getCollectionOfObjectValues([{conventions.TranslateType(propType)}::class, '{CreateDiscriminatorMethodName}'])"),
_ => (conventions.TranslateType(propType), $"getCollectionOfPrimitiveValues('{conventions.TranslateType(propType)}')")
};
else if (currentType.TypeDefinition is CodeEnum)
parseNodeMethod = $"getEnumValue({propertyType.ToFirstCharacterUpperCase()}::class)";
parseNodeMethod = (string.Empty, $"getEnumValue({propertyType.ToFirstCharacterUpperCase()}::class)");
}

var lowerCaseType = propertyType.ToLowerInvariant();
return string.IsNullOrEmpty(parseNodeMethod) ? lowerCaseType switch
{
"int" => "getIntegerValue()",
"bool" => "getBooleanValue()",
"number" => "getIntegerValue()",
"decimal" or "double" => "getFloatValue()",
"streaminterface" => "getBinaryContent()",
"byte" => "getByteValue()",
_ when conventions.PrimitiveTypes.Contains(lowerCaseType) => $"get{propertyType.ToFirstCharacterUpperCase()}Value()",
_ => $"getObjectValue([{propertyType.ToFirstCharacterUpperCase()}::class, '{CreateDiscriminatorMethodName}'])",
} : parseNodeMethod;
var res = parseNodeMethod;
if (string.IsNullOrEmpty(parseNodeMethod.Item2))
res = (string.Empty, lowerCaseType switch
{
"int" => "getIntegerValue()",
"bool" => "getBooleanValue()",
"number" => "getIntegerValue()",
"decimal" or "double" => "getFloatValue()",
"streaminterface" => "getBinaryContent()",
"byte" => "getByteValue()",
_ when conventions.PrimitiveTypes.Contains(lowerCaseType) =>
$"get{propertyType.ToFirstCharacterUpperCase()}Value()",
_ =>
$"getObjectValue([{propertyType.ToFirstCharacterUpperCase()}::class, '{CreateDiscriminatorMethodName}'])",
});

return res;
}

private void WriteSetterBody(LanguageWriter writer, CodeMethod codeElement, CodeClass parentClass)
Expand Down Expand Up @@ -672,7 +679,7 @@ private void WriteDeserializerPropertyCallback(CodeProperty property, CodeMethod
writer.WriteLine("},");
return;
}
writer.WriteLine($"'{property.WireName}' => fn(ParseNode $n) => $o->{property.Setter!.Name.ToFirstCharacterLowerCase()}($n->{GetDeserializationMethodName(property.Type, method)}),");
writer.WriteLine($"'{property.WireName}' => fn(ParseNode $n) => $o->{property.Setter!.Name.ToFirstCharacterLowerCase()}($n->{GetDeserializationMethodName(property.Type, method).Item2}),");
}

private static void WriteDeserializerBodyForIntersectionModel(CodeClass parentClass, LanguageWriter writer)
Expand Down Expand Up @@ -903,7 +910,8 @@ private void WriteFactoryMethodBodyForIntersectionModel(CodeMethod codeElement,
{
if (property.Type is CodeType propertyType)
{
var deserializationMethodName = $"{ParseNodeVarName}->{GetDeserializationMethodName(propertyType, codeElement)}";
var methodName = GetDeserializationMethodName(propertyType, codeElement);
var deserializationMethodName = $"{ParseNodeVarName}->{methodName.Item2}";
writer.StartBlock($"{(includeElse ? "} else " : string.Empty)}if ({deserializationMethodName} !== null) {{");
writer.WriteLine($"{ResultVarName}->{property.Setter!.Name.ToFirstCharacterLowerCase()}({deserializationMethodName});");
writer.DecreaseIndent();
Expand Down Expand Up @@ -973,9 +981,16 @@ private void WriteFactoryMethodBodyForUnionModelForUnDiscriminatedTypes(CodeMeth
.ToArray();
foreach (var property in otherProps)
{
var serializationMethodName = $"{ParseNodeVarName}->{GetDeserializationMethodName(property.Type, currentElement)}";
var methodName = GetDeserializationMethodName(property.Type, currentElement);
var serializationMethodName = $"{ParseNodeVarName}->{methodName.Item2}";
const string finalValueName = "$finalValue";
writer.StartBlock($"{(includeElse ? "} else " : string.Empty)}if ({serializationMethodName} !== null) {{");
writer.WriteLine($"{ResultVarName}->{property.Setter!.Name.ToFirstCharacterLowerCase()}({serializationMethodName});");
if (!string.IsNullOrEmpty(methodName.Item1))
{
writer.WriteLine($"/** @var array<{methodName.Item1}> {finalValueName} */");
}
writer.WriteLine($"{finalValueName} = {serializationMethodName};");
writer.WriteLine($"{ResultVarName}->{property.Setter!.Name.ToFirstCharacterLowerCase()}({finalValueName});");
writer.DecreaseIndent();
if (!includeElse)
includeElse = true;
Expand Down

0 comments on commit 6e0bcc6

Please sign in to comment.