Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 30, 2024
1 parent 1519785 commit ba760e5
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/generators/csharp/CSharpPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface EnumPresetType<O> extends EnumPreset<EnumRenderer, O> {
extensionMethods?: (
args: PresetArgs<EnumRenderer, O, ConstrainedEnumModel>
) => Promise<string> | string;
};
}
export type CSharpPreset<O = any> = Preset<{
class: CsharpClassPreset<O>;
record: CsharpRecordPreset<O>;
Expand Down
11 changes: 6 additions & 5 deletions src/generators/csharp/constrainer/EnumConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ export function defaultEnumKeyConstraints(
export function defaultEnumValueConstraints(): CSharpEnumValueConstraint {
return ({ enumValue }) => {
let normalizedEnumValue;
if(enumValue === null) return enumValue;
if(Array.isArray(enumValue)) return `"${JSON.stringify(enumValue).replace(
/"/g,
'\\"'
)}"`;
if (enumValue === null) {
return enumValue;
}
if (Array.isArray(enumValue)) {
return `"${JSON.stringify(enumValue).replace(/"/g, '\\"')}"`;
}
switch (typeof enumValue) {
case 'boolean':
case 'bigint':
Expand Down
30 changes: 16 additions & 14 deletions src/generators/csharp/presets/JsonSerializerPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function renderDeserializeProperty(model: ConstrainedObjectPropertyModel) {
) {
return `JsonSerializer.Deserialize<${model.property.name}>(ref reader)`;
}

return `JsonSerializer.Deserialize<${model.property.type}>(ref reader)`;
}

Expand Down Expand Up @@ -250,67 +250,69 @@ ${renderer.indent(serialize)}
}
},
enum: {
self({content, renderer}) {
renderer.dependencyManager.addDependency('using System.Text.Json;')
self({ content, renderer }) {
renderer.dependencyManager.addDependency('using System.Text.Json;');
return content;
},
extensionMethods({content, model, renderer}) {
extensionMethods({ content, model, renderer }) {
const enums = model.values || [];
const items: string[] = [];
const items2: string[] = [];

for (const enumValue of enums) {
let jsonValue = enumValue.value;
const jsonValue = enumValue.value;
const originalEnumValue = enumValue.originalInput;
let stringValue = jsonValue;
if(typeof jsonValue !== 'string') stringValue = `"${jsonValue}"`;
if (typeof jsonValue !== 'string') {
stringValue = `"${jsonValue}"`;
}
items.push(
`case ${model.name}.${enumValue.key}: return ${stringValue};`
);

if(typeof originalEnumValue === 'string'){
if (typeof originalEnumValue === 'string') {
items2.push(
`if (value?.ValueKind == JsonValueKind.String && value?.GetString() == ${enumValue.value})
{
return ${model.name}.${enumValue.key};
}`
);
} else if(originalEnumValue === null){
} else if (originalEnumValue === null) {
items2.push(
`if (value == null || value?.ValueKind == JsonValueKind.Null && value?.GetRawText() == "null")
{
return ${model.name}.${enumValue.key};
}`
);
} else if(typeof originalEnumValue === 'boolean'){
} else if (typeof originalEnumValue === 'boolean') {
items2.push(
`if (value?.ValueKind == JsonValueKind.True || value?.ValueKind == JsonValueKind.False && value?.GetBoolean() == ${enumValue.value})
{
return ${model.name}.${enumValue.key};
}`
);
} else if(Array.isArray(originalEnumValue)){
} else if (Array.isArray(originalEnumValue)) {
items2.push(
`if (value?.ValueKind == JsonValueKind.Array && value?.GetRawText() == ${enumValue.value})
{
return ${model.name}.${enumValue.key};
}`
);
} else if(typeof originalEnumValue === 'object'){
} else if (typeof originalEnumValue === 'object') {
items2.push(
`if (value?.ValueKind == JsonValueKind.Object && value?.GetRawText() == ${enumValue.value})
{
return ${model.name}.${enumValue.key};
}`
);
} else if(typeof originalEnumValue === 'number'){
} else if (typeof originalEnumValue === 'number') {
items2.push(
`if (value?.ValueKind == JsonValueKind.Number && value?.GetInt32() == ${enumValue.value})
{
return ${model.name}.${enumValue.key};
}`
);
} else if(typeof originalEnumValue === 'bigint'){
} else if (typeof originalEnumValue === 'bigint') {
items2.push(
`if (value?.ValueKind == JsonValueKind.Number && value?.GetInt64() == ${enumValue.value})
{
Expand Down Expand Up @@ -338,6 +340,6 @@ public static ${model.type}? FromJsonTo${model.name}(JsonElement? value)
${renderer.indent(newstuff2, 2)}
return null;
}`;
},
}
}
};
7 changes: 2 additions & 5 deletions src/generators/csharp/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const CSHARP_DEFAULT_ENUM_PRESET: EnumPresetType<CSharpOptions> = {
return `public static class ${model.name}Extensions
{
${renderer.indent(extensionMethods)}
}`
}`;
},
extensionMethods({ model, renderer }) {
const getValueCaseItemValues = renderer.getValueCaseItemValues();
Expand All @@ -101,9 +101,7 @@ return null;`;
${renderer.indent(toEnumCaseItemValues)}
}
return null;`;
const classContent = `public static ${model.type}? GetValue(this ${
model.name
} enumValue)
return `public static ${model.type}? GetValue(this ${model.name} enumValue)
{
${renderer.indent(enumValueSwitch)}
}
Expand All @@ -112,6 +110,5 @@ public static ${model.name}? To${model.name}(dynamic? value)
{
${renderer.indent(valueSwitch)}
}`;
return classContent;
}
};
2 changes: 1 addition & 1 deletion src/generators/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export * from './TypeScriptFileGenerator';
export { TS_DEFAULT_PRESET } from './TypeScriptPreset';
export type { TypeScriptPreset } from './TypeScriptPreset';
export * from './presets';
export {RESERVED_TYPESCRIPT_KEYWORDS} from './Constants';
export { RESERVED_TYPESCRIPT_KEYWORDS } from './Constants';

export {
defaultEnumKeyConstraints as typeScriptDefaultEnumKeyConstraints,
Expand Down
4 changes: 1 addition & 3 deletions src/generators/typescript/renderers/TypeRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { TypeScriptOptions } from '../TypeScriptGenerator';
*/
export class TypeRenderer extends TypeScriptRenderer<ConstrainedMetaModel> {
async defaultSelf(): Promise<string> {
const content = [
await this.runAdditionalContentPreset()
];
const content = [await this.runAdditionalContentPreset()];

return `type ${this.model.name} = ${this.model.type};
Expand Down
2 changes: 1 addition & 1 deletion test/generators/csharp/CSharpGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('CSharpGenerator', () => {
test('should render `enum` type', async () => {
const doc = {
$id: 'Things',
enum: ['Texas', '1', 1, false, { test: 'test' }, ["test", 1], null]
enum: ['Texas', '1', 1, false, { test: 'test' }, ['test', 1], null]
};
const models = await generator.generate(doc);
expect(models).toHaveLength(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const doc = {
numberProp: { type: 'number' },
enumProp: {
$id: 'EnumTest',
enum: ['Some enum String', true, { test: 'test' }, 2, ["test", 1], null]
enum: ['Some enum String', true, { test: 'test' }, 2, ['test', 1], null]
},
objectProp: {
type: 'object',
Expand Down

0 comments on commit ba760e5

Please sign in to comment.