Refactoring Keyword mapping, introducing tuples
This commit is contained in:
+29
-24
@@ -14,23 +14,31 @@ uses
|
||||
|
||||
type
|
||||
TDataValueKind = (
|
||||
// base types
|
||||
vkVoid,
|
||||
vkScalar,
|
||||
vkText,
|
||||
vkMethod,
|
||||
// generic structured types
|
||||
vkTuple,
|
||||
vkRecord,
|
||||
// scalar structured types
|
||||
// vkScalarTuple, (later)
|
||||
vkScalarRecord,
|
||||
// Series and streams
|
||||
vkSeries,
|
||||
vkRecordSeries,
|
||||
vkScalarRecord,
|
||||
vkGenericRecord,
|
||||
vkStream,
|
||||
// internal types
|
||||
vkManagedObject,
|
||||
vkMethod,
|
||||
vkInterface,
|
||||
vkPointer,
|
||||
vkStream,
|
||||
vkGeneric
|
||||
);
|
||||
|
||||
TDataValue = record
|
||||
type
|
||||
TTuple = TArray<TDataValue>;
|
||||
TFunc = reference to function(const ArgNodes: TArray<TDataValue>): TDataValue;
|
||||
|
||||
private
|
||||
@@ -84,7 +92,7 @@ type
|
||||
class function Map(const SourceSeries: ISeries; const MapperFunc: TFunc): ISeries; static;
|
||||
|
||||
class function FromRecordSeries(const AValue: IWriteableScalarRecordSeries): TDataValue; static; inline;
|
||||
class function FromScalarRecord(const AValue: IScalarRecord): TDataValue; static; inline;
|
||||
class function FromScalarRecord(const AValue: IKeywordMapping<TScalar>): TDataValue; static; inline;
|
||||
class function FromGenericRecord(const AValue: IKeywordMapping<TDataValue>): TDataValue; static; inline;
|
||||
class function FromSeries(const AValue: ISeries): TDataValue; static; inline;
|
||||
class function FromStream(const AStream: IStream): TDataValue; static; inline;
|
||||
@@ -94,8 +102,8 @@ type
|
||||
function AsMethod: TFunc; inline;
|
||||
function AsText: String; inline;
|
||||
function AsRecordSeries: IWriteableScalarRecordSeries; inline;
|
||||
function AsScalarRecord: IScalarRecord; inline;
|
||||
function AsGenericRecord: IKeywordMapping<TDataValue>; inline;
|
||||
function AsScalarRecord: IKeywordMapping<TScalar>; inline;
|
||||
function AsRecord: IKeywordMapping<TDataValue>; inline;
|
||||
function AsSeries: ISeries; inline;
|
||||
function AsObject: TObject; overload; inline;
|
||||
|
||||
@@ -141,7 +149,7 @@ begin
|
||||
vkSeries: Result := 'Series';
|
||||
vkRecordSeries: Result := 'RecordSeries';
|
||||
vkScalarRecord: Result := 'ScalarRecord';
|
||||
vkGenericRecord: Result := 'GenericRecord';
|
||||
vkRecord: Result := 'Record';
|
||||
vkManagedObject: Result := 'Object';
|
||||
vkMethod: Result := 'Method';
|
||||
vkInterface: Result := 'Interface';
|
||||
@@ -268,7 +276,7 @@ begin
|
||||
Result.FInterface := AValue;
|
||||
end;
|
||||
|
||||
class function TDataValue.FromScalarRecord(const AValue: IScalarRecord): TDataValue;
|
||||
class function TDataValue.FromScalarRecord(const AValue: IKeywordMapping<TScalar>): TDataValue;
|
||||
begin
|
||||
Result.FKind := vkScalarRecord;
|
||||
Result.FInterface := AValue;
|
||||
@@ -276,7 +284,7 @@ end;
|
||||
|
||||
class function TDataValue.FromGenericRecord(const AValue: IKeywordMapping<TDataValue>): TDataValue;
|
||||
begin
|
||||
Result.FKind := vkGenericRecord;
|
||||
Result.FKind := vkRecord;
|
||||
Result.FInterface := AValue;
|
||||
end;
|
||||
|
||||
@@ -350,17 +358,17 @@ begin
|
||||
Result := Pointer(FScalar.Value.AsInt64);
|
||||
end;
|
||||
|
||||
function TDataValue.AsScalarRecord: IScalarRecord;
|
||||
function TDataValue.AsScalarRecord: IKeywordMapping<TScalar>;
|
||||
begin
|
||||
if (FKind <> vkScalarRecord) then
|
||||
raise EInvalidCast.Create('Cannot read value as Record.');
|
||||
Result := IScalarRecord(FInterface);
|
||||
Result := IKeywordMapping<TScalar>(FInterface);
|
||||
end;
|
||||
|
||||
function TDataValue.AsGenericRecord: IKeywordMapping<TDataValue>;
|
||||
function TDataValue.AsRecord: IKeywordMapping<TDataValue>;
|
||||
begin
|
||||
if (FKind <> vkGenericRecord) then
|
||||
raise EInvalidCast.Create('Cannot read value as GenericRecord.');
|
||||
if (FKind <> vkRecord) then
|
||||
raise EInvalidCast.Create('Cannot read value as Record.');
|
||||
Result := IKeywordMapping<TDataValue>(FInterface);
|
||||
end;
|
||||
|
||||
@@ -388,7 +396,6 @@ end;
|
||||
function TDataValue.ToString: String;
|
||||
var
|
||||
sb: TStringBuilder;
|
||||
field: TScalarRecordField;
|
||||
genField: TPair<IKeyword, TDataValue>;
|
||||
first: Boolean;
|
||||
i: Integer;
|
||||
@@ -412,8 +419,7 @@ begin
|
||||
begin
|
||||
if not first then
|
||||
sb.Append(',');
|
||||
field := series.Def[i];
|
||||
sb.Append(field.Key.Name);
|
||||
sb.Append(series.Def.Keywords[i].Name);
|
||||
first := False;
|
||||
end;
|
||||
sb.Append('}');
|
||||
@@ -433,7 +439,7 @@ begin
|
||||
begin
|
||||
if not first then
|
||||
sb.Append(',');
|
||||
sb.Append(rec[i].Key.Name);
|
||||
sb.Append(rec.Keywords[i].Name);
|
||||
first := False;
|
||||
end;
|
||||
sb.Append('}');
|
||||
@@ -442,9 +448,9 @@ begin
|
||||
sb.Free;
|
||||
end;
|
||||
end;
|
||||
vkGenericRecord:
|
||||
vkRecord:
|
||||
begin
|
||||
var rec := AsGenericRecord;
|
||||
var rec := AsRecord;
|
||||
sb := TStringBuilder.Create;
|
||||
try
|
||||
sb.Append('{');
|
||||
@@ -453,12 +459,11 @@ begin
|
||||
begin
|
||||
if not first then
|
||||
sb.Append(',');
|
||||
genField := rec[i];
|
||||
sb.Append(genField.Key.Name);
|
||||
sb.Append(rec.Keywords[i].Name);
|
||||
first := False;
|
||||
end;
|
||||
sb.Append('}');
|
||||
Result := Format('<generic_record%s>', [sb.ToString]);
|
||||
Result := Format('<record%s>', [sb.ToString]);
|
||||
finally
|
||||
sb.Free;
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user