ATTRIBUTE or ATTRIBUTES

Started by Benjamin G., October 03, 2024, 11:43:01 AM

Previous topic - Next topic

Benjamin G.

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.

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:

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

or

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

instead of current simple diagram:

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 (genero) Select
DEFINE jdb RECORD ATTRIBUTES(json_name="database")
    rec_tlsbonret DYNAMIC ARRAY ATTRIBUTES(json_name="tlsbonret_2") OF t_tlsbonret
   ,rec_tlsclidis DYNAMIC ARRAY ATTRIBUTES(json_name="tlsclidis_2") OF t_tlsclidis
    END RECORD


output (with my own t_tlsbonret and t_tlsclidis types):


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


Seb

Benjamin G.

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.

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 (genero) Select

sf@toro:/tmp$ cat x.4gl
IMPORT util

TYPE t_rec RECORD
         field INTEGER
     END RECORD

DEFINE root_rec RECORD ATTRIBUTES(json_name="database")
        arr1 DYNAMIC ARRAY ATTRIBUTES(json_name="_arr1_") OF t_rec ATTRIBUTES(json_null="null")
       ,arr2 DYNAMIC ARRAY ATTRIBUTES(json_name="_arr2_") OF t_rec
    END RECORD

MAIN

    LET root_rec.arr1[1].field = 101
    --LET root_rec.arr1[2].field = 102
    LET root_rec.arr1[3].field = 103

    LET root_rec.arr2[1].field = 201
    --LET root_rec.arr2[2].field = 202
    LET root_rec.arr2[3].field = 203

    DISPLAY util.JSON.format( util.JSON.stringify(root_rec) )

END MAIN
sf@toro:/tmp$ fglcomp -M x.4gl && fglrun x.42m
{
    "_arr1_": [
        {
            "field": 101
        },
        null,
        {
            "field": 103
        }
    ],
    "_arr2_": [
        {
            "field": 201
        },
        {},
        {
            "field": 203
        }
    ]
}

Benjamin G.

Hi,

the second example should be included in the documentation

Regards

Reuben B.

Quote from:  . on October 04, 2024, 09:48:00 AM
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