Recent

Author Topic: Enumerate record feld names  (Read 13651 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Enumerate record feld names
« Reply #15 on: December 11, 2021, 08:55:50 pm »
I suspect it will be quicker for me to use Python to parse the DLL record definitions and emit the appropriate Pascal source code for the field name -> data mappings.

Thank you very much for your responses.

Yes, possibly. Obviously the fact that the Pascal unit is purely declarative and error-free is a big help, i.e. your lexer can break on any non-identifier (etc.) rather than having to work through character-by-character and having to be prepared for arbitrary expressions.

You're welcome to the little help I was able to offer. You've got quite a job on your hands.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

GhostlyMan

  • New Member
  • *
  • Posts: 14
Re: Enumerate record feld names
« Reply #16 on: December 11, 2021, 09:28:02 pm »
Quote
You've got quite a job on your hands.

Fortunately not. I have the records parsed into an intermediate form, which I used to generate, via a template,  the code for the Pydantic data models.
Code: Text  [Select][+][-]
  1. struct_name = 'TBatchCURec'
  2.  
  3. struct_desc = 'Customer/Supplier Record'
  4.  
  5. struct_def = [
  6.     # (name, array_size, pascal_type, qty, excluded, comment)
  7.     ('cust_code', 0, 'String', 6, False, 'Customer code, must not be blank'),
  8.     ('cust_supp', 0, 'Char', 1, False, 'Must be C or S'),
  9.     ('company', 0, 'String', 45, False, 'Company Name'),
  10.     ('area_code', 0, 'String', 4, False, 'Free Type Sort Field'),
  11.     ('rep_code', 0, 'String', 4, False, 'Free Type Sort Field'),
  12.     ('remit_code', 0, 'String', 10, False, 'Account Code of Remit Account'),
  13.     ('vat_reg_no', 0, 'String', 30, False, 'VAT Registration No.'),
  14.     ('addr', 5, 'String', 30, False, 'Address 1-5'),
  15.     ('pad_char5', 0, 'Char', 1, True, ''),
  16.     ('desp_addr', 0, 'WordBool', 1, False, 'Despatch Address'),
  17.     ('d_addr', 5, 'String', 30, False, 'Despatch Address 1-5'),
  18.     ('contact', 0, 'String', 25, False, 'Contact Name'),
  19.     ('phone', 0, 'String', 30, False, 'Phone No.'),
  20.     ('fax', 0, 'String', 30, False, 'Fax No.'),
  21.     ('ref_no', 0, 'String', 10, False, 'Our Code with them'),
  22.     ('trad_term', 0, 'WordBool', 1, False, 'Special Terms'),
  23.     ('s_terms', 2, 'String', 60, False, '2 Lines of Terms'),
  24.     ('currency', 0, 'SmallInt', 1, False, ''),
  25.     ('vat_code', 0, 'Char', 1, False, ''),
  26.     ('pad_char1', 0, 'Char', 1, True, 'Padding Char for Word Alignment'),
  27.     ('pay_terms', 0, 'SmallInt', 1, False, 'Number of days, 0-30, 999...'),
  28.     ('credit_limit', 0, 'Double', 1, False, ''),
  29.     ('discount', 0, 'Double', 1, False, ''),
  30.     ('credit_status', 0, 'SmallInt', 1, False, ''),
  31.     ('cust_cc', 0, 'String', 3, False, ''),
  32.     ('c_disc_ch', 0, 'Char', 1, False, ''),
  33.     ('cust_dep', 0, 'String', 3, False, ''),
  34.     ('pad_char2', 0, 'Char', 1, True, 'Padding Char for Word Alignment'),
  35.     ('eec_member', 0, 'WordBool', 1, False, 'VAT Inclusion for EE'),
  36.     ('inc_stat', 0, 'WordBool', 1, False, 'Include in Statement'),
  37.     ('def_nom_code', 0, 'LongInt', 1, False, 'DefNomCode'),
  38.     ('def_m_loc_stk', 0, 'String', 3, False, 'Default Multi Loc Stock'),
  39.     ('acc_status', 0, 'SmallInt', 1, False, 'On Hold, Closed, See notes'),
  40.     ('pay_type', 0, 'Char', 1, False, '[B]acs,[C]ash'),
  41.     ('bank_sort', 0, 'String', 15, False, 'Bank Sort Code'),
  42.     ('bank_acc', 0, 'String', 20, False, 'Bank Account No.'),
  43.     ('bank_ref', 0, 'String', 28, False, 'Bank additional ref, ie Build Soc.Acc'),
  44.     ('last_used', 0, 'String', 8, False, 'ReadOnly - Date last updated'),
  45.     # {Added on 7.1.97}
  46.     ('phone2', 0, 'String', 30, False, 'Second Phone No.'),
  47.     ('user_def1', 0, 'String', 20, False, 'User Definable string1'),
  48.     ('user_def2', 0, 'String', 20, False, 'User Definable string2'),
  49.     ('sop_inv_code', 0, 'String', 6, False, 'Ent SOP Invoice Code'),
  50.     ('pad_char3', 2, 'Char', 1, True, 'Padding Char for Word Alignment'),
  51.     ('sop_auto_w_off', 0, 'WordBool', 1, False, 'Auto write off Sales Order'),
  52.     ('pad_char6', 2, 'Char', 1, True, ''),
  53.     ('b_ord_val', 0, 'Double', 1, False, 'Book order value'),
  54.     # {* Added on 23/07/97 *}
  55.     ('def_cos_nom', 0, 'LongInt', 1, False, 'Cost of Sales Nominal Code'),
  56.     ('def_ctrl_nom', 0, 'LongInt', 1, False, 'Default Control Nominal Code'),
  57.     # {* Added on 20.1.98 *}
  58.     ('dir_deb', 0, 'SmallInt', 1, False, 'Current Direct Debit Mode'),
  59.     ('ccd_s_date', 0, 'String', 8, False, 'Credit Card Start Date'),
  60.     ('ccd_e_date', 0, 'String', 8, False, 'Credit Card End Date'),
  61.     ('ccd_name', 0, 'String', 50, False, 'Name on Credit Card'),
  62.     ('ccd_card_no', 0, 'String', 30, False, 'Credit Card No.'),
  63.     ('ccd_sa_ref', 0, 'String', 4, False, 'Credit Card Switch Ref'),
  64.     #
  65.     ('pad_char7', 0, 'Char', 1, True, ''),
  66.     # {* Added on 17.11.98 *}
  67.     ('def_set_d_days', 0, 'SmallInt', 1, False, 'Default Sett.Discount Days'),
  68.     ('pad_char4', 2, 'Char', 1, True, 'Padding Char for Word Alignment'),
  69.     ('def_set_disc', 0, 'Double', 1, False, 'Default Sett.Discount %'),
  70.     ('def_form_no', 0, 'SmallInt', 1, False, 'Default Form Set No., 0-99, default 0'),
  71.     #
  72.     # {*** New fields in ver 4.31 ***}
  73.     ('stat_d_mode', 0, 'SmallInt', 1, False, 'Statement/Remittance delivery mode. 0=Printed, 1=Fax, 2=email'),
  74.     ('email_addr', 0, 'String', 100, False, 'Email address for Statement/ Remittance'),
  75.     ('pad_char', 0, 'Char', 1, True, ''),
  76.     ('eml_snd_rdr', 0, 'WordBool', 1, False, 'On next email transmission, send reader & reset'),
  77.     ('ebus_pwrd', 0, 'String', 20, False, 'ebusiness module web password'),
  78.     ('post_code', 0, 'String', 20, False, 'Separate postcode ** Add index'),
  79.     ('cust_code2', 0, 'String', 20, False, 'Alternative look up code, can be blank'),
  80.     ('pad_char8', 0, 'Char', 1, True, ''),
  81.     ('allow_web', 0, 'SmallInt', 1, False, 'Allow upload to Web'),
  82.     ('eml_zip_atc', 0, 'WordBool', 1, False, 'Default Zip attachment'),
  83.     ('user_def3', 0, 'String', 30, False, 'User Defined 3'),
  84.     ('user_def4', 0, 'String', 30, False, 'User Defined 4'),
  85.     ('time_change', 0, 'String', 6, False, 'ReadOnly - Time stamp for record Change'),
  86.     ('ssd_del_terms', 0, 'String', 5, False, 'ReadOnly - Delivery Terms'),
  87.     ('c_vat_inc_flg', 0, 'Char', 1, False, ''),
  88.     ('ssd_mode_tr', 0, 'SmallInt', 1, False, ''),
  89.     ('last_opo', 0, 'String', 10, False, ''),
  90.     ('pad_char9', 0, 'Char', 1, True, ''),
  91.     ('inv_d_mode', 0, 'SmallInt', 1, False, 'Invoice delivery mode'),
  92.     ('eml_snd_html', 0, 'WordBool', 1, False, 'When sending XML, send HTML'),
  93.     ('web_live_cat', 0, 'String', 20, False, 'Read Only - Web current catalogue entry'),
  94.     ('web_prev_cat', 0, 'String', 20, False, 'Web previous catalogue entry'),
  95.     #
  96.     # {27.09.2000 }
  97.     ('sop_cons_ho', 0, 'SmallInt', 1, False, 'Consolidate Committed Balance. 0 or 1 only'),
  98.     #
  99.     # {*** ------------------------------------------------------ ***}
  100.     #
  101.     ('spare', 129, 'Char', 1, True, '131/129'),
  102.     ('last_char', 0, 'Char', 1, True, 'Padding Char for Word Alignment'),
  103. ]
  104.  
  105. struct_size = 1432
  106.  

I just need to decide upon the appropriate Free Pascal code to use as a field name -> data mapping, write the template and then the jobs done.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Enumerate record feld names
« Reply #17 on: December 11, 2021, 09:44:15 pm »
Looking good :-)

OK, you /had/ quite a job on your hands :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Blade

  • Full Member
  • ***
  • Posts: 177
Re: Enumerate record feld names
« Reply #18 on: December 12, 2021, 01:31:57 am »
I would like to have a general function which takes any record and enumerates through the fields to produce the equivalent JSON output.
Of the signature - RecordToJSON(Rec: Record): String

Not sure if you would be interested or how useful to your exact problem, but maybe check out RecordSaveJSON at https://raw.githubusercontent.com/synopse/mORMot/master/SynCommons.pas (Synopse mORMot Framework, SynCommons.pas).

GhostlyMan

  • New Member
  • *
  • Posts: 14
Re: Enumerate record feld names
« Reply #19 on: December 12, 2021, 03:23:35 pm »
@Blade

Thank you very much for the useful suggestion and link.

Looking through the code and this helpful blog post (https://blog.synopse.info/?post/2013/12/10/JSON-record-serialization) all looked like it might be a good solution. However, unfortunately, it doesn't support the ShortStrings that my interface/toolkit DLL records have in their definitions.  :(

The good news is that you have prompted me to have another, deeper look at mORMot and I think that it will be very useful in future projects.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Enumerate record feld names
« Reply #20 on: December 13, 2021, 02:11:27 pm »
I hope so. But it depends on the developers, before some things were available a long time in the trunk and did not get into the release

Anything that's in trunk/main comes into the next major release (which currently would be 3.4.0). The fixes branches never get new features (yes, there have been exceptions in the past, but they're exceptions and it's especially not done for big features).

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Enumerate record feld names
« Reply #21 on: December 13, 2021, 02:23:06 pm »
@GhostlyMan
For (near) future projects, consider mORMot2. Its the future of mORMot itself, and has much better RTTI (based on FPC typinfo), a feature you need for your goal.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Enumerate record feld names
« Reply #22 on: December 14, 2021, 09:24:16 am »
@GhostlyMan
For (near) future projects, consider mORMot2. Its the future of mORMot itself, and has much better RTTI (based on FPC typinfo), a feature you need for your goal.

It won't help him for this specific case, cause records still won't have any field names. And you can't magic up RTTI that simply isn't there.

 

TinyPortal © 2005-2018