I want to decode json to struct. My structs look like that:
type message struct {
Request baseRequest `json:"request"` //actually there should be other type here, but I can't think of what it could be
Auth auth `json:"auth"`
}
type baseRequest struct {
Foo string `json:"foo" validate:"required"`
}
type createRequest struct {
baseRequest
Bar string `json:"bar" validate:"required"`
}
type cancelRequest struct{
baseRequest
FooBar string `json:"foo_bar" validate:"required"`
}
I want to compose createRequest with baseRequest. All my code is revolving around passing message type in chain of responsibility pattern. I have implemented a handler, that creates a pointer to empty message struct, that is used by jsonReader() function. For /create path I want to use createRequest instead of baseRequest, and for /cancel path I want to use cancelRequest. So for example in:
func (factory *HandlerFactory) Create() http.Handler {
create := func() *message { return &message{} }
return factory.defaultChain(createNewMessage, nil)
}
I want to change the type of message.Request() to createRequest.
And in:
func (factory *HandlerFactory) Cancel() http.Handler {
create := func() *message { return &message{} }
return factory.defaultChain(createNewMessage, nil)
}
I want to change the type of message.Request to cancelRequest. How can I achieve something like that in Go?