Skip to content

Commit

Permalink
Mapper interfaces (#180)
Browse files Browse the repository at this point in the history
* Update mapper type definitions

* Fix tests

* Fix CompositeType className and modelProperties

* Bump to v0.17.0
  • Loading branch information
RikkiGibson authored Jul 31, 2018
1 parent 46f5065 commit 5bd7b67
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 76 deletions.
7 changes: 4 additions & 3 deletions lib/msRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export { signingPolicy } from "./policies/signingPolicy";
export { msRestUserAgentPolicy } from "./policies/msRestUserAgentPolicy";
export { deserializationPolicy } from "./policies/deserializationPolicy";
export {
BaseMapperType, CompositeMapper, DictionaryMapper, EnumMapper, Mapper,
MapperConstraints, MapperType, PolymorphicDiscriminator,
SequenceMapper, Serializer, UrlParameterValue, serializeObject
MapperType, SimpleMapperType, CompositeMapperType, DictionaryMapperType, SequenceMapperType, EnumMapperType,
Mapper, BaseMapper, CompositeMapper, SequenceMapper, DictionaryMapper, EnumMapper,
MapperConstraints, PolymorphicDiscriminator,
Serializer, UrlParameterValue, serializeObject
} from "./serializer";
export {
stripRequest, stripResponse, delay,
Expand Down
2 changes: 1 addition & 1 deletion lib/operationParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function getPathStringFromParameterPath(parameterPath: ParameterPath, map
} else if (Array.isArray(parameterPath)) {
result = parameterPath.join(".");
} else {
result = mapper.serializedName;
result = mapper.serializedName!;
}
return result;
}
97 changes: 59 additions & 38 deletions lib/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as base64 from "./util/base64";
import * as utils from "./util/utils";

export class Serializer {
constructor(public readonly modelMappers?: { [key: string]: any }, public readonly isXML?: boolean) { }
constructor(public readonly modelMappers: { [key: string]: any } = {}, public readonly isXML?: boolean) { }

validateConstraints(mapper: Mapper, value: any, objectName: string): void {
const failValidation = (constraintName: keyof MapperConstraints, constraintValue: any) => {
Expand Down Expand Up @@ -76,7 +76,7 @@ export class Serializer {
let payload: any = {};
const mapperType = mapper.type.name as string;
if (!objectName) {
objectName = mapper.serializedName;
objectName = mapper.serializedName!;
}
if (mapperType.match(/^Sequence$/ig) !== null) {
payload = [];
Expand Down Expand Up @@ -162,7 +162,7 @@ export class Serializer {
let payload: any;
const mapperType = mapper.type.name;
if (!objectName) {
objectName = mapper.serializedName;
objectName = mapper.serializedName!;
}

if (mapperType.match(/^Number$/ig) !== null) {
Expand Down Expand Up @@ -460,7 +460,7 @@ function serializeCompositeType(serializer: Serializer, mapper: CompositeMapper,
propName = propertyMapper.xmlElementName || propertyMapper.xmlName;
}
} else {
const paths = splitSerializeName(propertyMapper.serializedName);
const paths = splitSerializeName(propertyMapper.serializedName!);
propName = paths.pop();

for (const pathName of paths) {
Expand Down Expand Up @@ -509,14 +509,6 @@ function deserializeCompositeType(serializer: Serializer, mapper: CompositeMappe
mapper = getPolymorphicMapper(serializer, mapper, responseBody, objectName, "deserialize");
}

let instance: { [key: string]: any } = {};
let modelMapper: Mapper = {
required: false,
serializedName: "serializedName",
type: {
name: "Composite"
}
};
responseBody = responseBody || {};
let modelProps = mapper.type.modelProperties;
if (!modelProps) {
Expand All @@ -525,17 +517,18 @@ function deserializeCompositeType(serializer: Serializer, mapper: CompositeMappe
}
// get the mapper if modelProperties of the CompositeType is not present and
// then get the modelProperties from it.
modelMapper = (serializer.modelMappers as { [key: string]: any })[mapper.type.className];
const modelMapper: CompositeMapper = serializer.modelMappers[mapper.type.className];
if (!modelMapper) {
throw new Error(`mapper() cannot be null or undefined for model "${mapper.type.className}"`);
}
modelProps = (modelMapper as CompositeMapper).type.modelProperties;
modelProps = modelMapper.type.modelProperties;
if (!modelProps) {
throw new Error(`modelProperties cannot be null or undefined in the ` +
`mapper "${JSON.stringify(modelMapper)}" of type "${mapper.type.className}" for responseBody "${objectName}".`);
}
}

let instance: { [key: string]: any } = {};
for (const key of Object.keys(modelProps)) {
const propertyMapper = modelProps[key];
const { serializedName, xmlName, xmlElementName } = propertyMapper;
Expand Down Expand Up @@ -570,7 +563,7 @@ function deserializeCompositeType(serializer: Serializer, mapper: CompositeMappe
instance[key] = serializer.deserialize(propertyMapper, unwrappedProperty, propertyObjectName);
}
} else {
const paths = splitSerializeName(modelProps[key].serializedName);
const paths = splitSerializeName(modelProps[key].serializedName!);
// deserialize the property if it is present in the provided responseBody instance
let propertyInstance;
let res = responseBody;
Expand Down Expand Up @@ -738,8 +731,51 @@ export interface MapperConstraints {
MultipleOf?: number;
}

export interface BaseMapperType {
name: string;
export type MapperType = SimpleMapperType | CompositeMapperType | SequenceMapperType | DictionaryMapperType | EnumMapperType;

export interface SimpleMapperType {
name: "Base64Url"
| "Boolean"
| "ByteArray"
| "Date"
| "DateTime"
| "DateTimeRfc1123"
| "Object"
| "Stream"
| "String"
| "TimeSpan"
| "UnixTime"
| "Uuid"
| "Number"
| "any";
}

export interface CompositeMapperType {
name: "Composite";

// Only one of the two below properties should be present.
// Use className to reference another type definition,
// and use modelProperties when the reference to the other type has been resolved.
className?: string;
modelProperties?: { [propertyName: string]: Mapper };

uberParent?: string;
polymorphicDiscriminator?: string | PolymorphicDiscriminator;
}

export interface SequenceMapperType {
name: "Sequence";
element: Mapper;
}

export interface DictionaryMapperType {
name: "Dictionary";
value: Mapper;
}

export interface EnumMapperType {
name: "Enum";
allowedValues: any[];
}

export interface BaseMapper {
Expand All @@ -751,8 +787,8 @@ export interface BaseMapper {
isConstant?: boolean;
required?: boolean;
nullable?: boolean;
serializedName: string;
type: BaseMapperType;
serializedName?: string;
type: MapperType;
defaultValue?: any;
constraints?: MapperConstraints;
}
Expand All @@ -766,35 +802,20 @@ export interface PolymorphicDiscriminator {
}

export interface CompositeMapper extends BaseMapper {
type: {
name: "Composite";
className: string;
modelProperties: { [propertyName: string]: Mapper };
uberParent?: string;
polymorphicDiscriminator?: string | PolymorphicDiscriminator;
};
type: CompositeMapperType;
}

export interface SequenceMapper extends BaseMapper {
type: {
name: "Sequence";
element: Mapper;
};
type: SequenceMapperType;
}

export interface DictionaryMapper extends BaseMapper {
type: {
name: "Dictionary";
value: Mapper;
};
type: DictionaryMapperType;
headerCollectionPrefix?: string;
}

export interface EnumMapper extends BaseMapper {
type: {
name: "Enum";
allowedValues: Array<any>;
};
type: EnumMapperType;
}

export interface UrlParameterValue {
Expand Down
4 changes: 2 additions & 2 deletions lib/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export function serializeRequestBody(serviceClient: ServiceClient, httpRequest:
const isStream = typeName === MapperType.Stream;
if (operationSpec.isXML) {
if (typeName === MapperType.Sequence) {
httpRequest.body = utils.stringifyXML(utils.prepareXMLRootList(httpRequest.body, xmlElementName || xmlName || serializedName), { rootName: xmlName || serializedName });
httpRequest.body = utils.stringifyXML(utils.prepareXMLRootList(httpRequest.body, xmlElementName || xmlName || serializedName!), { rootName: xmlName || serializedName });
}
else if (!isStream) {
httpRequest.body = utils.stringifyXML(httpRequest.body, { rootName: xmlName || serializedName });
Expand Down Expand Up @@ -430,7 +430,7 @@ export function getOperationArgumentValueFromParameterPath(serviceClient: Servic
}
} else {
for (const propertyName in parameterPath) {
const propertyMapper: Mapper = (parameterMapper as CompositeMapper).type.modelProperties[propertyName];
const propertyMapper: Mapper = (parameterMapper as CompositeMapper).type.modelProperties![propertyName];
const propertyPath: ParameterPath = parameterPath[propertyName];
const propertyValue: any = getOperationArgumentValueFromParameterPath(serviceClient, operationArguments, propertyPath, propertyMapper, serializer);
// Serialize just for validation purposes.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"email": "azsdkteam@microsoft.com",
"url": "https://github.com/Azure/ms-rest-js"
},
"version": "0.16.0",
"version": "0.17.0",
"description": "Isomorphic client Runtime for Typescript/node.js/browser javascript client libraries generated using AutoRest",
"tags": [
"isomorphic",
Expand Down
8 changes: 4 additions & 4 deletions test/shared/operationParameterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("getParameterPathString()", () => {
mapper: {
serializedName: "value",
type: {
name: "number"
name: "Number"
}
}
};
Expand All @@ -33,7 +33,7 @@ describe("getParameterPathString()", () => {
mapper: {
serializedName: "value",
type: {
name: "number"
name: "Number"
}
}
};
Expand All @@ -46,7 +46,7 @@ describe("getParameterPathString()", () => {
mapper: {
serializedName: "value",
type: {
name: "number"
name: "Number"
}
}
};
Expand All @@ -62,7 +62,7 @@ describe("getParameterPathString()", () => {
mapper: {
serializedName: "value",
type: {
name: "number"
name: "Number"
}
}
};
Expand Down
Loading

0 comments on commit 5bd7b67

Please sign in to comment.