Global data value refactoring + 1st scripting version

This commit is contained in:
Michael Schimmel
2025-09-20 18:30:32 +02:00
parent 09bd25b318
commit 00f5861148
17 changed files with 1430 additions and 2742 deletions
+18 -31
View File
@@ -12,8 +12,8 @@ uses
type
TRttiAstHelper = class
private
// Maps a Delphi RTTI type to its TScalarKind.
class function TypeToScalarKind(AType: TRttiType): TScalarKind; static;
// Maps a Delphi RTTI type to its TScalar.TKind.
class function TypeToScalarKind(AType: TRttiType): TScalar.TKind; static;
public
// Creates a JSON definition string from a record type info.
class function RecordDefinitionToJson<T: record>: string; static;
@@ -43,7 +43,6 @@ begin
exit;
try
// The root element is expected to be a JSON array directly.
if not (jsonValue is TJSONArray) then
exit;
@@ -54,12 +53,11 @@ begin
begin
fieldObj := fieldsArray.Items[i] as TJSONObject;
if not Assigned(fieldObj) then
continue; // or raise error
continue;
fields[i].Name := fieldObj.GetValue<string>('name');
kindStr := fieldObj.GetValue<string>('kind');
fields[i].Kind := TScalarKind(GetEnumValue(TypeInfo(TScalarKind), kindStr));
fields[i].Kind := TScalar.TKind(GetEnumValue(TypeInfo(TScalar.TKind), kindStr));
end;
Result := TScalarRecordDefinition.Create(fields);
finally
@@ -84,54 +82,43 @@ begin
rttiRecordType := rttiType as TRttiRecordType;
// The root element is now the array itself.
fieldsArray := TJSONArray.Create;
try
for field in rttiRecordType.GetFields do
begin
fieldObj := TJSONObject.Create;
fieldObj.AddPair('name', TJSONString.Create(field.Name));
fieldObj.AddPair('kind', TJSONString.Create(GetEnumName(TypeInfo(TScalarKind), Ord(TypeToScalarKind(field.FieldType)))));
fieldObj.AddPair('kind', TJSONString.Create(GetEnumName(TypeInfo(TScalar.TKind), Ord(TypeToScalarKind(field.FieldType)))));
fieldsArray.Add(fieldObj);
end;
// Convert the array directly to a JSON string.
Result := fieldsArray.ToJSON;
finally
fieldsArray.Free;
end;
end;
class function TRttiAstHelper.TypeToScalarKind(AType: TRttiType): TScalarKind;
class function TRttiAstHelper.TypeToScalarKind(AType: TRttiType): TScalar.TKind;
var
typeHandle: PTypeInfo;
begin
// 1. Use a case statement or if-checks on AType.Handle.
typeHandle := AType.Handle;
// 2. Compare with TypeInfo(Integer), TypeInfo(Double), TypeInfo(TDecimal) etc.
if typeHandle = TypeInfo(Integer) then
Result := skInteger
else if typeHandle = TypeInfo(Int64) then
Result := skInt64
if typeHandle = TypeInfo(Int64) then
Result := TScalar.TKind.Ordinal
else if typeHandle = TypeInfo(Integer) then
Result := TScalar.TKind.Ordinal
else if typeHandle = TypeInfo(UInt64) then
Result := skUInt64
else if typeHandle = TypeInfo(Single) then
Result := skSingle
else if typeHandle = TypeInfo(Double) then
Result := skDouble
else if typeHandle = TypeInfo(TDateTime) then
Result := skDateTime
Result := TScalar.TKind.Ordinal
else if typeHandle = TypeInfo(Boolean) then
Result := skBoolean
else if typeHandle = TypeInfo(Char) then
Result := skChar
else if typeHandle = TypeInfo(TDecimal) then
Result := skDecimal
else if typeHandle = TypeInfo(TTimestamp) then
Result := skTimestamp
Result := TScalar.TKind.Ordinal
else if typeHandle = TypeInfo(Double) then
Result := TScalar.TKind.Float
else if typeHandle = TypeInfo(Single) then
Result := TScalar.TKind.Float
else if typeHandle = TypeInfo(TDateTime) then
Result := TScalar.TKind.Float // Correctly handle TDateTime as a Float
else
// 4. Raise an exception for unsupported types.
raise EArgumentException.CreateFmt('Unsupported record field type: %s', [AType.Name]);
end;
+238 -1128
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -53,6 +53,7 @@ type
class function Void: TDataValue; inline; static;
class operator Implicit(const AValue: TScalar): TDataValue; overload; inline;
class operator Implicit(const AValue: TDataValue): TScalar; overload; inline;
class operator Implicit(const AValue: String): TDataValue; overload; inline;
class operator Implicit(const AValue: TDataValue.TFunc): TDataValue; overload; inline;
class operator Implicit(const AValue: TObject): TDataValue; overload; inline;
@@ -435,6 +436,11 @@ begin
Result.FInterface := TObjVal.Create(AValue);
end;
class operator TDataValue.Implicit(const AValue: TDataValue): TScalar;
begin
Result := AValue.AsScalar;
end;
{ TDataValueKindHelper }
function TDataValueKindHelper.ToString: string;