Skip to content

Commit

Permalink
Add feature to omit generated SerDe methods (#957)
Browse files Browse the repository at this point in the history
* Add feature to omit generated SerDe methods

Adding extension x-ms-go-omit-serde-methods to a type will cause the
code generator to skip emitting SerDe methods.
Fixed handling of XML text data (x-ms-text extension).

* regenerate xmlgroup

* enable test

* add clarifying comment
  • Loading branch information
jhendrixMSFT authored May 15, 2023
1 parent 42fb08a commit 1bc784b
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 101 deletions.
4 changes: 2 additions & 2 deletions .scripts/regeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ for (namespace in goMappings) {
generate(namespace, inputFile, 'test/autorest/' + namespace, extraParams.join(' '));
}

const blobStorage = './swagger/specification/storage/data-plane/Microsoft.BlobStorage/preview/2021-12-02/blob.json';
generate("azblob", blobStorage, 'test/storage/azblob', '--security=AzureKey --module="azblob" --openapi-type="data-plane" --honor-body-placement');
const blobStorage = './swagger/specification/storage/data-plane/Microsoft.BlobStorage/readme.md';
generateFromReadme("azblob", blobStorage, 'package-2021-12', 'test/storage/azblob');

const network = './swagger/specification/network/resource-manager/readme.md';
generateFromReadme("armnetwork", network, 'package-2022-09', 'test/network/armnetwork', '--module=armnetwork --azure-arm=true --remove-unreferenced-types');
Expand Down
8 changes: 5 additions & 3 deletions packages/autorest.go/src/generator/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function generateStructs(modelImports: ImportManager, serdeImports: ImportManage
continue;
}
const structDef = generateStruct(modelImports, obj.language.go!, aggregateProperties(obj));
if (obj.language.go!.marshallingFormat === 'xml') {
if (obj.language.go!.marshallingFormat === 'xml' && !obj.language.go!.omitSerDeMethods) {
serdeImports.add('encoding/xml');
if (obj.language.go!.needsDateTimeMarshalling) {
serdeImports.add('time');
Expand Down Expand Up @@ -165,8 +165,10 @@ function generateStructs(modelImports: ImportManager, serdeImports: ImportManage
if (obj.language.go!.byteArrayFormat) {
structDef.HasJSONByteArray = true;
}
generateJSONMarshaller(serdeImports, obj, structDef);
generateJSONUnmarshaller(serdeImports, structDef);
if (!obj.language.go!.omitSerDeMethods) {
generateJSONMarshaller(serdeImports, obj, structDef);
generateJSONUnmarshaller(serdeImports, structDef);
}
structTypes.push(structDef);
}
return structTypes;
Expand Down
4 changes: 4 additions & 0 deletions packages/autorest.go/src/generator/structs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export function getXMLSerialization(prop: Property, lang: Language): string {
if (prop.schema.serialization?.xml?.name) {
// xml can specifiy its own name, prefer that if available
serialization = prop.schema.serialization.xml.name;
} else if (prop.schema.serialization?.xml?.text) {
// type has the x-ms-text attribute applied so it should be character data, not a node (https://github.com/Azure/autorest/tree/main/docs/extensions#x-ms-text)
// see https://pkg.go.dev/encoding/xml#Unmarshal for what ,chardata actually means
serialization = ',chardata';
}
if (prop.schema.serialization?.xml?.attribute) {
// value comes from an xml attribute
Expand Down
4 changes: 4 additions & 0 deletions packages/autorest.go/src/transform/namer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export async function namer(session: Session<CodeModel>) {
details.name = 'AdditionalProperties1';
}
}
// adding this extension to a type will skip generatings its serde (marshalling/unmarshalling) methods
if (obj.extensions?.['x-ms-go-omit-serde-methods']) {
obj.language.go!.omitSerDeMethods = true;
}
}

// fix up operation group names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ func TestGetWrappedLists(t *testing.T) {
}

func TestGetXMsText(t *testing.T) {
t.Skip("support NYI")
client := newXMLClient(t)
result, err := client.GetXMsText(context.Background(), nil)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion packages/autorest.go/test/autorest/xmlgroup/zz_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/autorest.go/test/storage/azblob/zz_models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 0 additions & 49 deletions packages/autorest.go/test/storage/azblob/zz_models_serde.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ directive:
to: 'SkuInfo'
```
``` yaml $(go)
directive:
- from: swagger-document
where: $.definitions
transform: >
$.Addon["x-ms-go-omit-serde-methods"] = true;
```
```yaml $(go)
# stuttering clean-up causes a name collision so we fix up the name
# https://github.com/Azure/autorest/blob/main/docs/generate/built-in-directives.md#rename-model
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Code Generation - Azure Blob SDK for Golang

### Settings

```yaml
go: true
clear-output-folder: false
version: "^3.0.0"
license-header: MICROSOFT_MIT_NO_VERSION
input-file: "preview/2021-12-02/blob.json"
credential-scope: "https://storage.azure.com/.default"
openapi-type: "data-plane"
verbose: true
security: AzureKey
honor-body-placement: true
module: azblob
modelerfour:
group-parameters: false
lenient-model-deduplication: true
export-clients: true
```
``` yaml
directive:
- from: swagger-document
where: $.definitions
transform: >
$.BlobItemInternal["x-ms-go-omit-serde-methods"] = true;
$.AccessPolicy["x-ms-go-omit-serde-methods"] = true;
```
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ directive:
- suppress: LongRunningOperationsWithLongRunningExtension
- suppress: OAV107
```
---
---

0 comments on commit 1bc784b

Please sign in to comment.