Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python/backing store support #3013

Merged
merged 15 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Added backing store support for Python. [#2858](https://github.com/microsoft/kiota/issues/2858)

### Changed

Expand Down
6 changes: 3 additions & 3 deletions src/Kiota.Builder/Refiners/PythonRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
RemoveCancellationParameter(generatedCode);
CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType, CorrectImplements);
cancellationToken.ThrowIfCancellationRequested();
CorrectCoreTypesForBackingStore(generatedCode, "BackingStoreFactorySingleton.__instance.create_backing_store()");
CorrectCoreTypesForBackingStore(generatedCode, "field(default_factory=BackingStoreFactorySingleton(backing_store_factory=None).backing_store_factory.create_backing_store, repr=False)");
AddPropertiesAndMethodTypesImports(generatedCode, true, true, true, codeTypeFilter);
AddParsableImplementsForModelClasses(generatedCode, "Parsable");
cancellationToken.ThrowIfCancellationRequested();
Expand Down Expand Up @@ -82,7 +82,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
CodePropertyKind.AdditionalData,
},
static (_, s) => s.ToSnakeCase(),
_configuration.UsesBackingStore,
false,
false,
string.Empty,
string.Empty);
Expand Down Expand Up @@ -155,7 +155,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
method.Parameters.Any(y => y.IsOfKind(CodeParameterKind.BackingStore)),
$"{AbstractionsPackageName}.store", "BackingStoreFactory", "BackingStoreFactorySingleton"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore),
$"{AbstractionsPackageName}.store", "BackingStore", "BackedModel", "BackingStoreFactorySingleton" ),
$"{AbstractionsPackageName}.store", "BackedModel", "BackingStore", "BackingStoreFactorySingleton" ),
new (static x => x is CodeClass @class && (@class.IsOfKind(CodeClassKind.Model) || x.Parent is CodeClass), "dataclasses", "dataclass, field"),
new (static x => x is CodeClass { OriginalComposedType: CodeIntersectionType intersectionType } && intersectionType.Types.Any(static y => !y.IsExternal) && intersectionType.DiscriminatorInformation.HasBasicDiscriminatorInformation,
$"{AbstractionsPackageName}.serialization", "ParseNodeHelper"),
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/LanguageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static LanguageWriter GetLanguageWriter(GenerationLanguage language, stri
GenerationLanguage.TypeScript => new TypeScriptWriter(outputPath, clientNamespaceName, usesBackingStore),
GenerationLanguage.Ruby => new RubyWriter(outputPath, clientNamespaceName),
GenerationLanguage.PHP => new PhpWriter(outputPath, clientNamespaceName, usesBackingStore),
GenerationLanguage.Python => new PythonWriter(outputPath, clientNamespaceName),
GenerationLanguage.Python => new PythonWriter(outputPath, clientNamespaceName, usesBackingStore),
GenerationLanguage.Go => new GoWriter(outputPath, clientNamespaceName),
GenerationLanguage.Shell => new ShellWriter(outputPath, clientNamespaceName),
GenerationLanguage.Swift => new SwiftWriter(outputPath, clientNamespaceName),
Expand Down
44 changes: 20 additions & 24 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me
writer.WriteLine($"self.{pathParametersProperty.Name.ToSnakeCase()}[\"base_url\"] = self.{requestAdapterPropertyName}.base_url");
}
if (backingStoreParameter != null)
writer.WriteLine($"self.{requestAdapterPropertyName}.enable_backing_store({backingStoreParameter.Name})");
writer.WriteLine($"self.{requestAdapterPropertyName}.enable_backing_store({backingStoreParameter.Name.ToSnakeCase()})");
}
private static void WriteQueryParametersMapper(CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
Expand Down Expand Up @@ -338,15 +338,6 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho
WriteDirectAccessProperties(parentClass, writer);
WriteSetterAccessProperties(parentClass, writer);
WriteSetterAccessPropertiesWithoutDefaults(parentClass, writer);
if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParam)
conventions.AddParametersAssignment(writer,
pathParametersParam.Type.AllTypes.OfType<CodeType>().FirstOrDefault(),
pathParametersParam.Name.ToFirstCharacterLowerCase(),
currentMethod.Parameters
.Where(x => x.IsOfKind(CodeParameterKind.Path))
.Select(x => (x.Type, x.SerializationName, x.Name.ToFirstCharacterLowerCase()))
.ToArray());
AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.PathParameters, CodePropertyKind.PathParameters, writer, conventions.TempDictionaryVarName);
}

if (parentClass.IsOfKind(CodeClassKind.Model))
Expand All @@ -362,15 +353,20 @@ private void WriteDirectAccessProperties(CodeClass parentClass, LanguageWriter w
.ThenBy(static x => x.Name))
{
var returnType = conventions.GetTypeString(propWithDefault.Type, propWithDefault, true, writer);
var defaultValue = propWithDefault.DefaultValue;
if (propWithDefault.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition)
{
defaultValue = $"{enumDefinition.Name.ToFirstCharacterUpperCase()}({defaultValue})";
}
conventions.WriteInLineDescription(propWithDefault.Documentation.Description, writer);
if (parentClass.IsOfKind(CodeClassKind.Model))
{
writer.WriteLine($"{propWithDefault.Name.ToSnakeCase()}: {(propWithDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithDefault.Type.IsNullable ? "]" : string.Empty)} = {propWithDefault.DefaultValue}");
writer.WriteLine($"{propWithDefault.Name.ToSnakeCase()}: {(propWithDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithDefault.Type.IsNullable ? "]" : string.Empty)} = {defaultValue}");
writer.WriteLine();
}
else
{
writer.WriteLine($"self.{conventions.GetAccessModifier(propWithDefault.Access)}{propWithDefault.NamePrefix}{propWithDefault.Name.ToSnakeCase()}: {(propWithDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithDefault.Type.IsNullable ? "]" : string.Empty)} = {propWithDefault.DefaultValue}");
writer.WriteLine($"self.{conventions.GetAccessModifier(propWithDefault.Access)}{propWithDefault.NamePrefix}{propWithDefault.Name.ToSnakeCase()}: {(propWithDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithDefault.Type.IsNullable ? "]" : string.Empty)} = {defaultValue}");
writer.WriteLine();
}
}
Expand All @@ -384,10 +380,20 @@ private void WriteSetterAccessProperties(CodeClass parentClass, LanguageWriter w
.OrderByDescending(static x => x.Kind)
.ThenBy(static x => x.Name))
{
var defaultValue = propWithDefault.DefaultValue;
if (propWithDefault.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition)
{
defaultValue = $"{enumDefinition.Name.ToFirstCharacterUpperCase()}({defaultValue})";
}
var returnType = conventions.GetTypeString(propWithDefault.Type, propWithDefault, true, writer);
conventions.WriteInLineDescription(propWithDefault.Documentation.Description, writer);
var setterString = $"{propWithDefault.Name.ToSnakeCase()}: {(propWithDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithDefault.Type.IsNullable ? "]" : string.Empty)} = {defaultValue}";
if (parentClass.IsOfKind(CodeClassKind.Model))
writer.WriteLine($"{propWithDefault.Name.ToSnakeCase()} = {propWithDefault.DefaultValue}");
{
writer.WriteLine($"{setterString}");
}
else
writer.WriteLine($"self.{propWithDefault.Name.ToSnakeCase()} = {propWithDefault.DefaultValue}");
writer.WriteLine($"self.{setterString}");
}
}
private void WriteSetterAccessPropertiesWithoutDefaults(CodeClass parentClass, LanguageWriter writer)
Expand All @@ -405,16 +411,6 @@ private void WriteSetterAccessPropertiesWithoutDefaults(CodeClass parentClass, L
writer.WriteLine($"self.{conventions.GetAccessModifier(propWithoutDefault.Access)}{propWithoutDefault.NamePrefix}{propWithoutDefault.Name.ToSnakeCase()}: {(propWithoutDefault.Type.IsNullable ? "Optional[" : string.Empty)}{returnType}{(propWithoutDefault.Type.IsNullable ? "]" : string.Empty)} = None");
}
}
private static void AssignPropertyFromParameter(CodeClass parentClass, CodeMethod currentMethod, CodeParameterKind parameterKind, CodePropertyKind propertyKind, LanguageWriter writer, string? variableName = default)
{
if (parentClass.GetPropertyOfKind(propertyKind) is CodeProperty property)
{
if (!string.IsNullOrEmpty(variableName))
writer.WriteLine($"self.{property.Name.ToSnakeCase()} = {variableName.ToSnakeCase()}");
else if (currentMethod.Parameters.OfKind(parameterKind) is CodeParameter parameter)
writer.WriteLine($"self.{property.Name.ToSnakeCase()} = {parameter.Name.ToSnakeCase()}");
}
}
private static void WriteSetterBody(CodeMethod codeElement, LanguageWriter writer, CodeClass parentClass)
{
if (!parentClass.IsOfKind(CodeClassKind.Model))
Expand Down
Loading
Loading