dcbot/internal/twhelp/twhelp.go
Dawid Wysokiński eec0909c78
All checks were successful
continuous-integration/drone/push Build is passing
feat: notifications (#25)
Reviewed-on: #25
2022-10-27 09:56:40 +00:00

155 lines
3.0 KiB
Go

package twhelp
import (
"encoding/json"
"time"
)
type ErrorCode string
const (
ErrorCodeInternalServerError ErrorCode = "internal-server-error"
ErrorCodeEntityNotFound ErrorCode = "entity-not-found"
ErrorCodeValidationError ErrorCode = "validation-error"
ErrorCodeRouteNotFound ErrorCode = "route-not-found"
ErrorCodeMethodNotAllowed ErrorCode = "method-not-allowed"
)
func (c ErrorCode) String() string {
return string(c)
}
type APIError struct {
Code ErrorCode `json:"code"`
Message string `json:"message"`
}
func (e APIError) Error() string {
return e.Message + " (code=" + e.Code.String() + ")"
}
type errorResp struct {
Error APIError `json:"error"`
}
type Version struct {
Code string `json:"code"`
Host string `json:"host"`
Name string `json:"name"`
Timezone string `json:"timezone"`
}
type listVersionsResp struct {
Data []Version `json:"data"`
}
type Server struct {
Key string `json:"key"`
URL string `json:"url"`
Open bool `json:"open"`
}
type getServerResp struct {
Data Server `json:"data"`
}
type Tribe struct {
ID int64 `json:"id"`
Tag string `json:"tag"`
Name string `json:"name"`
ProfileURL string `json:"profileUrl"`
DeletedAt time.Time `json:"deletedAt"`
}
type getTribeResp struct {
Data Tribe `json:"data"`
}
type TribeMeta struct {
ID int64 `json:"id"`
Name string `json:"name"`
Tag string `json:"tag"`
ProfileURL string `json:"profileUrl"`
}
type NullTribeMeta struct {
Tribe TribeMeta
Valid bool
}
func (t NullTribeMeta) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return json.Marshal(t.Tribe)
}
func (t *NullTribeMeta) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
if err := json.Unmarshal(data, &t.Tribe); err != nil {
return err
}
t.Valid = true
return nil
}
type PlayerMeta struct {
ID int64 `json:"id"`
Name string `json:"name"`
ProfileURL string `json:"profileUrl"`
Tribe NullTribeMeta `json:"tribe"`
}
type NullPlayerMeta struct {
Player PlayerMeta
Valid bool
}
func (p NullPlayerMeta) MarshalJSON() ([]byte, error) {
if !p.Valid {
return []byte("null"), nil
}
return json.Marshal(p.Player)
}
func (p *NullPlayerMeta) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
if err := json.Unmarshal(data, &p.Player); err != nil {
return err
}
p.Valid = true
return nil
}
type VillageMeta struct {
ID int64 `json:"id"`
FullName string `json:"fullName"`
ProfileURL string `json:"profileUrl"`
}
type Ennoblement struct {
ID int64 `json:"id"`
Village VillageMeta `json:"village"`
NewOwner NullPlayerMeta `json:"newOwner"`
OldOwner NullPlayerMeta `json:"oldOwner"`
CreatedAt time.Time `json:"createdAt"`
}
type listEnnoblementsResp struct {
Data []Ennoblement `json:"data"`
}