Skip to content

Commit

Permalink
Handle composite and sequence mapper separately (#780)
Browse files Browse the repository at this point in the history
* Handle composite and sequence mapper separately

* Update smoke tests
  • Loading branch information
joheredi authored Oct 27, 2020
1 parent 7b8262b commit 3f2d8db
Show file tree
Hide file tree
Showing 49 changed files with 6,119 additions and 1,091 deletions.
79 changes: 67 additions & 12 deletions src/generators/mappersGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
Mapper,
CompositeMapper,
CompositeMapperType,
SequenceMapperType,
PolymorphicDiscriminator,
MapperConstraints
} from "@azure/core-http";
import { ModelProperties } from "../transforms/mapperTransforms";
import { keys, isEmpty, isString, isNil, isEqual } from "lodash";
import { getStringForValue } from "../utils/valueHelpers";
import { getStringForValue, MapperTypes } from "../utils/valueHelpers";
import { PolymorphicObjectDetails, ObjectKind } from "../models/modelDetails";
import { logger } from "../utils/logger";

Expand Down Expand Up @@ -152,30 +153,84 @@ export function writeMapper(writer: CodeBlockWriter, mapper: Mapper) {
// so we extract them from the type object. The remaining of the type
// object we'll just write all its properties as they are using writeObjectProps
const { type, defaultValue, constraints, ...restMapper } = mapper;
const {
modelProperties,
polymorphicDiscriminator,
...restType
} = type as CompositeMapperType;

writer.block(() => {
// we need to handle default value differently, since some types need to be
// converted, such as ByteAttay, hence extracting it from the props
writeDefaultValue(writer, defaultValue, restType);
writeDefaultValue(writer, defaultValue, type);
// Write mapper constraints
writeMapperContraints(writer, constraints);
// Writing the rest of the props
writeObjectProps(restMapper, writer)
.write("type:")
.block(() => {
// Write all type properties that don't need special handling
writeObjectProps(restType, writer);
// Write type properties that need special handling
writePolymorphicDiscriminator(writer, polymorphicDiscriminator);
writeModelProperties(writer, parents, modelProperties);
// Write tipe properties for the current mapper
writeMapperType(writer, mapper.type, parents);
});
});
}

/**
* Figures out if the mapper type to generate is a Sequence or Composite
* mapper type and generates it.
*/
function writeMapperType(
writer: CodeBlockWriter,
mapperType: MapperType,
parents: string[]
) {
if (isSequenceMapperType(mapperType)) {
return writeSequenceMapperType(writer, mapperType);
}

return writeCompositeMapperType(writer, mapperType, parents);
}

/**
* Check if a MapperType is a SequenceMapperType
*/
function isSequenceMapperType(
mapperType: MapperType
): mapperType is SequenceMapperType {
return (mapperType as SequenceMapperType).element !== undefined;
}

/**
* Generates the type content for a CompositeMapperType
*/
function writeCompositeMapperType(
writer: CodeBlockWriter,
mapperType: MapperType,
parents: string[]
) {
const {
modelProperties,
polymorphicDiscriminator,
...restType
} = mapperType as CompositeMapperType;
writeObjectProps(restType, writer);
// Write type properties that need special handling
writePolymorphicDiscriminator(writer, polymorphicDiscriminator);
writeModelProperties(writer, parents, modelProperties);
}

/**
* Generates the type content for a SequenceMapperType
*/
function writeSequenceMapperType(
writer: CodeBlockWriter,
mapperType: MapperType
) {
const sequenceMapperType = mapperType as SequenceMapperType;
const { element } = sequenceMapperType;
if (!element) {
return writer;
}
writer.write(`name: "${MapperType.Sequence}",`);
writer.write("element:");
return writeMapper(writer, element);
}

function writeMapperContraints(
writer: CodeBlockWriter,
constraints?: MapperConstraints
Expand Down
21 changes: 21 additions & 0 deletions test/integration/generated/arrayConstraints/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions test/integration/generated/arrayConstraints/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Azure ArrayConstraintsClient SDK for JavaScript

This package contains an isomorphic SDK for ArrayConstraintsClient.

### Currently supported environments

- Node.js version 8.x.x or higher
- Browser JavaScript

### How to Install

```bash
npm install array-constraints-client
```

### How to use

#### Sample code

Refer the sample code in the [azure-sdk-for-js-samples](https://github.com/Azure/azure-sdk-for-js-samples) repository.

## Related projects

- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)


![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcdn%2Farm-cdn%2FREADME.png)
18 changes: 18 additions & 0 deletions test/integration/generated/arrayConstraints/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "./esm/index.d.ts",
"docModel": { "enabled": true },
"apiReport": { "enabled": true, "reportFolder": "./review" },
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./esm/index.d.ts"
},
"messages": {
"tsdocMessageReporting": { "default": { "logLevel": "none" } },
"extractorMessageReporting": {
"ae-missing-release-tag": { "logLevel": "none" },
"ae-unresolved-link": { "logLevel": "none" }
}
}
}
48 changes: 48 additions & 0 deletions test/integration/generated/arrayConstraints/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "array-constraints-client",
"author": "Microsoft Corporation",
"description": "A generated SDK for ArrayConstraintsClient.",
"version": "1.0.0-preview1",
"dependencies": { "@azure/core-http": "^1.1.4", "tslib": "^1.9.3" },
"keywords": ["node", "azure", "typescript", "browser", "isomorphic"],
"license": "MIT",
"main": "./dist/array-constraints-client.js",
"module": "./esm/index.js",
"types": "./esm/index.d.ts",
"devDependencies": {
"typescript": "^3.1.1",
"rollup": "^0.66.2",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"uglify-js": "^3.4.9",
"@microsoft/api-extractor": "7.9.10"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js",
"repository": {
"type": "git",
"url": "https://github.com/Azure/azure-sdk-for-js.git"
},
"bugs": { "url": "https://github.com/Azure/azure-sdk-for-js/issues" },
"files": [
"dist/**/*.js",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/*.d.ts.map",
"esm/**/*.js",
"esm/**/*.js.map",
"esm/**/*.d.ts",
"esm/**/*.d.ts.map",
"src/**/*.ts",
"README.md",
"rollup.config.js",
"tsconfig.json"
],
"scripts": {
"build": "tsc && rollup -c rollup.config.js && npm run minify && npm run extract-api",
"minify": "uglifyjs -c -m --comments --source-map \"content='./dist/array-constraints-client.js.map'\" -o ./dist/array-constraints-client.min.js ./dist/array-constraints-client.js",
"prepack": "npm install && npm run build",
"extract-api": "api-extractor run --local"
},
"sideEffects": false,
"autoPublish": true
}
39 changes: 39 additions & 0 deletions test/integration/generated/arrayConstraints/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import rollup from "rollup";
import nodeResolve from "rollup-plugin-node-resolve";
import sourcemaps from "rollup-plugin-sourcemaps";

/**
* @type {rollup.RollupFileOptions}
*/
const config = {
input: "./esm/arrayConstraintsClient.js",
external: ["@azure/core-http", "@azure/core-arm"],
output: {
file: "./dist/array-constraints-client.js",
format: "umd",
name: "ArrayConstraintsClient",
sourcemap: true,
globals: {
"@azure/core-http": "coreHttp",
"@azure/core-arm": "coreArm"
},
banner: `/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/ `
},
plugins: [nodeResolve({ module: true }), sourcemaps()]
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

import * as coreHttp from "@azure/core-http";
import * as Parameters from "./models/parameters";
import * as Mappers from "./models/mappers";
import { ArrayConstraintsClientContext } from "./arrayConstraintsClientContext";
import {
ArrayConstraintsClientOptionalParams,
Enum0,
Product,
ArrayConstraintsClientPostValueOptionalParams,
ArrayConstraintsClientPostValueResponse,
ArrayConstraintsClientApiV1ValueGetOptionalParams,
ArrayConstraintsClientApiV1ValueGetResponse
} from "./models";

export class ArrayConstraintsClient extends ArrayConstraintsClientContext {
/**
* Initializes a new instance of the ArrayConstraintsClient class.
* @param $host server parameter
* @param apiVersion
* @param options The parameter options
*/
constructor(
$host: string,
apiVersion: Enum0,
options?: ArrayConstraintsClientOptionalParams
) {
super($host, apiVersion, options);
}

/**
* @param arrayBody array of complex type with [{'integer': 1 'string': '2'}, {'integer': 3, 'string':
* '4'}, {'integer': 5, 'string': '6'}]
* @param options The options parameters.
*/
postValue(
arrayBody: Product[],
options?: ArrayConstraintsClientPostValueOptionalParams
): Promise<ArrayConstraintsClientPostValueResponse> {
const operationOptions: coreHttp.RequestOptionsBase = coreHttp.operationOptionsToRequestOptionsBase(
options || {}
);
return this.sendOperationRequest(
{ arrayBody, options: operationOptions },
postValueOperationSpec
) as Promise<ArrayConstraintsClientPostValueResponse>;
}

/**
* @param options The options parameters.
*/
apiV1ValueGet(
options?: ArrayConstraintsClientApiV1ValueGetOptionalParams
): Promise<ArrayConstraintsClientApiV1ValueGetResponse> {
const operationOptions: coreHttp.RequestOptionsBase = coreHttp.operationOptionsToRequestOptionsBase(
options || {}
);
return this.sendOperationRequest(
{ options: operationOptions },
apiV1ValueGetOperationSpec
) as Promise<ArrayConstraintsClientApiV1ValueGetResponse>;
}
}
// Operation Specifications

const serializer = new coreHttp.Serializer(Mappers, /* isXml */ false);

const postValueOperationSpec: coreHttp.OperationSpec = {
path: "/api/v1/value",
httpMethod: "POST",
responses: {
200: {
bodyMapper: {
type: { name: "Sequence", element: { type: { name: "String" } } }
}
}
},
requestBody: Parameters.arrayBody,
queryParameters: [Parameters.pageRange],
urlParameters: [Parameters.$host],
headerParameters: [
Parameters.contentType,
Parameters.accept,
Parameters.apiVersion
],
mediaType: "json",
serializer
};
const apiV1ValueGetOperationSpec: coreHttp.OperationSpec = {
path: "/api/v1/value",
httpMethod: "GET",
responses: {
200: {
bodyMapper: {
type: { name: "Sequence", element: { type: { name: "String" } } }
}
}
},
queryParameters: [Parameters.pageRange],
urlParameters: [Parameters.$host],
headerParameters: [Parameters.apiVersion, Parameters.accept1],
serializer
};
Loading

0 comments on commit 3f2d8db

Please sign in to comment.