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

Dotnet change uint to long #295

Merged
merged 2 commits into from
Nov 16, 2023
Merged
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
84 changes: 42 additions & 42 deletions generator/plugins/dotnet/custom/OrTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class OrTypeConverter<T, U> : JsonConverter<OrType<T, U>>

Type[] types = new Type[] { typeof(T), typeof(U) };

if (reader.TokenType == JsonToken.Integer && (Validators.HasType(types, typeof(uint)) || Validators.HasType(types, typeof(int))))
if (reader.TokenType == JsonToken.Integer && (Validators.HasType(types, typeof(long)) || Validators.HasType(types, typeof(int))))
{
return ReadIntegerToken(reader, serializer, types);
}
Expand All @@ -61,15 +61,15 @@ public class OrTypeConverter<T, U> : JsonConverter<OrType<T, U>>
private static OrType<T, U> ReadIntegerToken(JsonReader reader, JsonSerializer serializer, Type[] types)
{
long integer = serializer.Deserialize<long>(reader);
if (Validators.InUIntegerRange(integer) && Validators.HasType(types, typeof(uint)))
if (Validators.InUIntegerRange(integer) && Validators.HasType(types, typeof(long)))
{
if (typeof(T) == typeof(uint))
if (typeof(T) == typeof(long))
{
return new OrType<T, U>((T)(object)(uint)integer);
return new OrType<T, U>((T)(object)(long)integer);
}
if (typeof(U) == typeof(uint))
if (typeof(U) == typeof(long))
{
return new OrType<T, U>((U)(object)(uint)integer);
return new OrType<T, U>((U)(object)(long)integer);
}
}
if (Validators.InIntegerRange(integer) && Validators.HasType(types, typeof(int)))
Expand All @@ -83,7 +83,7 @@ private static OrType<T, U> ReadIntegerToken(JsonReader reader, JsonSerializer s
return new OrType<T, U>((U)(object)(int)integer);
}
}
throw new ArgumentOutOfRangeException($"Integer out-of-range of LSP Signed Integer[{int.MinValue}:{int.MaxValue}] and out-of-range of LSP Unsigned Integer [{uint.MinValue}:{uint.MaxValue}] => {integer}");
throw new ArgumentOutOfRangeException($"Integer out-of-range of LSP Signed Integer[{int.MinValue}:{int.MaxValue}] and out-of-range of LSP Unsigned Integer [{long.MinValue}:{long.MaxValue}] => {integer}");
}

private static OrType<T, U> ReadFloatToken(JsonReader reader, JsonSerializer serializer, Type[] types)
Expand Down Expand Up @@ -136,9 +136,9 @@ private static OrType<T, U> ReadObjectToken(JToken token, JsonSerializer seriali
try
{
object? value = null;
if (token.Type == JTokenType.Array && type == typeof((uint, uint)))
if (token.Type == JTokenType.Array && type == typeof((long, long)))
{
uint[]? o = token.ToObject<uint[]>(serializer);
long[]? o = token.ToObject<long[]>(serializer);
if (o != null)
{
value = (o[0], o[1]);
Expand Down Expand Up @@ -179,10 +179,10 @@ public override void WriteJson(JsonWriter writer, OrType<T, U>? value, JsonSeria
{
writer.WriteNull();
}
else if (value?.Value?.GetType() == typeof((uint, uint)))
else if (value?.Value?.GetType() == typeof((long, long)))
{
ValueTuple<uint, uint> o = (ValueTuple<uint, uint>)(value.Value);
serializer.Serialize(writer, new uint[] { o.Item1, o.Item2 });
ValueTuple<long, long> o = (ValueTuple<long, long>)(value.Value);
serializer.Serialize(writer, new long[] { o.Item1, o.Item2 });
}
else
{
Expand All @@ -204,7 +204,7 @@ public class OrTypeConverter<T, U, V> : JsonConverter<OrType<T, U, V>>

Type[] types = new Type[] { typeof(T), typeof(U), typeof(V) };

if (reader.TokenType == JsonToken.Integer && (Validators.HasType(types, typeof(uint)) || Validators.HasType(types, typeof(int))))
if (reader.TokenType == JsonToken.Integer && (Validators.HasType(types, typeof(long)) || Validators.HasType(types, typeof(int))))
{
return ReadIntegerToken(reader, serializer, types);
}
Expand All @@ -228,19 +228,19 @@ public class OrTypeConverter<T, U, V> : JsonConverter<OrType<T, U, V>>
private static OrType<T, U, V> ReadIntegerToken(JsonReader reader, JsonSerializer serializer, Type[] types)
{
long integer = serializer.Deserialize<long>(reader);
if (Validators.InUIntegerRange(integer) && Validators.HasType(types, typeof(uint)))
if (Validators.InUIntegerRange(integer) && Validators.HasType(types, typeof(long)))
{
if (typeof(T) == typeof(uint))
if (typeof(T) == typeof(long))
{
return new OrType<T, U, V>((T)(object)(uint)integer);
return new OrType<T, U, V>((T)(object)(long)integer);
}
if (typeof(U) == typeof(uint))
if (typeof(U) == typeof(long))
{
return new OrType<T, U, V>((U)(object)(uint)integer);
return new OrType<T, U, V>((U)(object)(long)integer);
}
if (typeof(V) == typeof(uint))
if (typeof(V) == typeof(long))
{
return new OrType<T, U, V>((V)(object)(uint)integer);
return new OrType<T, U, V>((V)(object)(long)integer);
}
}
if (Validators.InIntegerRange(integer) && Validators.HasType(types, typeof(int)))
Expand All @@ -258,7 +258,7 @@ private static OrType<T, U, V> ReadIntegerToken(JsonReader reader, JsonSerialize
return new OrType<T, U, V>((V)(object)(int)integer);
}
}
throw new ArgumentOutOfRangeException($"Integer out-of-range of LSP Signed Integer[{int.MinValue}:{int.MaxValue}] and out-of-range of LSP Unsigned Integer [{uint.MinValue}:{uint.MaxValue}] => {integer}");
throw new ArgumentOutOfRangeException($"Integer out-of-range of LSP Signed Integer[{int.MinValue}:{int.MaxValue}] and out-of-range of LSP Unsigned Integer [{long.MinValue}:{long.MaxValue}] => {integer}");
}

private static OrType<T, U, V> ReadFloatToken(JsonReader reader, JsonSerializer serializer, Type[] types)
Expand Down Expand Up @@ -323,9 +323,9 @@ private static OrType<T, U, V> ReadObjectToken(JToken token, JsonSerializer seri
try
{
object? value = null;
if (token.Type == JTokenType.Array && type == typeof((uint, uint)))
if (token.Type == JTokenType.Array && type == typeof((long, long)))
{
uint[]? o = token.ToObject<uint[]>(serializer);
long[]? o = token.ToObject<long[]>(serializer);
if (o != null)
{
value = (o[0], o[1]);
Expand Down Expand Up @@ -368,10 +368,10 @@ public override void WriteJson(JsonWriter writer, OrType<T, U, V>? value, JsonSe
{
writer.WriteNull();
}
else if (value?.Value?.GetType() == typeof((uint, uint)))
else if (value?.Value?.GetType() == typeof((long, long)))
{
ValueTuple<uint, uint> o = (ValueTuple<uint, uint>)(value.Value);
serializer.Serialize(writer, new uint[] { o.Item1, o.Item2 });
ValueTuple<long, long> o = (ValueTuple<long, long>)(value.Value);
serializer.Serialize(writer, new long[] { o.Item1, o.Item2 });
}
else
{
Expand All @@ -393,7 +393,7 @@ public class OrTypeConverter<T, U, V, W> : JsonConverter<OrType<T, U, V, W>>

Type[] types = new Type[] { typeof(T), typeof(U), typeof(V), typeof(W) };

if (reader.TokenType == JsonToken.Integer && (Validators.HasType(types, typeof(uint)) || Validators.HasType(types, typeof(int))))
if (reader.TokenType == JsonToken.Integer && (Validators.HasType(types, typeof(long)) || Validators.HasType(types, typeof(int))))
{
return ReadIntegerToken(reader, serializer, types);
}
Expand All @@ -417,23 +417,23 @@ public class OrTypeConverter<T, U, V, W> : JsonConverter<OrType<T, U, V, W>>
private static OrType<T, U, V, W> ReadIntegerToken(JsonReader reader, JsonSerializer serializer, Type[] types)
{
long integer = serializer.Deserialize<long>(reader);
if (Validators.InUIntegerRange(integer) && Validators.HasType(types, typeof(uint)))
if (Validators.InUIntegerRange(integer) && Validators.HasType(types, typeof(long)))
{
if (typeof(T) == typeof(uint))
if (typeof(T) == typeof(long))
{
return new OrType<T, U, V, W>((T)(object)(uint)integer);
return new OrType<T, U, V, W>((T)(object)(long)integer);
}
if (typeof(U) == typeof(uint))
if (typeof(U) == typeof(long))
{
return new OrType<T, U, V, W>((U)(object)(uint)integer);
return new OrType<T, U, V, W>((U)(object)(long)integer);
}
if (typeof(V) == typeof(uint))
if (typeof(V) == typeof(long))
{
return new OrType<T, U, V, W>((V)(object)(uint)integer);
return new OrType<T, U, V, W>((V)(object)(long)integer);
}
if (typeof(W) == typeof(uint))
if (typeof(W) == typeof(long))
{
return new OrType<T, U, V, W>((W)(object)(uint)integer);
return new OrType<T, U, V, W>((W)(object)(long)integer);
}
}
if (Validators.InIntegerRange(integer) && Validators.HasType(types, typeof(int)))
Expand All @@ -455,7 +455,7 @@ private static OrType<T, U, V, W> ReadIntegerToken(JsonReader reader, JsonSerial
return new OrType<T, U, V, W>((W)(object)(int)integer);
}
}
throw new ArgumentOutOfRangeException($"Integer out-of-range of LSP Signed Integer[{int.MinValue}:{int.MaxValue}] and out-of-range of LSP Unsigned Integer [{uint.MinValue}:{uint.MaxValue}] => {integer}");
throw new ArgumentOutOfRangeException($"Integer out-of-range of LSP Signed Integer[{int.MinValue}:{int.MaxValue}] and out-of-range of LSP Unsigned Integer [{long.MinValue}:{long.MaxValue}] => {integer}");
}

private static OrType<T, U, V, W> ReadFloatToken(JsonReader reader, JsonSerializer serializer, Type[] types)
Expand Down Expand Up @@ -532,9 +532,9 @@ private static OrType<T, U, V, W> ReadObjectToken(JToken token, JsonSerializer s
try
{
object? value = null;
if (token.Type == JTokenType.Array && type == typeof((uint, uint)))
if (token.Type == JTokenType.Array && type == typeof((long, long)))
{
uint[]? o = token.ToObject<uint[]>(serializer);
long[]? o = token.ToObject<long[]>(serializer);
if (o != null)
{
value = (o[0], o[1]);
Expand Down Expand Up @@ -582,10 +582,10 @@ public override void WriteJson(JsonWriter writer, OrType<T, U, V, W>? value, Jso
{
writer.WriteNull();
}
else if (value?.Value?.GetType() == typeof((uint, uint)))
else if (value?.Value?.GetType() == typeof((long, long)))
{
ValueTuple<uint, uint> o = (ValueTuple<uint, uint>)(value.Value);
serializer.Serialize(writer, new uint[] { o.Item1, o.Item2 });
ValueTuple<long, long> o = (ValueTuple<long, long>)(value.Value);
serializer.Serialize(writer, new long[] { o.Item1, o.Item2 });
}
else
{
Expand Down
20 changes: 20 additions & 0 deletions generator/plugins/dotnet/custom/Validators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,24 @@ public static bool InUIntegerRange(long value)
{
return value >= uint.MinValue && value <= uint.MaxValue;
}

public static long? validUInteger(long? value){
if(value == null){
return null;
}

if (Validators.InUIntegerRange((long)value))
{
return value;
}
throw new ArgumentOutOfRangeException("value", value, "Value is not in the range of LSP uinteger");
}

public static long validUInteger(long value){
if (Validators.InUIntegerRange(value))
{
return value;
}
throw new ArgumentOutOfRangeException("value", value, "Value is not in the range of LSP uinteger");
}
}
8 changes: 7 additions & 1 deletion generator/plugins/dotnet/dotnet_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def lsp_to_base_types(lsp_type: model.BaseType):
elif lsp_type.name in ["integer"]:
return "int"
elif lsp_type.name in ["uinteger"]:
return "uint"
return "long"
elif lsp_type.name in ["boolean"]:
return "bool"
elif lsp_type.name in ["null"]:
Expand Down Expand Up @@ -250,6 +250,12 @@ def generate_property(
lines.append(
f'public {type_name}{optional} {name} {{ get; init; }} = "{prop_def.type.value}";'
)
elif prop_def.type.kind == "base" and prop_def.type.name == "uinteger":
private_name = f"_{prop_def.name}" if prop_def.name == name else prop_def.name
lines.append(
f"public {type_name}{optional} {name} {{ get => {private_name}; set => {private_name} = Validators.validUInteger(value); }}"
)
lines.append(f"private {type_name}{optional} {private_name};")
else:
lines.append(f"public {type_name}{optional} {name} {{ get; init; }}")

Expand Down
Loading