Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: ATTRIBUTE or ATTRIBUTES  (Read 912 times)
Benjamin G.
Posts: 130


« on: October 03, 2024, 11:43:01 am »

Hi,

Look at this 4gl codes (tested version 4 and 5) :

1/
DEFINE jdb RECORD ATTRIBUTES(json_name="database")
    rec_tlsbonret  DYNAMIC ARRAY ATTRIBUTE(json_name="tlsbonret_2") OF t_tlsbonret
   ,rec_tlsclidis  DYNAMIC ARRAY ATTRIBUTES(json_name="tlsclidis_2") OF t_tlsclidis
    END RECORD
DISPLAY util.JSON.stringify(jdb)
It seems that you can use the two syntax ATTRIBUTE or ATRIBUTES and it's works.

2/
This syntax is accepted by the compiler but at runtime ATTRIBUTE(S) are ignored ...

DEFINE jdb RECORD ATTRIBUTES(json_name="database")
    rec_tlsbonret  DYNAMIC ARRAY  OF t_tlsbonret ATTRIBUTE(json_name="tlsbonret_2")
   ,rec_tlsclidis  DYNAMIC ARRAY OF t_tlsclidis ATTRIBUTES(json_name="tlsclidis_2")
    END RECORD
DISPLAY util.JSON.stringify(jdb)

Regards
Sebastien F.
Four Js
Posts: 545


« Reply #1 on: October 03, 2024, 02:58:59 pm »

Hello Benjamin,

1/

The fglcomp compiler considers ATTRIBUTE and ATTRIBUTES as synonyms (I would say for backward compatibility)

We have only documented the second keyword, because you can specify multiple attributeS in this clause.

If some consider that we should document both keywords, the syntax diagram would look like this:

Code:
ATTRIBUTE[S] ( attribute [ = "value" ] [,...] )

or

Code:
{ATTRIBUTE|ATTRIBUTES} ( attribute [ = "value" ] [,...] )

instead of current simple diagram:

Code:
ATTRIBUTES ( attribute [ = "value" ] [,...] )

https://4js.com/online_documentation/fjs-fgl-manual-html/#fgl-topics/c_fgl_variables_attributes.html

If we document both, programmers will tend to use sometimes ATTRIBUTE and sometime ATTRIBUTES, with the risk to miss one or the other when searching in your code ( vim ctrl-* )

Return to simplicity.

2/

In JSON, array elements have no name.

Try to put the ATTRIBUTES() clause just after DYNAMIC ARRAY:

Code
  1. DEFINE jdb RECORD ATTRIBUTES(json_name="database")
  2.    rec_tlsbonret DYNAMIC ARRAY ATTRIBUTES(json_name="tlsbonret_2") OF t_tlsbonret
  3.   ,rec_tlsclidis DYNAMIC ARRAY ATTRIBUTES(json_name="tlsclidis_2") OF t_tlsclidis
  4.    END RECORD
  5.  

output (with my own t_tlsbonret and t_tlsclidis types):

Code:
{
    "tlsbonret_2": [
        {
            "field11": 9991
        }
    ],
    "tlsclidis_2": [
        {
            "field21": 9992
        }
    ]
}

Seb
Benjamin G.
Posts: 130


« Reply #2 on: October 03, 2024, 03:11:29 pm »

Hi,

for the point "2" i think that compiler must be at least "warn" the user because the syntax will not produce the good result ...

Regards
Sebastien F.
Four Js
Posts: 545


« Reply #3 on: October 04, 2024, 09:34:06 am »

Hello,

1/

After a discussion with the team we will document ATTRIBUTE and ATTRIBUTES as synonyms.

2/

Sometimes the ATTRIBUTE/ATTRIBUTES clause can be used at the array element level:

See this:
Code
  1. sf@toro:/tmp$ cat x.4gl
  2. IMPORT util
  3.  
  4. TYPE t_rec RECORD
  5.         field INTEGER
  6.     END RECORD
  7.  
  8. DEFINE root_rec RECORD ATTRIBUTES(json_name="database")
  9.        arr1 DYNAMIC ARRAY ATTRIBUTES(json_name="_arr1_") OF t_rec ATTRIBUTES(json_null="null")
  10.       ,arr2 DYNAMIC ARRAY ATTRIBUTES(json_name="_arr2_") OF t_rec
  11.    END RECORD
  12.  
  13. MAIN
  14.  
  15.    LET root_rec.arr1[1].field = 101
  16.    --LET root_rec.arr1[2].field = 102
  17.    LET root_rec.arr1[3].field = 103
  18.  
  19.    LET root_rec.arr2[1].field = 201
  20.    --LET root_rec.arr2[2].field = 202
  21.    LET root_rec.arr2[3].field = 203
  22.  
  23.    DISPLAY util.JSON.format( util.JSON.stringify(root_rec) )
  24.  
  25. END MAIN
  26. sf@toro:/tmp$ fglcomp -M x.4gl && fglrun x.42m
  27. {
  28.     "_arr1_": [
  29.         {
  30.             "field": 101
  31.         },
  32.        null,
  33.        {
  34.             "field": 103
  35.         }
  36.    ],
  37.    "_arr2_": [
  38.        {
  39.             "field": 201
  40.         },
  41.        {},
  42.        {
  43.             "field": 203
  44.         }
  45.    ]
  46. }
  47.  
Benjamin G.
Posts: 130


« Reply #4 on: October 04, 2024, 09:48:00 am »

Hi,

the second example should be included in the documentation

Regards
Reuben B.
Four Js
Posts: 1119


« Reply #5 on: October 06, 2024, 10:40:35 pm »

Hi,

the second example should be included in the documentation

Regards

... I did write an article on that about six/seven months ago after we had a couple of support calls all falling into the same trap https://4js.com/ask-reuben/ig-217/

Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines