Let's say I must have this data structure:

├── Operations (struct)
│   ├── date_begining (uint)
│   └── date_end (uint)
│   └── responsible (address)
│   └── type **(?)**
│          ├── Observation (struct)
│          |     └── Production units (string)
│          |     └── Parcel (string)
|          OR
│          ├── ObservationPrague (struct)
│          |     └── Fon state (bool)
│          |     └── Trapped (string)
|          OR
│          ├── ObservationDesiese (struct)
│          |     └── Other var (bool)
│          |     └── Some Other var (uint)
|          OR
│          ├── Logistics (struct)
│          |     └── Other var1 (bool)
│          |     └── Some Other var1 (uint)

So As you can see, I need a struct of operations that always have 4 keys: date_begining, date_end, responsible and type. That ok until now. But now, my type should have one of the 4 following choices: Observation, ObservationPrague, ObservationDesiese or Logistics, that consequently have their own key/values too. So, what is the best way for me struct my "type". Is there a way I can say, "it's one of those four structs" in solidity lang? I thought of the enums, but concluded it couldn't be because then I wouldn't be able to have key/value pairs under the enum. Appreciate any help.

  • You should probably ask yourself (and tell us the answer when you know it) - why exactly is this ORing between the 4 structures? Do you have some state which determines which one to use at a given point? How exactly is the big structure being used in your code, that you need it to contain all 4 sub-structures in it? There's probably a good chance that you can design your system such that you will not need this cumbersome structure, but I'm concerned that at this point you are already poised to force this structure on your system rather than the other way round (i.e., the right way). – goodvibration Feb 5 '20 at 15:27
  • Indeed, you're right. Although, I haven't specified any code yet. Either way, I reorganised my thoughts and reverted the situation by creating a struct to each my ORing options and inside I have the common values to all of them. Thank you anyway :) – João Quintanilha Feb 5 '20 at 16:46

You cannot have an optional struct layout.

You can accomplish something similar with related structs.

└── Operations (struct)
    ├── date_begining (uint)
    └── date_end (uint)
    └── responsible (address)
    └── typeID (bytes32)
    └── typeSchema (refer to the class that applies)

Define the four types:

└── Observation (struct)
    └── Production units (string)
    └── Parcel (string)

└── ObservationPrague (struct)
     └── Fon state (bool)
     └── Trapped (string)

You will need a way to organize instances of the four types. Possibly over-simplified:

mapping(bytes32 => Observation) observations;
mapping(bytes32 => ObservationPrague) observationPragues;

A caveat here is this seems a little on the busy side and I would look for ways to simplify the on-chain design to avoid this if possible.

Hope it helps.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.