Data Types
This commit is contained in:
@@ -7,7 +7,7 @@ uses
|
||||
System.SysUtils;
|
||||
|
||||
type
|
||||
TDataKind = (dkOrdinal, dkFloat, dkText, dkTimestamp, dkRecord, dkTuple, dkArray);
|
||||
TDataKind = (dkOrdinal, dkFloat, dkText, dkTimestamp, dkRecord, dkTuple, dkArray, dkEnum);
|
||||
|
||||
IDataType = interface
|
||||
function GetName: String;
|
||||
@@ -18,7 +18,9 @@ type
|
||||
|
||||
IDataValue = interface
|
||||
function GetDataType: IDataType;
|
||||
function GetAsString: string;
|
||||
property DataType: IDataType read GetDataType;
|
||||
property AsString: string read GetAsString;
|
||||
end;
|
||||
|
||||
IDataOrdinalValue = interface(IDataValue)
|
||||
@@ -57,6 +59,21 @@ type
|
||||
function CreateValue(const AValue: TDateTime): IDataTimestampValue;
|
||||
end;
|
||||
|
||||
IDataEnumValue = interface(IDataValue)
|
||||
function GetValue: Integer;
|
||||
property Value: Integer read GetValue;
|
||||
end;
|
||||
|
||||
IDataEnumType = interface(IDataType)
|
||||
function GetIdentifier(Idx: Integer): string;
|
||||
function GetIdentifierCount: Integer;
|
||||
function IndexOf(const AIdentifier: string): Integer;
|
||||
function CreateValue(const AValue: Integer): IDataEnumValue; overload;
|
||||
function CreateValue(const AIdentifier: string): IDataEnumValue; overload;
|
||||
property IdentifierCount: Integer read GetIdentifierCount;
|
||||
property Identifiers[Idx: Integer]: string read GetIdentifier; default;
|
||||
end;
|
||||
|
||||
IDataRecordValue = interface(IDataValue)
|
||||
function GetItem(Idx: Integer): IDataValue;
|
||||
property Items[Idx: Integer]: IDataValue read GetItem; default;
|
||||
@@ -127,6 +144,7 @@ type
|
||||
class function ArrayOfType(const AElementType: IDataType): IDataArrayType; static;
|
||||
class function RecordOf(const Fields: TArray<TRecordField>): IDataRecordType; overload; static;
|
||||
class function RecordOf(const Fields: array of TRecordField): IDataRecordType; overload; static;
|
||||
class function EnumOf(const AName: string; const AIdentifiers: array of string): IDataEnumType; static;
|
||||
|
||||
property DataType: IDataType read FDataType;
|
||||
property Name: String read GetName;
|
||||
@@ -147,6 +165,7 @@ type
|
||||
function AsRecord: IDataRecordValue;
|
||||
function AsTuple: IDataTupleValue;
|
||||
function AsArray: IDataArrayValue;
|
||||
function AsEnum: IDataEnumValue;
|
||||
|
||||
class operator Implicit(const A: IDataValue): TDataValue; overload;
|
||||
class operator Implicit(const A: TDataValue): IDataValue; overload;
|
||||
@@ -173,7 +192,8 @@ uses
|
||||
Myc.Data.Types.Timestamp,
|
||||
Myc.Data.Types.Arrays,
|
||||
Myc.Data.Types.Records,
|
||||
Myc.Data.Types.Tuple;
|
||||
Myc.Data.Types.Tuple,
|
||||
Myc.Data.Types.Enum;
|
||||
|
||||
{ TRecordField }
|
||||
|
||||
@@ -262,6 +282,11 @@ begin
|
||||
Result := RecordOf(tFields);
|
||||
end;
|
||||
|
||||
class function TDataType.EnumOf(const AName: string; const AIdentifiers: array of string): IDataEnumType;
|
||||
begin
|
||||
Result := TImplDataEnumType.Create(AName, AIdentifiers);
|
||||
end;
|
||||
|
||||
class function TDataType.Text: IDataTextType;
|
||||
begin
|
||||
Result := TImplDataTextType.Singleton;
|
||||
@@ -343,6 +368,13 @@ begin
|
||||
Result := IDataTupleValue(FDataValue);
|
||||
end;
|
||||
|
||||
function TDataValue.AsEnum: IDataEnumValue;
|
||||
begin
|
||||
if FDataValue.DataType.Kind <> dkEnum then
|
||||
raise EInvalidCast.Create('Enum expected');
|
||||
Result := IDataEnumValue(FDataValue);
|
||||
end;
|
||||
|
||||
class function TDataValue.FromOrdinal(const AValue: Int64): IDataOrdinalValue;
|
||||
begin
|
||||
Result := TImplDataOrdinalType.Singleton.CreateValue(AValue);
|
||||
|
||||
Reference in New Issue
Block a user