This commit is contained in:
Garrett Serack 2019-03-14 14:32:23 -07:00
Родитель 11d4ea868b
Коммит 253c59eea6
6 изменённых файлов: 108 добавлений и 9 удалений

Просмотреть файл

@ -7,12 +7,16 @@ import { Components, IParameter, LanguageDetails } from './components';
import { Extensions } from './extensions';
import { HttpOperation } from './http-operation';
import { ProgramaticOperationDetails, ProgrammaticOperation } from './programatic-operation';
import { Schema } from './schema';
import { Schema, VirtualProperty } from './schema';
import { DeepPartial, Dictionary } from '@microsoft.azure/codegen';
import { uid } from './uid';
export interface CommandOperationDetails extends ProgramaticOperationDetails {
virtualParameters?: {
body: Array<VirtualParameter>,
operation: Array<VirtualParameter>
}
}
export interface CommandOperation extends ProgrammaticOperation {
@ -24,6 +28,15 @@ export interface CommandOperation extends ProgrammaticOperation {
callGraph: Array<HttpOperation>;
}
export interface VirtualParameter {
name: string;
description: string;
required: boolean;
schema: Schema;
origin: VirtualProperty | IParameter;
}
export class CommandOperation extends Extensions implements CommandOperation {
public extensions = new Dictionary<any>();
public details: LanguageDetails<CommandOperationDetails>;

Просмотреть файл

@ -49,6 +49,7 @@ export interface VirtualProperty {
private?: boolean;
}
export interface SchemaDetails extends ImplementationDetails {
/** namespace of the implementation of this item */
namespace?: string;

Просмотреть файл

@ -24,4 +24,5 @@ export { command };
export { http };
export { codemodel };
export { exportedModels };
export { uid } from './code-model/uid';
export { uid } from './code-model/uid';
export { VirtualParameter } from './code-model/command-operation';

Просмотреть файл

@ -23,6 +23,9 @@ export function deepFreeze(instance: object) {
}
export function clone(instance: any, shouldFreeze = false, hash = new WeakMap(), skip: Array<string> = [], refCopyPropertyNames: Array<string> = []): any {
return _clone(instance, shouldFreeze, hash, new Set(skip), new Set(refCopyPropertyNames));
}
function _clone(instance: any, shouldFreeze = false, hash = new WeakMap(), skip: Set<string>, refCopyPropertyNames: Set<string>): any {
const freeze = shouldFreeze ? Object.freeze : (i: any) => i;
const obj = <AnyObject>instance;
@ -40,7 +43,7 @@ export function clone(instance: any, shouldFreeze = false, hash = new WeakMap(),
if (obj instanceof Set) {
let set = new Set();
for (const value of obj.values()) {
set.add(clone(value, shouldFreeze, hash, skip, refCopyPropertyNames));
set.add(_clone(value, shouldFreeze, hash, skip, refCopyPropertyNames));
}
set = freeze(set);
hash.set(obj, set);
@ -50,7 +53,7 @@ export function clone(instance: any, shouldFreeze = false, hash = new WeakMap(),
if (Array.isArray(obj)) {
let array = new Array();
for (const value of obj) {
array.push(clone(value, shouldFreeze, hash, skip, refCopyPropertyNames));
array.push(_clone(value, shouldFreeze, hash, skip, refCopyPropertyNames));
}
array = freeze(array);
hash.set(obj, array);
@ -60,7 +63,7 @@ export function clone(instance: any, shouldFreeze = false, hash = new WeakMap(),
// as do Maps
if (obj instanceof Map) {
let map = new Map<any, any>();
Array.from(obj, ([key, val]) => map.set(key, clone(val, shouldFreeze, hash, skip, refCopyPropertyNames)));
Array.from(obj, ([key, val]) => map.set(key, _clone(val, shouldFreeze, hash, skip, refCopyPropertyNames)));
map = freeze(map);
hash.set(obj, map);
return map;
@ -70,14 +73,26 @@ export function clone(instance: any, shouldFreeze = false, hash = new WeakMap(),
obj instanceof Date ? new Date(obj) :
obj instanceof RegExp ? new RegExp(obj.source, obj.flags) : {};
// store it to prevent cyclic reference failures
hash.set(obj, result);
// recurse thru children
Object.assign(result, ...Object.keys(obj).filter(key => skip.indexOf(key) === -1).map(key => refCopyPropertyNames.indexOf(key) > -1 ? ({ [key]: obj[key] }) : ({ [key]: clone(obj[key], shouldFreeze, hash, skip, refCopyPropertyNames) })));
for (const each of Object.keys(obj)) {
if (skip.has(each)) {
continue;
}
if (refCopyPropertyNames.has(each)) {
(<AnyObject>result)[each] = obj[each];
} else {
(<AnyObject>result)[each] = _clone(obj[each], shouldFreeze, hash, skip, refCopyPropertyNames);
}
}
// Object.assign(result, ...Object.keys(obj).filter(key => skip.indexOf(key) === -1).map(key => refCopyPropertyNames.indexOf(key) > -1 ? ({ [key]: obj[key] }) : ({ [key]: clone(obj[key], shouldFreeze, hash, skip, refCopyPropertyNames) })));
// freeze it if necessary
result = freeze(result);
// store it to prevent cyclic reference failures
hash.set(obj, result);
return result;
}

Просмотреть файл

@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export * from './freeze';
export * from './visitor'
export interface Index<T> {
[key: number]: T;

68
libraries/linq/visitor.ts Normal file
Просмотреть файл

@ -0,0 +1,68 @@
interface AnyObject {
[key: string]: any;
[key: number]: any;
}
export interface Leaf {
index: string | number,
parent: AnyObject,
instance: any,
}
export function visitor(instance: AnyObject): Iterable<Leaf> {
return _visitor(instance, new WeakSet<object>());
}
function* _visitor(instance: AnyObject, visited: WeakSet<object>): Iterable<Leaf> {
if (instance === null || instance === undefined || visited.has(instance)) {
return;
}
visited.add(instance);
if (instance instanceof Set || Array.isArray(instance)) {
let ndx = 0;
for (const each of instance) {
if (typeof each === 'object') {
yield* _visitor(each, visited);
}
// yield the member after visiting children
yield {
index: ndx++,
parent: instance,
instance: each
};
}
return;
}
if (instance instanceof Map) {
// walk thru map members.
for (const [key, value] of instance.entries()) {
if (typeof value === 'object') {
yield* _visitor(value, visited);
}
// yield the member after visiting children
yield {
index: key,
parent: instance,
instance: value
};
}
return;
}
// objects
for (const key of Object.keys(instance)) {
const value = instance[key];
if (typeof value === 'object') {
yield* _visitor(value, visited);
}
// yield the member after visiting children
yield {
index: key,
parent: instance,
instance: value
};
}
}