feat: command validation
This commit is contained in:
@@ -5,9 +5,11 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/iliadenisov/galaxy/internal/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/iliadenisov/galaxy/internal/model/rest"
|
||||
)
|
||||
|
||||
@@ -43,27 +45,67 @@ func parseCommand(actor string, c json.RawMessage) (Command, error) {
|
||||
return nil, err
|
||||
}
|
||||
switch t := meta.Type; t {
|
||||
case rest.CommandTypeRaceQuit:
|
||||
return commandRaceQuit(actor)
|
||||
case rest.CommandTypeRaceVote:
|
||||
return giveVotes(actor, c)
|
||||
return commandRaceVote(actor, c)
|
||||
case rest.CommandTypeRaceRelation:
|
||||
return updateRelation(actor, c)
|
||||
return commandRaceRelation(actor, c)
|
||||
case rest.CommandTypeShipClassCreate:
|
||||
return commandShipClassCreate(actor, c)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown comman type: %s", t)
|
||||
}
|
||||
}
|
||||
|
||||
func giveVotes(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandVote
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error { return c.RaceVote(actor, v.Recipient) }, nil
|
||||
func commandRaceQuit(actor string) (Command, error) {
|
||||
return func(c controller.Ctrl) error { return c.RaceQuit(actor) }, nil
|
||||
}
|
||||
|
||||
func updateRelation(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandUpdateRelation
|
||||
func commandRaceVote(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandRaceVote
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error { return c.RaceRelation(actor, v.Opponent, v.Relation) }, nil
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceVote(actor, v.Recipient)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func commandRaceRelation(actor string, c json.RawMessage) (Command, error) {
|
||||
var v rest.CommandRaceRelation
|
||||
if err := json.Unmarshal(c, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.RaceRelation(actor, v.Opponent, v.Relation)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func commandShipClassCreate(actor string, c json.RawMessage) (Command, error) {
|
||||
v := new(rest.CommandShipClassCreate)
|
||||
if err := json.Unmarshal(c, v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateCommand(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(c controller.Ctrl) error {
|
||||
return c.ShipClassCreate(actor, v.Name, v.Drive, int(v.Armament), v.Weapons, v.Shields, v.Cargo)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func validateCommand(v any) error {
|
||||
if ve, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
||||
if err := ve.Struct(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user