finos / FDC3

An open standard for the financial desktop.
https://fdc3.finos.org
Other
202 stars 132 forks source link

Modify generate-type-predicates to only include interfaces that have … #1444

Open Roaders opened 4 days ago

Roaders commented 4 days ago

Describe your change

Modification of the generate-type-predicates code to build unions types of interfaces not based on the interface name but based on the type declared in the interface. If type extends the RequestMessageType union then the interface is included in the RequestMessage union

Related Issue

Contributor License Agreement

Review Checklist


THIS SOFTWARE IS CONTRIBUTED SUBJECT TO THE TERMS OF THE FINOS CORPORATE CONTRIBUTOR LICENSE AGREEMENT.

THIS SOFTWARE IS LICENSED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY WARRANTY OF NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THIS SOFTWARE MAY BE REDISTRIBUTED TO OTHERS ONLY BY EFFECTIVELY USING THIS OR ANOTHER EQUIVALENT DISCLAIMER IN ADDITION TO ANY OTHER REQUIRED LICENSE TERMS.

kriswest commented 4 days ago

@Roaders it looks like everything is being generated twice (two copies of each type constant and is* function). Also can we discuss what's being generated here and the use cases again?

I've found the is* typeguard functions useful, but I am concerned that through the use of the convert functions, they may be more expensive than necessary and will return false (I think) if there are any excess properties... If they are for typing rather than full validation they could just be based on checking the type fields...

The message unions (intended to help create discriminated unions https://github.com/finos/FDC3/pull/1387#issuecomment-2419628543) seem to serve a similar role, as I could either do:

public onMessage(message: RequestMessage): void{
    switch(message.type){
         case: "raiseIntentRequest":
                ...
                break;
        case "raiseIntentForContextRequest":
                ...
                break;
        default:
                ...
                console.log(`Unknown message type encountered: ` + message.type);
    }

or

//Edit: removed, was not correct

The former would (currently) be more efficient. Do we need both?

I'm also concerned about the type constants generated and whether encouraging use of them is a mistake, adding complexity to code without benefit... They obscure the underlying values in code while not providing the usual benefit of a constant (a single place to set and change the value). Use of the generated types for messages would already confirm that the type field values are correct... The main thing we need (that the quicktype generated code doesn't give us) is a consistent, clean way to determine the type of an incoming message - where I believe this code currently provides us two competing methods of doing so.

I am aware that @robmoffat has the opposite opinion on use of the constants in code. We need to bring this to ground to progress the fdc3-for-web implementation.

Finally, perhaps we should just replace the schema-generated group types (e.g. AppRequestMessage) with their discriminated union counter parts (i.e. RequestMessage) to avoid duplication?

Roaders commented 4 days ago

Have pushed some updates.

I still have to create the additional validated type predicate which won't take long in the morning. Do we want to remove the constants? I'm afraid I didn't understand @robmoffat explanation of what they were for.

Roaders commented 4 days ago

Hmmmm.... the diff is now huge... Could this just be because I deleted BrowserTypes and regenerated with the functions in a different order?

Roaders commented 3 days ago
public onMessage(message: AppRequestMessage): void{
    switch(message){
         case: isRaiseIntentRequest(message):
                ...
                break;
        case isRaiseIntentForContextRequest(message):
                ...
                break;
        default:
                ...
                console.log(`Unknown message type encountered: ` + message.type);
    }

@kriswest is the above valid typescript? I had never seen a switch statement like that (switching on the object then using type predicates for the cases). When I try it I get compile errors:

//this is fine
if (isAddIntentListenerRequest(message)) {
    return this.onAddIntentListenerRequest(message, sourceApp);
}

switch (message) {
    // this gives a compile error:
    // Type 'boolean' is not comparable to type 'AddContextListenerRequest | BroadcastRequest | CreatePrivateChannelRequest | FindInstancesRequest 
    case isAddEventListenerRequest(message):
        return this.onAddIntentListenerRequest(message, sourceApp);
}
Roaders commented 3 days ago

Added the valid type predicates and sped up the normal predicate. Added some progress messages (generation is a bit slow). Added comments to type predicates.

kriswest commented 3 days ago

@kriswest is the above valid typescript? I had never seen a switch statement like that (switching on the object then using type predicates for the cases). When I try it I get compile errors:

No its not valid - does narrow the type for you inside the cases, but doesn't make a correct comparison. My bad. A switch on message.type with the discriminated union and type strings in the cases should narrow the type inside the cases and check that the strings are valid - I'll test that later when back at my desk.

kriswest commented 3 days ago

Hmmmm.... the diff is now huge... Could this just be because I deleted BrowserTypes and regenerated with the functions in a different order?

Its not that bad, looks to me mostly like the replacement union types, the is functions and isValid functions. The new functions LGTM based on a quick glance.

Roaders commented 3 days ago

Hmmmm.... the diff is now huge... Could this just be because I deleted BrowserTypes and regenerated with the functions in a different order?

Its not that bad, looks to me mostly like the replacement union types, the is functions and isValid functions. The new functions LGTM based on a quick glance.

I undid my changes to the file and just ran the type predicates script on it rather than generating from fresh.

Roaders commented 13 hours ago

one of the checks is failing. what do I need to do to fixs that? is it test coverage?