Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: various problems #2122

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ This preset is a generator for the meta model `ConstrainedEnumModel` and [can be
| Method | Description | Additional arguments |
|---|---|---|
| `item` | A method to extend enum's item. | `item` object as a [`ConstrainedEnumValueModel`](./internal-model.md#the-constrained-meta-model) instance, which contains the value and key of enum's item. |
| `extension` | A method to extend the enums extension class. | |
| `extensionMethods` | A method to extend the enums extension class methods instead of the whole class. | |

### Rust
#### **Struct**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ public partial class Root
{
return JsonSerializer.Serialize(this, options);
}
public static Root Deserialize(string json)
public static Root Deserialize(string json, JsonSerializerOptions options = null)
{
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new RootConverter());
return JsonSerializer.Deserialize<Root>(json, deserializeOptions);
return JsonSerializer.Deserialize<Root>(json, options);
}
}

internal class RootConverter : JsonConverter<Root>
{
public override bool CanConvert(System.Type objectType)
{
// this converter can be applied to any type
return true;
}
public override Root Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
Expand All @@ -60,10 +53,12 @@ internal class RootConverter : JsonConverter<Root>
}

string propertyName = reader.GetString();

// Advance to the value token
reader.Read();
if (propertyName == \\"email\\")
{
var value = JsonSerializer.Deserialize<string?>(ref reader, options);
instance.Email = value;
instance.Email = JsonSerializer.Deserialize<string?>(ref reader);
continue;
}
}
Expand All @@ -72,26 +67,19 @@ internal class RootConverter : JsonConverter<Root>
}
public override void Write(Utf8JsonWriter writer, Root value, JsonSerializerOptions options)
{
if (value == null)
{
JsonSerializer.Serialize(writer, null, options);
return;
}
var properties = value.GetType().GetProperties();
var properties = value?.GetType().GetProperties();

writer.WriteStartObject();

if(value.Email != null) {
if(value?.Email != null) {
// write property name and let the serializer serialize the value itself
writer.WritePropertyName(\\"email\\");
JsonSerializer.Serialize(writer, value.Email, options);
JsonSerializer.Serialize(writer, value?.Email, options);
}


writer.WriteEndObject();
}

}
",
}",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,32 @@ Array [
Delivered,
Cancelled
}

public static class OrderStatusExtensions
{
public static int? GetValue(this OrderStatus enumValue)
public static class OrderStatusExtensions
{
switch (enumValue)
public static int? GetValue(this OrderStatus enumValue)
{
case OrderStatus.Ordered: return 30;
case OrderStatus.UnderDelivery: return 40;
case OrderStatus.Delivered: return 50;
case OrderStatus.Cancelled: return 99;
switch (enumValue)
{
case OrderStatus.Ordered: return 30;
case OrderStatus.UnderDelivery: return 40;
case OrderStatus.Delivered: return 50;
case OrderStatus.Cancelled: return 99;
}
return null;
}
return null;
}

public static OrderStatus? ToOrderStatus(dynamic? value)
{
switch (value)
public static OrderStatus? ToOrderStatus(dynamic? value)
{
case 30: return OrderStatus.Ordered;
case 40: return OrderStatus.UnderDelivery;
case 50: return OrderStatus.Delivered;
case 99: return OrderStatus.Cancelled;
switch (value)
{
case 30: return OrderStatus.Ordered;
case 40: return OrderStatus.UnderDelivery;
case 50: return OrderStatus.Delivered;
case 99: return OrderStatus.Cancelled;
}
return null;
}
return null;
}
}
",
]
`;
15 changes: 11 additions & 4 deletions src/generators/csharp/CSharpPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
PresetArgs,
PropertyArgs,
ConstrainedObjectModel,
InterfacePreset
InterfacePreset,
ConstrainedEnumModel
} from '../../models';
import { CSharpOptions } from './CSharpGenerator';
import {
Expand Down Expand Up @@ -40,12 +41,18 @@ export interface CsharpRecordPreset<O>

export type ClassPresetType<O> = CsharpClassPreset<O>;
export type RecordPresetType<O> = CsharpRecordPreset<O>;
export type EnumPresetType<O> = EnumPreset<EnumRenderer, O>;

export interface EnumPresetType<O> extends EnumPreset<EnumRenderer, O> {
extension?: (
args: PresetArgs<EnumRenderer, O, ConstrainedEnumModel>
) => Promise<string> | string;
extensionMethods?: (
args: PresetArgs<EnumRenderer, O, ConstrainedEnumModel>
) => Promise<string> | string;
}
export type CSharpPreset<O = any> = Preset<{
class: CsharpClassPreset<O>;
record: CsharpRecordPreset<O>;
enum: EnumPreset<EnumRenderer, O>;
enum: EnumPresetType<O>;
}>;

export const CSHARP_DEFAULT_PRESET: CSharpPreset<CSharpOptions> = {
Expand Down
6 changes: 6 additions & 0 deletions src/generators/csharp/constrainer/EnumConstrainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +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, '\\"')}"`;
}
switch (typeof enumValue) {
case 'boolean':
case 'bigint':
Expand Down
Loading
Loading