accordproject / concerto

Business schema language and runtime
https://concerto.accordproject.org
Apache License 2.0
119 stars 105 forks source link

Extends support for enum declarations #849

Open dselman opened 5 months ago

dselman commented 5 months ago

Feature Request 🛍️

Add the ability to extend an existing enum declaration.

Use Case

namespace vehicle@1.0.0

enum VehicleStatus {
   o STARTED
   o STOPPED
}

concept Vehicle {
   o VehicleStatus status
}

And then...

namespace acme@1.0.0
import {VehicleStatus} from vehicle@1.0.0

enum AcmeVehicleStatus extends VehicleStatus {
   o IDLING
}

A valid instance (IFF the acme@1.0.0 namespace is imported into the model manager):

{
   "$class" : "vehicle@1.0.0.Vehicle",
   "status" : "IDLING"
}

Possible Solution

Allow enum declarations to be extended with new enum values. Make properties of concepts that are enums "polymorphic" in the JSON validator: searching all enum declarations that extend the field type, and taking the union of all their values.

Context

User wants to extend the values of an enum without incrementing the model version of the concept that is using the enum declaration.

Detailed Description

Note that this does complicate namespace resolution, as the transitive closure of all models required by a namespace is now no longer explicit, even in strict mode. Before validating incoming JSON the model manager must be explicitly populated with all the namespaces that extend an enum declaration.

mttrbrts commented 5 months ago

Are you proposing a Liskov substitution semantic, or is this just syntactic sugar for creating a new enum type that shares values with another type?

dselman commented 5 months ago

Closer to the former. The line

o VehicleStatus status

Is saying, "expect an enum value here, coming from the VehicleStatus enum declaration, or any enum declaration that extends VehicleStatus. We would have to add logic to enum declarations to check that they were not redeclaring enum values from a super type...

The implementation could be something like rebuilding EnumDeclaration on top of ConceptDeclaration so we share a lot of the logic for handling super types, decorators etc.

mttrbrts commented 5 months ago

I'm having some déja vu! https://github.com/accordproject/concerto/issues/291