Guest User

Untitled

a guest
Oct 11th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.09 KB | None | 0 0
  1. program PropertyField_02dField_01;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   mormot.core.json,
  7.   mormot.core.rtti,
  8.   mormot.orm.core;
  9.  
  10. type
  11.   TSource = class(TSynAutoCreateFields)
  12.   private
  13.     FField_01: UTF8String;
  14.     FField_02: UTF8String;
  15.   published
  16.     property Field_01: UTF8String read FField_01 write FField_01;
  17.     // even though unused in sample, the prop is needed to force error
  18.     property Field_02: UTF8String read FField_02 write FField_02;
  19.   end;
  20.  
  21.   TDestination = class(TOrm)
  22.   private
  23.     FDestProp: UTF8String;
  24.   published
  25.     property DestProp: UTF8String read FDestProp write FDestProp;
  26.   end;
  27.  
  28. var
  29.   Source: TSource;
  30.   Dest: TDestination;
  31.   SourceRtti, DestRtti: TRttiCustom;
  32.   SourceProp, DestProp: PRttiCustomProp;
  33.  
  34.   CopyData: TRttiVarData;
  35. begin
  36.   // register classes
  37.   DestRtti := Rtti.RegisterClass(TSource);
  38.   SourceRtti := Rtti.RegisterClass(TDestination);
  39.  
  40.   // get source and dest props
  41.   SourceProp := DestRtti.Props.Find('Field_01');
  42.   DestProp := SourceRtti.Props.Find('DestProp');
  43.  
  44.   // check them
  45.   Writeln('PropSource ', SourceProp^.Name);
  46.   Writeln('PropDest ', DestProp^.Name);
  47.  
  48.   // create source
  49.   Source := TSource.Create;
  50.   try
  51.     // and prepare
  52.  
  53.     // Note: If you put something into Field_01, everything works as expected
  54.     //       it seems to be the empty string, that causes the trouble
  55.     Source.Field_01 := '';
  56.     Source.Field_02 := '';
  57.  
  58.     // create dest
  59.     Dest := TDestination.Create;
  60.     try
  61.       // read source value
  62.       SourceProp^.GetValue(Source, CopyData);
  63.  
  64.       Writeln(CopyData.VType);
  65.       // CopyData.VType will be   256/0x0100 for empty string
  66.       // CopyData.VType will be 16640/0x4100 for non-empty string
  67.  
  68.       // write to dest - EXCEPTION on empty string value
  69.  
  70.       // Note: I have tried both for "andclear", true and false
  71.       //       without seeing a difference
  72.       DestProp^.SetValue(Dest, CopyData, False);
  73.  
  74.       // write content of dest
  75.       Writeln('Destination.DestProp _', Dest.DestProp, '_');
  76.     finally
  77.       Dest.Free;
  78.     end;
  79.   finally
  80.     Source.Free;
  81.   end;
  82. end.
Advertisement
Add Comment
Please, Sign In to add comment