Skip to content

Commit

Permalink
samples updated
Browse files Browse the repository at this point in the history
  • Loading branch information
exilon committed Apr 6, 2019
1 parent b2f1ed3 commit 7739dc2
Show file tree
Hide file tree
Showing 33 changed files with 6,329 additions and 132 deletions.
38 changes: 38 additions & 0 deletions samples/delphi/QuickArrayHelper/ArrayHelpers.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
program ArrayHelpers;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
Quick.Commons,
Quick.Console,
Quick.Arrays.Helper;

var

myarray : TArray<string>;

begin
try
ReportMemoryLeaksOnShutdown := True;

myarray.Add('one');
myarray.Add('two');
myarray.Add('three');
coutFmt('count: %d',[myarray.Count],etInfo);
if myarray.Contains('two') then cout('found "two" in array',etInfo)
else cout('not found',etInfo);

coutFmt('"three" in position %d',[myarray.IndexOf('three')],etInfo);

TArrayHelper<string>.Add(myarray,'Four');

cout('Press <Enter> to Exit',ccYellow);
ConsoleWaitForEnterKey;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
678 changes: 678 additions & 0 deletions samples/delphi/QuickArrayHelper/ArrayHelpers.dproj

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions samples/delphi/QuickArrays/FlexArrays/ManageFlexArray.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
program ManageFlexArray;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
Quick.Commons,
Quick.Console,
Quick.Arrays;

type
TUser = class
private
fName : string;
public
property Name : string read fName write fName;
end;

var
xarThings : TFlexArray;
user : TUser;

begin
try
xarThings.Add(10);
xarThings.Add('Hello');
user := TUser.Create;
try
user.Name := 'Joe';
xarThings.Add(user);

cout('Integer Item = %d',[xarThings[0].AsInteger],etInfo);
cout('String Item = %s',[xarThings[1].AsString],etInfo);
cout('Record Item = %s',[TUser(xarThings[2]).Name],etInfo);
finally
user.Free;
end;
cout('Press <Enter> to Exit',ccYellow);
ConsoleWaitForEnterKey;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
678 changes: 678 additions & 0 deletions samples/delphi/QuickArrays/FlexArrays/ManageFlexArray.dproj

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions samples/delphi/QuickArrays/FlexPairArrays/ManageFlexPairArray.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
program ManageFlexPairArray;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
Quick.Commons,
Quick.Console,
Quick.Arrays;

type
TUser = class
private
fName : string;
public
property Name : string read fName write fName;
end;

var
flexarray : TFlexPairArray;
user : TUser;

begin
try
flexarray.Add('onenumber',10);
flexarray.Add('other','Hello boy!');
user := TUser.Create;
try
user.Name := 'Joe';
flexarray.Add('myuser',user);

cout('Integer Item = %d',[flexarray.GetValue('onenumber').AsInteger],etInfo);
cout('String Item = %s',[flexarray.GetValue('other').AsString],etInfo);
cout('Record Item = %s',[TUser(flexarray.GetValue('myuser')).Name],etInfo);
finally
user.Free;
end;

cout('Press <Enter> to Exit',ccYellow);
ConsoleWaitForEnterKey;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
680 changes: 680 additions & 0 deletions samples/delphi/QuickArrays/FlexPairArrays/ManageFlexPairArray.dproj

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions samples/delphi/QuickArrays/ManageArrays/ManageArrays.dpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
program ManageArrays;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
Quick.Commons,
Quick.Console,
Quick.Arrays;

type

TUser = record
Name : string;
Age : Integer;
end;

var
userarray : TXArray<TUser>;
user : TUser;
normalarray : TArray<TUser>;

begin
try
ReportMemoryLeaksOnShutdown := True;

user.Name := 'Joe';
user.Age := 30;
userarray.Add(user);
user.Name := 'Peter';
user.Age := 32;
userarray.Add(user);
user.Name := 'James';
user.Age := 40;
userarray.Add(user);

if userarray.Contains(user) then cout('found user in array',etInfo);

for user in userarray do
begin
coutFmt('User: %s',[user.Name],etInfo);
end;

normalarray := userarray;

coutFmt('Copied array value 1: %s',[normalarray[1].Name],etInfo);

cout('Press <Enter> to Exit',ccYellow);
ConsoleWaitForEnterKey;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
678 changes: 678 additions & 0 deletions samples/delphi/QuickArrays/ManageArrays/ManageArrays.dproj

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions samples/delphi/QuickAutoMapper/AutoMappingObjects.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,26 @@ begin
//User2 := TMapper<TUser2>.Map(User);
AutoMapper := TAutoMapper<TUser,TUser2>.Create;
try

//option1: you can define auto map different named properties
AutoMapper.CustomMapping.AddMap('Cash','Money');
AutoMapper.CustomMapping.AddMap('Id','IdUser');

//option2: you can decide to modify each property manually or allow to auto someones
AutoMapper.OnDoMapping := procedure(const aSrcObj : TUser; const aTargetName : string; out Value : TFlexValue)
begin
if aTargetName = 'Money' then Value := aSrcObj.Cash * 2
else if aTargetName = 'IdUser' then Value := aSrcObj.Id;
end;

//option3: you can modify some properties after automapping done
AutoMapper.OnAfterMapping := procedure(const aSrcObj : TUser; aTgtObj : TUser2)
begin
aTgtObj.Money := aSrcObj.Cash * 2;
aTgtObj.IdUser := aSrcObj.Id;
end;


User2 := AutoMapper.Map(User);
//User2 := TUser2.Create;
//User.MapTo(User2);
Expand Down
2 changes: 1 addition & 1 deletion samples/delphi/QuickAutoMapper/AutoMappingObjects.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{3F29272C-7851-41C3-B29E-C0ACD8029C21}</ProjectGuid>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<FrameworkType>None</FrameworkType>
<MainSource>AutoMappingObjects.dpr</MainSource>
<Base>True</Base>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
program JsonSerializerTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils,
System.Generics.Collections,
Quick.Commons,
Quick.Console,
Quick.Json.Serializer;

type
THost = class
private
fName : string;
fIP : string;
fPort : Integer;
published
property Name : string read fName write fName;
property IP : string read fIP write fIP;
property Port : Integer read fPort write fPort;
end;

THostList = TObjectList<THost>;

TConfig = class
private
fHosts : THostList;
fDebugMode : Boolean;
fLevel : Integer;
published
constructor Create;
destructor Destroy; override;
property Hosts : THostList read fHosts write fHosts;
property DebugMode : Boolean read fDebugMode write fDebugMode;
property Level : Integer read fLevel write fLevel;
end;

const
jsonstring = '{"Hosts":[{"Name":"Host 1 año perfeción","IP":"127.0.0.1","Port":80},{"Name":"Host 2","IP":"192.168.1.1","Port":443}],"DebugMode":true,"Level":1}';
jsonstring2 = '{"Hosts":{"List":[{"Name":"Host 1","IP":"127.0.0.2","Port":80},{"Name":"Host 2","IP":"192.168.1.2","Port":443}]},"DebugMode":true,"Level":2}';

var
config : TConfig;
host : THost;
serializer : TJsonSerializer;
json : string;

{ TConfig }

constructor TConfig.Create;
begin
fHosts := THostList.Create(True);
end;

destructor TConfig.Destroy;
begin
fHosts.Free;
inherited;
end;

begin
try
serializer := TJsonSerializer.Create(slPublishedProperty);
try

//created from object
cout('Create from object',ccYellow);
config := TConfig.Create;
try
host := THost.Create;
host.Name := 'Host 1';
host.IP := '127.0.0.1';
host.Port := 80;
config.DebugMode := True;
config.Level := 1;
config.Hosts.Add(host);

host := THost.Create;
host.Name := 'Host 2';
host.IP := '192.168.1.1';
host.Port := 443;
config.Hosts.Add(host);

json := serializer.ObjectToJson(config,True);
cout(json,ccWhite);
coutFmt('Capacity: %d / Count: %d',[config.Hosts.Capacity,config.Hosts.Count],etInfo);
finally
config.Free;
end;

//from json string without list property
cout('Create from jsonstring without "List" property',ccYellow);
config := TConfig.Create;
try
serializer.JsonToObject(config,jsonstring);
json := serializer.ObjectToJson(config,True);
cout(json,ccWhite);
coutFmt('Capacity: %d / Count: %d',[config.Hosts.Capacity,config.Hosts.Count],etInfo);
finally
config.Free;
end;

//from json string with list property
cout('Create from jsonstring with "List" property',ccYellow);
config := TConfig.Create;
try
serializer.JsonToObject(config,jsonstring2);
json := serializer.ObjectToJson(config,True);
cout(json,ccWhite);
coutFmt('Capacity: %d / Count: %d',[config.Hosts.Capacity,config.Hosts.Count],etInfo);
finally
config.Free;
end;
finally
serializer.Free;
end;
ConsoleWaitForEnterKey;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Loading

0 comments on commit 7739dc2

Please sign in to comment.