I have these JSON models:
{
"type": "signup",
"data": {username: "", "password": ""}
}
{
"type": "new_post",
"data": {"title": "", "description": ""}
}
type Event struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}
type Signup struct {
Username string `json:"username"`
Password string `json:"password"`
}
type NewPost struct {
Title string `json:"title"`
Description string `json:"description"`
}
Now I have these functions:
func HandleSignup(signup *Signup) {}
func HandleNewPost(newPost *NewPost) {}
What I need is a map to store these functions that receive different types, then have another function to act based on event type and decode the json.RawMessage to the respective`Type`and pass it to the respected handler.
I love reflection but the company doesn't :D
Just use type parameter ig
how about using a code generator like go-swagger?
I'll check it out, haven't worked with generators
Обсуждают сегодня