user29071247
user29071247

Reputation: 1

Ballerina does not support "type" field in a json map

I use ballerina to process composer.json files. such files have repositories entries like this:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/itdoc-Dev/my_git_repository.git"
    },
    {
        "type": "vcs",
        "url": "https://github.com/symfony-Dev/another_git_repository.git"
    },
    ...
]

When such a json map is loaded in my ballerina script, the "type" field cannot be manipulated as other filed,

for example: this compiles:

string url = check theRepo.url.ensureType();

but this does not:

string type = check theRepo.type.ensureType();

the error is:

ERROR [dev_it.bal:(114:12,114:16)] invalid token 'type'
ERROR [dev_it.bal:(114:17,114:17)] missing identifier
ERROR [dev_it.bal:(114:33,114:33)] missing identifier
ERROR [dev_it.bal:(114:33,114:33)] missing semicolon token
ERROR [dev_it.bal:(114:33,114:37)] type definitions are allowed only at module level
ERROR [dev_it.bal:(114:37,114:38)] invalid token '.'

and my ballerina version is:

$bal --version
Ballerina 2201.10.3 (Swan Lake Update 10)
Language specification 2024R1
Update Tool 1.4.3

... I worked around it with this:


    theRepo["url"];
    theRepo["type"];

Upvotes: 0

Views: 20

Answers (1)

user29071247
user29071247

Reputation: 1

I fixed it after reading the the section about identifiers in the doc: https://ballerina.io/learn/by-example/identifiers/ identifiers that are reserved keywords should be prefixed with a single quote: see 'from variable example. so the fix is:

    string 'type = check theRepo.'type.ensureType();

Upvotes: 0

Related Questions