DEV Community

loading...

Discussion on: Something I've come to appreciate about dynamic typing

Collapse
theodesp profile image
Theofanis Despoudis

When you have a this.customer[field] which is an enum of values how can you apply polymorphism here? You will need more abstractions. What you can do is just don't accept the JSON as it is but implement a custom Parser:

jsonformatter.io/golang-json-parse...

So in the end the customer.email and the customer.phone would be custom fields and then you can do something like:

type field interface {
    Handle ()
}

type EmailField struct {
    s string
}

type PhoneField struct {
    p string
}

func (email *EmailField) Handle()  {
    // Code to callFunction or whatever
}

func (phone *PhoneField) Handle()  {
    // Code to callFunction or whatever
}

Just an idea.