Skip to content

Commit

Permalink
fix some issue for enum and header
Browse files Browse the repository at this point in the history
  • Loading branch information
msxichen committed Aug 4, 2020
1 parent 05e101b commit 18b6291
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
23 changes: 15 additions & 8 deletions powershell/internal/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { codemodel, PropertyDetails, exportedModels as T, ModelState, JsonType,
import { DeepPartial } from '@azure-tools/codegen';
import { PwshModel } from '../utils/PwshModel';
import { NewModelState } from '../utils/model-state';
import { Schema as NewSchema } from '@azure-tools/codemodel';
import { BooleanSchema, Schema as NewSchema, SchemaType } from '@azure-tools/codemodel';
import { NewBoolean } from '../llcsharp/schema/boolean';

export type Schema = T.SchemaT<LanguageDetails<SchemaDetails>, LanguageDetails<PropertyDetails>>;

Expand All @@ -40,6 +41,13 @@ export class PSSwitch extends Boolean {

}

export class NewPSSwitch extends NewBoolean {
get declaration(): string {
return `global::System.Management.Automation.SwitchParameter${this.isRequired ? '' : '?'}`;
}

}

export class PSSchemaResolver extends SchemaDefinitionResolver {
inResolve = false;
resolveTypeDeclaration(schema: Schema | undefined, required: boolean, state: ModelState<codemodel.Model>): EnhancedTypeDeclaration {
Expand All @@ -64,13 +72,12 @@ export class NewPSSchemaResolver extends NewSchemaDefinitionResolver {
resolveTypeDeclaration(schema: NewSchema | undefined, required: boolean, state: NewModelState<PwshModel>): NewEnhancedTypeDeclaration {
const before = this.inResolve;
try {
// skip-for-time-being
// if (!this.inResolve) {
// this.inResolve = true;
// if (schema && schema.type === JsonType.Boolean) {
// return new PSSwitch(schema, required);
// }
// }
if (!this.inResolve) {
this.inResolve = true;
if (schema && schema.type === SchemaType.Boolean) {
return new NewPSSwitch(schema as BooleanSchema, required);
}
}

return super.resolveTypeDeclaration(schema, required, state);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion powershell/llcsharp/model/model-class-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class NewDeserializerPartialClass extends NewSerializationPartialClass {

return function* () {
yield '// actually deserialize ';
// skip-for-time-being

for (const virtualProperty of values(<Array<NewVirtualProperty>>$this.allVirtualProperties)) {
// yield `// deserialize ${virtualProperty.name} from ${$this.serializationFormat}`;
const type = $this.resolver(<NewSchema>virtualProperty.property.schema, virtualProperty.property.language.default.required);
Expand Down
22 changes: 11 additions & 11 deletions powershell/llcsharp/model/model-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { PropertyOriginAttribute, DoNotFormatAttribute, FormatTableAttribute } f
import { Schema } from '../code-model';
import { DictionaryImplementation } from './model-class-dictionary';
import { Languages, Language, Schema as NewSchema } from '@azure-tools/codemodel';
import { VirtualProperty as NewVirtualProperty } from '../../utils/schema';
import { VirtualProperty as NewVirtualProperty, getAllVirtualProperties as newGetAllVirtualProperties } from '../../utils/schema';

export function getVirtualPropertyName(vp?: VirtualProperty): string {

Expand Down Expand Up @@ -930,23 +930,23 @@ export class NewModelClass extends Class implements NewEnhancedTypeDeclaration {
// }
// }
private addHeaderDeserializer() {
const avp = getAllVirtualProperties(this.schema.language.csharp?.virtualProperties);
const avp = newGetAllVirtualProperties(this.schema.language.csharp?.virtualProperties);
const headers = new Parameter('headers', System.Net.Http.Headers.HttpResponseHeaders);
const readHeaders = new Method(`${ClientRuntime.IHeaderSerializable}.ReadHeaders`, undefined, {
access: Access.Explicit,
parameters: [headers],
});

const used = false;
let used = false;

// skip-for-time-being
// for (const headerProperty of values(avp).where(each => each.property.details.csharp[HeaderProperty] === HeaderPropertyType.HeaderAndBody || each.property.details.csharp[HeaderProperty] === HeaderPropertyType.Header)) {
// used = true;
// const t = `((${headerProperty.originalContainingSchema.details.csharp.fullInternalInterfaceName})this)`;
// const values = `__${camelCase([...deconstruct(headerProperty.property.serializedName), 'Header'])}`;
// const td = this.state.project.modelsNamespace.resolveTypeDeclaration(<Schema>headerProperty.property.schema, false, this.state.path('schema'));
// readHeaders.add(If(`${valueOf(headers)}.TryGetValues("${headerProperty.property.serializedName}", out var ${values})`, `${t}.${headerProperty.name} = ${td.deserializeFromContainerMember(KnownMediaType.Header, headers, values, td.defaultOfType)};`));
// }
for (const headerProperty of values(avp).where(each => each.property.language.csharp?.[HeaderProperty] === HeaderPropertyType.HeaderAndBody || each.property.language.csharp?.[HeaderProperty] === HeaderPropertyType.Header)) {
used = true;
headerProperty.property.schema
const t = `((${headerProperty.originalContainingSchema.language.csharp?.fullInternalInterfaceName})this)`;
const values = `__${camelCase([...deconstruct(headerProperty.property.serializedName), 'Header'])}`;
const td = this.state.project.modelsNamespace.NewResolveTypeDeclaration(headerProperty.property.schema, false, this.state);
readHeaders.add(If(`${valueOf(headers)}.TryGetValues("${headerProperty.property.serializedName}", out var ${values})`, `${t}.${headerProperty.name} = ${td.deserializeFromContainerMember(KnownMediaType.Header, headers, values, td.defaultOfType)};`));
}
if (used) {
this.interfaces.push(ClientRuntime.IHeaderSerializable);
this.add(readHeaders);
Expand Down
5 changes: 5 additions & 0 deletions powershell/llcsharp/model/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export class NewModelsNamespace extends Namespace {
this.NewResolveTypeDeclaration(schema, true, <NewState>state);
}
}
if (schemas.sealedChoices) {
for (const schema of schemas.sealedChoices) {
this.NewResolveTypeDeclaration(schema, true, <NewState>state);
}
}
//todo, need to add support for other types

}
Expand Down
2 changes: 1 addition & 1 deletion powershell/llcsharp/schema/schema-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export class NewSchemaDefinitionResolver {
}
return this.add(schema, new NewObjectImplementation(<ObjectSchema>schema));
}
case SchemaType.SealedChoice:
case SchemaType.String: {
return new NewString(<StringSchema>schema, required);

Expand Down Expand Up @@ -223,7 +224,6 @@ export class NewSchemaDefinitionResolver {
throw new Error('Unknown Model. Fatal.');
}
case SchemaType.Choice:
case SchemaType.SealedChoice:
return new NewEnumImplementation(schema, required);
case undefined:
if (schema.extensions && schema.extensions['x-ms-enum']) {
Expand Down
6 changes: 3 additions & 3 deletions powershell/llcsharp/schema/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { OneOrMoreStatements } from '@azure-tools/codegen-csharp';
import { Variable } from '@azure-tools/codegen-csharp';
import { ClientRuntime } from '../clientruntime';
import { Schema } from '../code-model';
import { ChoiceSchema, Schema as NewSchema, StringSchema } from '@azure-tools/codemodel';
import { ChoiceSchema, Schema as NewSchema, SchemaType, SealedChoiceSchema, StringSchema } from '@azure-tools/codemodel';
import { popTempVar, pushTempVar } from './primitive';
import { EnhancedTypeDeclaration, NewEnhancedTypeDeclaration } from './extended-type-declaration';
import { length } from '@azure-tools/linq';
Expand Down Expand Up @@ -398,10 +398,10 @@ ${this.validateEnum(eventListener, property)}
return `await ${eventListener}.AssertRegEx(${nameof(property.value)},${property},@"${pattern}");`;
}
private validateEnum(eventListener: Variable, property: Variable): string {
if (!(this.schema instanceof ChoiceSchema)) {
if (this.schema.type !== SchemaType.Choice && this.schema.type !== SchemaType.SealedChoice) {
return '';
}
const choiceValues = this.schema.choices.map((c) => c.value);
const choiceValues = (<ChoiceSchema | SealedChoiceSchema>this.schema).choices.map((c) => c.value);
return `await ${eventListener}.AssertEnum(${nameof(property.value)},${property},${choiceValues.joinWith((v) => `@"${v}"`)});`;
}
}

0 comments on commit 18b6291

Please sign in to comment.