Skip to content

Commit baa7c76

Browse files
author
Zbynek Novotny
committed
Work on simplifying unmarshaling
I want to leverage the JSON tags of the structures to replace the heavy-handed code that reads from a map of unmarshaled data
1 parent 1e5ac9a commit baa7c76

File tree

17 files changed

+158
-79
lines changed

17 files changed

+158
-79
lines changed

gameclient/client.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[connection]
2-
ServerURL=claytestgameserver.azurewebsites.net
3-
#ServerURL=localhost:8000
2+
#ServerURL=claytestgameserver.azurewebsites.net
3+
ServerURL=localhost:8000
44
ConnectionTimeout=10

gameclient/client/leaderboard/ctf.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type CTFLeaderboard []CTFLeaderboardRecord
1919

2020
// UnmarshalCTFLeaderboard unmarshals a map of elements
2121
// into an instance of the CTFLeaderboardRecord
22-
func UnmarshalCTFLeaderboard(in []map[string]interface{}) (*CTFLeaderboard, error) {
22+
/*func UnmarshalCTFLeaderboard(in []map[string]interface{}) (*CTFLeaderboard, error) {
2323
retval := CTFLeaderboard{}
2424
2525
for _, lbRecIf := range in {
@@ -65,3 +65,4 @@ func unmarshalCTFLeaderboardRecord(in map[string]interface{}) (CTFLeaderboardRec
6565
Captures: captures,
6666
}, nil
6767
}
68+
*/

gameclient/client/leaderboard/dm.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type DMLeaderboard []DMLeaderboardRecord
1919

2020
// UnmarshalDMLeaderboard unmarshals a map of elements
2121
// into an instance of the DMLeaderboardRecord
22-
func UnmarshalDMLeaderboard(in []map[string]interface{}) (*DMLeaderboard, error) {
22+
/*func UnmarshalDMLeaderboard(in []map[string]interface{}) (*DMLeaderboard, error) {
2323
retval := DMLeaderboard{}
2424
2525
for _, lbRecIf := range in {
@@ -59,3 +59,4 @@ func unmarshalDMLeaderboardRecord(in map[string]interface{}) (DMLeaderboardRecor
5959
Deaths: deaths,
6060
}, nil
6161
}
62+
*/

gameclient/client/leaderboard/duel.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type DuelLeaderboard []DuelLeaderboardRecord
1919

2020
// UnmarshalDuelLeaderboard unmarshals a map of elements
2121
// into an instance of the DuelLeaderboardRecord
22-
func UnmarshalDuelLeaderboard(in []map[string]interface{}) (*DuelLeaderboard, error) {
22+
/*func UnmarshalDuelLeaderboard(in []map[string]interface{}) (*DuelLeaderboard, error) {
2323
retval := DuelLeaderboard{}
2424
2525
for _, lbRecIf := range in {
@@ -65,3 +65,4 @@ func unmarshalDuelLeaderboardRecord(in map[string]interface{}) (DuelLeaderboardR
6565
Wins: wins,
6666
}, nil
6767
}
68+
*/

gameclient/client/leaderboard/lms.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package leaderboard
22

33
import (
44
"github.com/mrclayman/rest-and-go/gameclient/client/player"
5-
"github.com/mrclayman/rest-and-go/gameclient/client/shared"
65
)
76

87
// LMSLeaderboardRecord contains information
@@ -19,7 +18,7 @@ type LMSLeaderboard []LMSLeaderboardRecord
1918

2019
// UnmarshalLMSLeaderboard unmarshals a map of elements
2120
// into an instance of the LMSLeaderboardRecord
22-
func UnmarshalLMSLeaderboard(in []map[string]interface{}) (*LMSLeaderboard, error) {
21+
/*func UnmarshalLMSLeaderboard(in []map[string]interface{}) (*LMSLeaderboard, error) {
2322
retval := LMSLeaderboard{}
2423
2524
for _, lbRecIf := range in {
@@ -65,3 +64,4 @@ func unmarshalLMSLeaderboardRecord(in map[string]interface{}) (LMSLeaderboardRec
6564
Wins: wins,
6665
}, nil
6766
}
67+
*/

gameclient/client/list_match.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111
// ListMatches queries the server using player's credentials
1212
// and prints out to stdout the list of active matches
1313
func ListMatches(c *http.Client, ps net.PlayerSession) (*match.Matchlist, error) {
14-
var matches match.Matchlist
14+
var mu match.MatchlistUnmarshaler
1515

16-
if err := net.Get(c, "/matches", ps, &matches); err != nil {
16+
if err := net.Get(c, "/matches", ps, &mu); err != nil {
1717
return nil, err
1818
}
1919

20-
listDMMatches(&matches.DM)
21-
listCTFMatches(&matches.CTF)
22-
listLMSMatches(&matches.LMS)
23-
listDuelMatches(&matches.Duel)
24-
return &matches, nil
20+
listDMMatches(&mu.Matchlist.DM)
21+
listCTFMatches(&mu.Matchlist.CTF)
22+
listLMSMatches(&mu.Matchlist.LMS)
23+
listDuelMatches(&mu.Matchlist.Duel)
24+
return &mu.Matchlist, nil
2525
}
2626

2727
func listDMMatches(ml *match.DMMatches) {

gameclient/client/match/ctf.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package match
22

33
import (
4-
"errors"
5-
64
"github.com/mrclayman/rest-and-go/gameclient/client/player"
7-
"github.com/mrclayman/rest-and-go/gameclient/client/shared"
85
)
96

107
// CTFPlayerRank defines the structure of a player's
@@ -18,7 +15,7 @@ type CTFPlayerRank struct {
1815

1916
// CTFPlayerRanks is a slice of CTF game type
2017
// player rank objects
21-
type CTFPlayerRanks []CTFPlayerRank
18+
type CTFPlayerRanks map[player.ID]CTFPlayerRank
2219

2320
// CTFMatch contains information on
2421
// a CTF type match
@@ -32,7 +29,7 @@ type CTFMatches []*CTFMatch
3229

3330
// unmarshalCTFMatch unmarshals the contents of the
3431
// input map into an instance of CTFMatch
35-
func unmarshalCTFMatch(in map[string]interface{}) (*CTFMatch, error) {
32+
/*func unmarshalCTFMatch(in map[string]interface{}) (*CTFMatch, error) {
3633
mID, err := IDFromMap(in)
3734
if err != nil {
3835
return nil, err
@@ -99,3 +96,4 @@ func unmarshalCTFRankRecord(in map[string]interface{}) (CTFPlayerRank, error) {
9996
Captures: captures,
10097
}, nil
10198
}
99+
*/

gameclient/client/match/dm.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package match
22

33
import (
4-
"errors"
5-
64
"github.com/mrclayman/rest-and-go/gameclient/client/player"
7-
"github.com/mrclayman/rest-and-go/gameclient/client/shared"
85
)
96

107
// DMPlayerRank aggregates information on a player's
@@ -16,7 +13,7 @@ type DMPlayerRank struct {
1613
}
1714

1815
// DMPlayerRanks defines a list of players' ranks
19-
type DMPlayerRanks []DMPlayerRank
16+
type DMPlayerRanks map[player.ID]DMPlayerRank
2017

2118
// DMMatch contains information on a
2219
// DeathMatch type match
@@ -30,7 +27,7 @@ type DMMatches []*DMMatch
3027

3128
// unmarshalDMMatch unmarshals the contents of the
3229
// input map into an instance of DMMatch
33-
func unmarshalDMMatch(in map[string]interface{}) (*DMMatch, error) {
30+
/*func unmarshalDMMatch(in map[string]interface{}) (*DMMatch, error) {
3431
mID, err := IDFromMap(in)
3532
if err != nil {
3633
return nil, err
@@ -90,3 +87,4 @@ func unmarshalDMRankRecord(in map[string]interface{}) (DMPlayerRank, error) {
9087
Deaths: deaths,
9188
}, nil
9289
}
90+
*/

gameclient/client/match/duel.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package match
22

33
import (
4-
"errors"
5-
64
"github.com/mrclayman/rest-and-go/gameclient/client/player"
7-
"github.com/mrclayman/rest-and-go/gameclient/client/shared"
85
)
96

107
// DuelPlayerRank defines the structure of
@@ -17,7 +14,7 @@ type DuelPlayerRank struct {
1714

1815
// DuelPlayerRanks defines the type for a slice
1916
// of Duel game type player rank objects
20-
type DuelPlayerRanks []DuelPlayerRank
17+
type DuelPlayerRanks map[player.ID]DuelPlayerRank
2118

2219
// DuelMatch contains information on
2320
// a Duel type match
@@ -31,7 +28,7 @@ type DuelMatches []*DuelMatch
3128

3229
// unmarshalDuelMatch unmarshals the contents of the
3330
// input map into an instance of DuelMatch
34-
func unmarshalDuelMatch(in map[string]interface{}) (*DuelMatch, error) {
31+
/*func unmarshalDuelMatch(in map[string]interface{}) (*DuelMatch, error) {
3532
mID, err := IDFromMap(in)
3633
if err != nil {
3734
return nil, err
@@ -91,3 +88,4 @@ func unmarshalDuelRankRecord(in map[string]interface{}) (DuelPlayerRank, error)
9188
Deaths: deaths,
9289
}, nil
9390
}
91+
*/

gameclient/client/match/lms.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package match
22

33
import (
4-
"errors"
5-
64
"github.com/mrclayman/rest-and-go/gameclient/client/player"
7-
"github.com/mrclayman/rest-and-go/gameclient/client/shared"
85
)
96

107
// LMSPlayerRank defines the structure of a player's
@@ -17,7 +14,7 @@ type LMSPlayerRank struct {
1714

1815
// LMSPlayerRanks is a type for a slice of
1916
// LMS game type player rank objects
20-
type LMSPlayerRanks []LMSPlayerRank
17+
type LMSPlayerRanks map[player.ID]LMSPlayerRank
2118

2219
// LMSMatch contains information on
2320
// a LMS type match
@@ -31,7 +28,7 @@ type LMSMatches []*LMSMatch
3128

3229
// unmarshalLMSMatch unmarshals the contents of the
3330
// input map into an instance of LMSMatch
34-
func unmarshalLMSMatch(in map[string]interface{}) (*LMSMatch, error) {
31+
/*func unmarshalLMSMatch(in map[string]interface{}) (*LMSMatch, error) {
3532
mID, err := IDFromMap(in)
3633
if err != nil {
3734
return nil, err
@@ -91,3 +88,4 @@ func unmarshalLMSRankRecord(in map[string]interface{}) (LMSPlayerRank, error) {
9188
Deaths: deaths,
9289
}, nil
9390
}
91+
*/

gameclient/client/match/match.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package match
22

3-
import (
4-
"encoding/json"
5-
"errors"
6-
"reflect"
7-
"strconv"
8-
)
9-
103
// Number defines the type that holds
114
// the value of the number of the match
125
type Number uint64
@@ -25,7 +18,7 @@ type ID struct {
2518
// IDFromMap retrieves an element with key "match_id"
2619
// from the map in the argument and verifies that the
2720
// element's type is an uint64.
28-
func IDFromMap(m map[string]interface{}) (uint64, error) {
21+
/*func IDFromMap(m map[string]interface{}) (uint64, error) {
2922
if v, ok := m["match_id"]; !ok {
3023
return 0, errors.New("Match does not seem to have 'match_id' key")
3124
} else if IDNum, ok := v.(json.Number); !ok {
@@ -40,4 +33,4 @@ func IDFromMap(m map[string]interface{}) (uint64, error) {
4033
}
4134
return ID, nil
4235
}
43-
}
36+
}*/

gameclient/client/match/matchlist.go

+107-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,118 @@
11
package match
22

33
import (
4-
"errors"
5-
"reflect"
6-
7-
"github.com/mrclayman/rest-and-go/gameclient/client/shared"
4+
"bytes"
5+
"encoding/json"
86
)
97

108
// Matchlist defines a structure that holds
119
// lists of existing matches for all game types
1210
type Matchlist struct {
13-
DM DMMatches
14-
CTF CTFMatches
15-
LMS LMSMatches
16-
Duel DuelMatches
11+
DM DMMatches `json:"dm"`
12+
CTF CTFMatches `json:"ctf"`
13+
LMS LMSMatches `json:"lms"`
14+
Duel DuelMatches `json:"duel"`
15+
}
16+
17+
// MatchlistUnmarshaler unmarshals the contents
18+
// of the Matchlist structure from JSON byte slice
19+
type MatchlistUnmarshaler struct {
20+
Matchlist Matchlist
1721
}
1822

23+
// UnmarshalJSON unmarshals the contents of a server's
24+
// response into the internal structure of match lists
25+
func (m *MatchlistUnmarshaler) UnmarshalJSON(in []byte) error {
26+
d := json.NewDecoder(bytes.NewReader(in))
27+
//d.UseNumber()
28+
29+
d.Decode(&m.Matchlist)
30+
return nil
31+
// Read opening curly brace
32+
/* var t json.Token
33+
var err error
34+
if t, err = d.Token(); err != nil {
35+
return err
36+
}
37+
38+
for d.More() {
39+
var gt string
40+
var ok bool
41+
42+
// Get the game type identifier
43+
t, err = d.Token()
44+
gt, ok = t.(string)
45+
if !ok {
46+
return errors.New("Token not a string (game type)")
47+
}
48+
fmt.Printf("Processing match list for type %v\n", gt)
49+
50+
if err = d.Decode(&gt); err != nil {
51+
fmt.Println("Failed to read game type identifier")
52+
return err
53+
}
54+
55+
switch gt {
56+
case DeathMatch:
57+
err = m.unmarshalDMMatchlist(d)
58+
case CaptureTheFlag:
59+
err = m.unmarshalCTFMatchlist(d)
60+
case LastManStanding:
61+
err = m.unmarshalLMSMatchlist(d)
62+
case Duel:
63+
err = m.unmarshalDuelMatchlist(d)
64+
}
65+
66+
if err != nil {
67+
return err
68+
}
69+
}
70+
71+
return nil
72+
*/
73+
}
74+
75+
func (m *Matchlist) unmarshalDMMatchlist(d *json.Decoder) error {
76+
matches := make(DMMatches, 0, 5)
77+
if err := d.Decode(&matches); err != nil {
78+
return err
79+
}
80+
81+
m.DM = matches
82+
return nil
83+
}
84+
85+
func (m *Matchlist) unmarshalCTFMatchlist(d *json.Decoder) error {
86+
matches := make(CTFMatches, 0, 5)
87+
if err := d.Decode(&matches); err != nil {
88+
return err
89+
}
90+
91+
m.CTF = matches
92+
return nil
93+
}
94+
95+
func (m *Matchlist) unmarshalLMSMatchlist(d *json.Decoder) error {
96+
matches := make(LMSMatches, 0, 5)
97+
if err := d.Decode(&matches); err != nil {
98+
return err
99+
}
100+
101+
m.LMS = matches
102+
return nil
103+
}
104+
105+
func (m *Matchlist) unmarshalDuelMatchlist(d *json.Decoder) error {
106+
matches := make(DuelMatches, 0, 5)
107+
if err := d.Decode(&matches); err != nil {
108+
return err
109+
}
110+
111+
m.Duel = matches
112+
return nil
113+
}
114+
115+
/*
19116
// UnmarshalJSON unmarshals input byteslice into
20117
// a map structure, from which individual match
21118
// type instances are synthesized
@@ -60,6 +157,7 @@ func (m *Matchlist) UnmarshalJSON(in []byte) error {
60157
return nil
61158
}
62159
160+
63161
func (m *Matchlist) unmarshalDMMatches(in []interface{}) error {
64162
for _, mMapIf := range in {
65163
mMap, ok := mMapIf.(map[string]interface{})
@@ -123,3 +221,4 @@ func (m *Matchlist) unmarshalDuelMatches(in []interface{}) error {
123221
}
124222
return nil
125223
}
224+
*/

0 commit comments

Comments
 (0)