Do you think this is a reasonable way to model server endpoints? Basically I have a general endpoint, but want to specialize on payload. This seems a bit verbose to me, especially when accessing fields on the structs.
https://play.golang.org/p/VOWXI7RndCt
package main
import (
"fmt"
)
type Endpoint struct {
NeedsAuthentication bool
Uri string
}
type RefreshEndpoint struct {
Endpoint
Payload RefreshPayload
}
type RefreshPayload struct {
RefreshToken string
}
func main() {
refreshEndpoint := RefreshEndpoint{Endpoint: Endpoint{NeedsAuthentication: true, Uri: "/foo"}, Payload: RefreshPayload{RefreshToken: "xxx"}}
fmt.Printf("The uri is: %s, the access token is: %s", refreshEndpoint.Endpoint.Uri, refreshEndpoint.Payload.RefreshToken)
}
One way around the odd indirectness of refreshEndpoint.Endpoint.Uri
is to define a method called Uri
on RefreshEndpoint
:
package main
import (
"fmt"
)
type Endpoint struct {
NeedsAuthentication bool
Uri string
}
type RefreshEndpoint struct {
Endpoint
Payload RefreshPayload
}
type RefreshPayload struct {
RefreshToken string
}
func (e RefreshEndpoint) Uri() string {
return e.Endpoint.Uri
}
func main() {
refreshEndpoint := RefreshEndpoint{Endpoint: Endpoint{NeedsAuthentication: true, Uri: "/foo"}, Payload: RefreshPayload{RefreshToken: "xxx"}}
fmt.Printf("The uri is: %s, the access token is: %s", refreshEndpoint.Uri(), refreshEndpoint.Payload.RefreshToken)
}
That way we can access the uri like refreshEndpoint.Uri()
.
The issue remains though. How do I elegantly model a general Endpoint
that can take different kinds of payloads depending on the endpoint?