-
Notifications
You must be signed in to change notification settings - Fork 5k
Implement resource serialization #50229
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
162f1f8
wip
live1206 14a1817
Merge branch 'main' into resource-serialization
live1206 fe2c10c
implement methods
live1206 4edb07b
Merge remote-tracking branch 'origin/main' into resource-serialization
live1206 30bdf55
bump version of Azure.Generator
live1206 1d29e38
cleanup
live1206 62ead0c
minor
live1206 da60fa9
add description for parameter
live1206 f6c8f0a
address comments
live1206 29280dc
Merge remote-tracking branch 'origin/main' into resource-serialization
live1206 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...-mgmt/generator/Azure.Generator.Management/src/Providers/ResourceSerializationProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.TypeSpec.Generator.ClientModel.Snippets; | ||
using Microsoft.TypeSpec.Generator.Expressions; | ||
using Microsoft.TypeSpec.Generator.Primitives; | ||
using Microsoft.TypeSpec.Generator.Providers; | ||
using System; | ||
using System.ClientModel.Primitives; | ||
using System.IO; | ||
using System.Text.Json; | ||
using static Microsoft.TypeSpec.Generator.Snippets.Snippet; | ||
|
||
namespace Azure.Generator.Management.Providers | ||
{ | ||
internal class ResourceSerializationProvider : TypeProvider | ||
{ | ||
private readonly FieldProvider _dataField; | ||
private readonly CSharpType _resourceDataType; | ||
private readonly ResourceClientProvider _resoruce; | ||
private readonly CSharpType _jsonModelInterfaceType; | ||
public ResourceSerializationProvider(ResourceClientProvider resource) | ||
{ | ||
_resoruce = resource; | ||
_resourceDataType = resource.ResourceData.Type; | ||
_jsonModelInterfaceType = new CSharpType(typeof(IJsonModel<>), _resourceDataType); | ||
_dataField = new FieldProvider(FieldModifiers.Private | FieldModifiers.Static, _jsonModelInterfaceType, "s_dataDeserializationInstance", this); | ||
} | ||
|
||
protected override string BuildName() => _resoruce.Name; | ||
|
||
protected override string BuildRelativeFilePath() | ||
=> Path.Combine("src", "Generated", $"{Name}.Serialization.cs"); | ||
|
||
protected override TypeSignatureModifiers BuildDeclarationModifiers() | ||
=> TypeSignatureModifiers.Public | TypeSignatureModifiers.Partial; | ||
|
||
protected override CSharpType[] BuildImplements() => [_jsonModelInterfaceType]; | ||
|
||
protected override FieldProvider[] BuildFields() => [_dataField]; | ||
|
||
protected override PropertyProvider[] BuildProperties() => | ||
[ | ||
new PropertyProvider(null, MethodSignatureModifiers.Private | MethodSignatureModifiers.Static, _jsonModelInterfaceType, "DataDeserializationInstance", new ExpressionPropertyBody(new AssignmentExpression(_dataField, New.Instance(_resourceDataType), true)), this) | ||
]; | ||
|
||
protected override MethodProvider[] BuildMethods() | ||
{ | ||
var options = new ParameterProvider("options", $"The client options for reading and writing models.", typeof(ModelReaderWriterOptions)); | ||
var iModelTInterface = new CSharpType(typeof(IPersistableModel<>), _resourceDataType); | ||
var data = new ParameterProvider("data", $"The binary data to be processed.", typeof(BinaryData)); | ||
|
||
// void IJsonModel<T>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) | ||
var writer = new ParameterProvider("writer", $"The writer to serialize the model to.", typeof(Utf8JsonWriter)); | ||
var jsonModelWriteMethod = new MethodProvider( | ||
new MethodSignature(nameof(IJsonModel<object>.Write), null, MethodSignatureModifiers.None, null, null, [writer, options], ExplicitInterface: _jsonModelInterfaceType), | ||
// => ((IJsonModel<T>)Data).Write(writer, options); | ||
new MemberExpression(null, "Data").CastTo(_jsonModelInterfaceType).Invoke(nameof(IJsonModel<object>.Write), writer, options), | ||
this); | ||
|
||
// T IJsonModel<T>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) | ||
var reader = new ParameterProvider("reader", $"The reader for deserializing the model.", typeof(Utf8JsonReader), isRef: true); | ||
var jsonModelCreatemethod = new MethodProvider( | ||
new MethodSignature(nameof(IJsonModel<object>.Create), null, MethodSignatureModifiers.None, _resourceDataType, null, [reader, options], ExplicitInterface: _jsonModelInterfaceType), | ||
// => DataDeserializationInstance.Create(reader, options); | ||
new MemberExpression(null, "DataDeserializationInstance").Invoke(nameof(IJsonModel<object>.Create), reader, options), | ||
this); | ||
|
||
// BinaryData IPersistableModel<T>.Write(ModelReaderWriterOptions options) | ||
var persistableWriteMethod = new MethodProvider( | ||
new MethodSignature(nameof(IPersistableModel<object>.Write), null, MethodSignatureModifiers.None, typeof(BinaryData), null, [options], ExplicitInterface: iModelTInterface), | ||
// => ModelReaderWriter.Write<ResourceData>(Data, options); | ||
Static(typeof(ModelReaderWriter)).Invoke("Write", [new MemberExpression(null, "Data"), options, ModelReaderWriterContextSnippets.Default], [_resourceDataType], false), | ||
this); | ||
|
||
// T IPersistableModel<T>.Create(BinaryData data, ModelReaderWriterOptions options) | ||
var persistableCreateMethod = new MethodProvider( | ||
new MethodSignature(nameof(IPersistableModel<object>.Create), null, MethodSignatureModifiers.None, _resourceDataType, null, [data, options], ExplicitInterface: iModelTInterface), | ||
// => ModelReaderWriter.Read<ResourceData>(new BinaryData(reader.ValueSequence)); | ||
Static(typeof(ModelReaderWriter)).Invoke("Read", [data, options, ModelReaderWriterContextSnippets.Default], [_resourceDataType], false), | ||
this); | ||
|
||
// ModelReaderWriterFormat IPersistableModel<T>.GetFormatFromOptions(ModelReaderWriterOptions options) | ||
var persistableGetFormatMethod = new MethodProvider( | ||
new MethodSignature(nameof(IPersistableModel<object>.GetFormatFromOptions), null, MethodSignatureModifiers.None, typeof(string), null, [options], ExplicitInterface: iModelTInterface), | ||
// => DataDeserializationInstance.GetFormatFromOptions(options); | ||
new MemberExpression(null, "DataDeserializationInstance").Invoke(nameof(IPersistableModel<object>.GetFormatFromOptions), options), | ||
this); | ||
|
||
return [jsonModelWriteMethod, jsonModelCreatemethod, persistableWriteMethod, persistableCreateMethod, persistableGetFormatMethod]; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...gmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/FooResource.Serialization.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
...generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/FoosListAsyncCollectionResult.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
...erator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/FoosListAsyncCollectionResultOfT.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.