anz-bank / sysl-go

Communication library used by SYSL-generated code written in Go.
Apache License 2.0
10 stars 14 forks source link

Update codegen to support Sysl union type. #198

Closed ericzhang6222 closed 4 years ago

ericzhang6222 commented 4 years ago

This is a simple and quick fixing as #95 is a blocker issue, now it can transfer Sysl union type too. For example, transform

!type Cat:
        id <: string 
        age <: int?:
            @json_tag = "age"
        hunts <: bool?:
            @json_tag = "hunts"

!type Dog:
        id <: string
        bark <: bool?:
            @json_tag = "bark"
        breed <: string?:
            @json_tag = "breed"

!union PetA:
        Cat
        Dog

to

// PetA can be one of following types in runtime:
// Cat
// Dog
type PetA interface {
    // isPetA is identifier method
    isPetA()
}

// isPetA identifies Cat is instance of PetA
func (i Cat) isPetA() {
}

// isPetA identifies Dog is instance of PetA
func (i Dog) isPetA() {
}

It can be enhanced still, like adding util method to transform instance of Pet to Cat or Dog.

ericzhang6222 commented 4 years ago

I see an issue lurking over here, as per my understanding, both Cat and Dog can behave as Pet during runtime as they are part of a union(both implement Pet interface {}). But essentially in the generated code the method set associated with Pet interface {} is empty, which means even a struct like type struct Empty {} can be a Pet. Please add a method to the Pet interface {} and implement the same in the structs Cat and Dog.

Fixed.