Refactor the sample composer to use CodeWriter instead#3672
Merged
ArcturusZhang merged 45 commits intoAzure:feature/v3from Sep 14, 2023
Merged
Refactor the sample composer to use CodeWriter instead#3672ArcturusZhang merged 45 commits intoAzure:feature/v3from
CodeWriter instead#3672ArcturusZhang merged 45 commits intoAzure:feature/v3from
Conversation
03f34eb to
c6fe04a
Compare
ArcturusZhang
commented
Aug 18, 2023
test/TestProjects/FirstTest-TypeSpec/src/Generated/Docs/FirstTestTypeSpecClient.xml
Outdated
Show resolved
Hide resolved
ArcturusZhang
commented
Aug 18, 2023
test/TestProjects/FirstTest-TypeSpec/src/Generated/Docs/FirstTestTypeSpecClient.xml
Outdated
Show resolved
Hide resolved
ArcturusZhang
commented
Aug 21, 2023
test/TestProjects/FirstTest-TypeSpec/tests/Generated/Samples/Samples_FirstTestTypeSpecClient.cs
Show resolved
Hide resolved
a5d807b to
89b38a4
Compare
…r how to implement the code writer part to reuse some code in the serialization writer
9c99a4f to
c4d70f4
Compare
ArcturusZhang
commented
Sep 7, 2023
samples/AnomalyDetector/tests/Generated/Samples/Samples_AnomalyDetectorClient.cs
Outdated
Show resolved
Hide resolved
chunyu3
reviewed
Sep 8, 2023
samples/AnomalyDetector/src/Generated/Docs/AnomalyDetectorClient.xml
Outdated
Show resolved
Hide resolved
chunyu3
reviewed
Sep 8, 2023
src/AutoRest.CSharp/Common/Input/InputTypes/Examples/InputOperationExample.cs
Show resolved
Hide resolved
chunyu3
approved these changes
Sep 13, 2023
lirenhe
approved these changes
Sep 14, 2023
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #3383
Fixes #3337
Fixes #3499
Fixes #3732
Depends on #3711Possible follow ups:
Description
How this example writer work now?
This PR separates our previous sample generator into two parts:
ExampleMockValueBuilderclass inAutoRest.CSharp.Common.Input.ExamplesnamespaceThe biggest benefit of this separation is that in the future, if we want to consume the examples in the typespec, we just need an extra thing to deserialize those values into the structures we are having right now in this PR and the rest part introduced in this PR will write them into sample code.
How does the example writer write sample code?
Now the sample writer will write things based on the CSharpType of the property or the parameter regardless what type of the example value is. We must consider this because we would like to let the generator always generates code that could compile properly.
Therefore in this PR, when we are writing the code, we will always check the type of the property or parameter first, and then try to write the value in a value that could compile. If the type or format of the value mismatches, the generator will always generate
defaultthere to ensure it could compile properly.This whole mechanism lives in an extension class of
CodeWritercalledCodeWriterExampleExtensions.Refactors around the xml writer
Now our generator uses
XDocumentto build a xml document instead of usingStringBuilderand putting everything manually by ourselves.When writing the standalone xml document file, the generator will not write the code inside it because we need the sample code to be reduced and formatted by Rolsyn before we put them in the xml file.
Therefore the generator now just writes a test method name to where the sample code should be.
In the
GeneratedCodeWorkspace, this PR adds a step after every file has been reduced and formatted to read the generated sample file, and parse it to get the implementation by the test method name we wrote before in the xml file, then replace it with the reduced and formatted code.Changes on the generated samples
RequestContextis required on the signature, we will passnullinstead ofnew RequestContext(). Because we make it required to avoid ambiguous calls between protocol and convenience,nullvalue is still allowed there.Dictionary<string, object>to avoid the case that we get compilation errors when the property's serialized name has special characters or collides with C# keywords in anonymous objects (see issue [Sample] The sample composer should never change the serialized name of a property #3499 for details)Issues in anonymous objects
Previously we always use anonymous object as the initialization in the samples for protocol methods which is nice to read for our customers. But unfortunately, as in #3499 , when the serialized name of a property is not a proper identifier in C# (which happens occasionally, such as a property named
continuewhich is a C# keyword, or when a property name has hyphen in it), we no longer could construct it using the anonymous object syntax. When this happens, we have to use a dictionary.in the implementation, when we could use the anonymous object syntax, we will use it. Only when we cannot, we switch to dictionaries.
Checklist
To ensure a quick review and merge, please ensure:
Ready to Land?