-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Generation/Generator/Renderer/Public/Field/Converter/Bitfield.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class Bitfield : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Bitfield>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
var type = (GirModel.Bitfield) field.AnyTypeOrCallback.AsT0.AsT0; | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.ComplexType.GetFullyQualified(type); | ||
} | ||
} | ||
|
26 changes: 26 additions & 0 deletions
26
src/Generation/Generator/Renderer/Public/Field/Converter/Enumeration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class Enumeration : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Enumeration>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
var type = (GirModel.Enumeration) field.AnyTypeOrCallback.AsT0.AsT0; | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.ComplexType.GetFullyQualified(type); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/Generation/Generator/Renderer/Public/Field/Converter/PrimitiveValueType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class PrimitiveValueType : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.PrimitiveValueType>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.Type.GetName(field.AnyTypeOrCallback.AsT0.AsT0); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Generation/Generator/Renderer/Public/Field/Converter/String.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class String : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.String>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: Type.GetName(field.AnyTypeOrCallback.AsT0.AsT0) | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Generation/Generator/Renderer/Public/Field/FieldConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
public interface FieldConverter | ||
{ | ||
bool Supports(GirModel.Field field); | ||
RenderableField Convert(GirModel.Field field); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Generator.Renderer.Public; | ||
|
||
internal static class Fields | ||
{ | ||
private static readonly List<Field.FieldConverter> Converters = new() | ||
{ | ||
new Field.Bitfield(), | ||
new Field.Enumeration(), | ||
new Field.PrimitiveValueType(), | ||
new Field.String(), | ||
}; | ||
|
||
public static Field.RenderableField GetRenderableField(GirModel.Field field) | ||
{ | ||
foreach (var converter in Converters) | ||
if (converter.Supports(field)) | ||
return converter.Convert(field); | ||
|
||
throw new System.Exception($"Internal field \"{field.Name}\" of type {field.AnyTypeOrCallback} can not be rendered"); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Generation/Generator/Renderer/Public/Field/RenderableField.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
public record RenderableField(string Name, string NullableTypeName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters