Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unions generation: The operator '[]' isn't defined for the class 'int' #894

Open
supervital opened this issue Oct 4, 2022 · 4 comments
Open

Comments

@supervital
Copy link

Hello,

I have the next union:

"unions": {
    "myValue": {
      "types": [
        {
          "type": "integer",
          "description": "Simple integer value."
        },
        {
          "type": "double",
          "description": "Simple double value."
        },
        {
          "type": "someCustomValue",
          "description": "A custom value."
        },
        {
          "type": "someCustomValue2",
          "description": "A custom value."
        }
      ]
    }
  }

And while compiling I get the next error:

Error: The operator '[]' isn't defined for the class 'int'.
[ +6 ms] Try correcting the operator to an existing operator, or defining a '[]' operator.
[ ] factory.first((json['integer'] as int)['value']));

A part of the generated code (dart):

switch (discriminator) {
      case 'integer':
        return blaBlaMyValue._(
            factory.first((json['integer'] as int)['value']));
      case 'double':
        return blaBlaMyValue._(
            factory.second((json['double'] as double)['value']));
      case 'pressureObservationValue':

When I manually remove ['value'] compilation is successful.

What am I doing wrong? Thanks.

@supervital supervital changed the title Union generation: The operator '[]' isn't defined for the class 'int' Unions generation: The operator '[]' isn't defined for the class 'int' Oct 4, 2022
@mbryzek
Copy link
Collaborator

mbryzek commented Oct 4, 2022

is it possible for you to add a discriminator field on your union type?

"unions": {
    "discriminator": "name of field you want",
    "myValue": {
      "types": [
      ]
    }
  }

and then try again? I'm not familiar with the dart generator so may need to contact the author of that code generator directly

@supervital
Copy link
Author

is it possible for you to add a discriminator field on your union type?

"unions": {
    "discriminator": "name of field you want",
    "myValue": {
      "types": [
      ]
    }
  }

and then try again? I'm not familiar with the dart generator so may need to contact the author of that code generator directly

Thanks for the reply.
I have just tried and it has no effect. And "discriminator" of the union, like this:

"unions": {
    "myValue": {
    "discriminator": "name of field you want",
      "types": [
      ]
    }
  }

@supervital
Copy link
Author

Looks like the dart generator doesn't work with primitive types in union. So, as a fix, I created a wrapper object for the primitive types:

{
   "unions":{
      "myValue":{
         "types":[
            {
               "type":"integerValue",
               "description":"Simple integer value."
            },
            {
               "type":"doubleValue",
               "description":"Simple double value."
            },
            {
               "type":"someCustomValue",
               "description":"A custom value."
            },
            {
               "type":"someCustomValue2",
               "description":"A custom value."
            }
         ]
      },
      "integerValue":{
         "description":"Object containing an integer.",
         "fields":[
            {
               "name":"value",
               "type":"integer"
            }
         ]
      },
      "doubleValue":{
         "description":"Object containing a double.",
         "fields":[
            {
               "name":"value",
               "type":"double"
            }
         ]
      }
   }
}

@mbryzek
Copy link
Collaborator

mbryzek commented Oct 4, 2022

thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants