Skip to content

Commit 1a36444

Browse files
committed
core: make structs private, only expose interfaces
1 parent e675501 commit 1a36444

23 files changed

+211
-211
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Check out `debug_on.go` for any other settings that can be changed.
224224

225225
### Generating interfaces
226226

227-
We generate interfaces such as `IGameState` from structs to make it easier to keep docs in synch over structs and interfaces.
227+
We generate interfaces such as `GameState` from structs to make it easier to keep docs in synch over structs and interfaces.
228228
For this we use [@vburenin](https://github.com/vburenin)'s [`ifacemaker`](https://github.com/vburenin/ifacemaker) tool.
229229

230230
You can download the latest version [here](https://github.com/vburenin/ifacemaker/releases).

examples/mocking/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mocking the parser
22

3-
This example shows you how to use the provided [`fake` package](https://godoc.org/github.com/markus-wa/demoinfocs-golang/fake) to mock `demoinfocs.IParser` and other parts of the library.
3+
This example shows you how to use the provided [`fake` package](https://godoc.org/github.com/markus-wa/demoinfocs-golang/fake) to mock `demoinfocs.Parser` and other parts of the library.
44
That way you will be able to write useful unit tests for your application.
55

66
## System under test
@@ -13,14 +13,14 @@ import (
1313
events "github.com/markus-wa/demoinfocs-golang/v2/events"
1414
)
1515

16-
func collectKills(parser dem.IParser) (kills []events.Kill, err error) {
16+
func collectKills(parser dem.Parser) (kills []events.Kill, err error) {
1717
...
1818
}
1919
```
2020

2121
We deliberately ignore the implementation so we don't make assumptions about the code since it might change in the future.
2222

23-
As you can see `collectKills` takes an `IParser` as input and returns a slice of `events.Kill` and potentially an error.
23+
As you can see `collectKills` takes an `Parser` as input and returns a slice of `events.Kill` and potentially an error.
2424

2525
## Positive test case
2626

examples/mocking/collect_kills.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
events "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/events"
66
)
77

8-
func collectKills(parser demoinfocs.IParser) (kills []events.Kill, err error) {
8+
func collectKills(parser demoinfocs.Parser) (kills []events.Kill, err error) {
99
parser.RegisterEventHandler(func(kill events.Kill) {
1010
kills = append(kills, kill)
1111
})

pkg/demoinfocs/datatables.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
maxWeapons = 64
2424
)
2525

26-
func (p *Parser) mapEquipment() {
26+
func (p *parser) mapEquipment() {
2727
for _, sc := range p.stParser.ServerClasses() {
2828
switch sc.Name() {
2929
case "CC4":
@@ -69,7 +69,7 @@ func (p *Parser) mapEquipment() {
6969
}
7070

7171
// Bind the attributes of the various entities to our structs on the parser
72-
func (p *Parser) bindEntities() {
72+
func (p *parser) bindEntities() {
7373
p.bindTeamStates()
7474
p.bindBombSites()
7575
p.bindPlayers()
@@ -78,7 +78,7 @@ func (p *Parser) bindEntities() {
7878
p.bindGameRules()
7979
}
8080

81-
func (p *Parser) bindBomb() {
81+
func (p *parser) bindBomb() {
8282
bomb := &p.gameState.bomb
8383

8484
// Track bomb when it is dropped on the ground or being held by a player
@@ -115,7 +115,7 @@ func (p *Parser) bindBomb() {
115115
})
116116
}
117117

118-
func (p *Parser) bindTeamStates() {
118+
func (p *parser) bindTeamStates() {
119119
p.stParser.ServerClasses().FindByName("CCSTeam").OnEntityCreated(func(entity *st.Entity) {
120120
team := entity.PropertyValueMust("m_szTeamname").StringVal
121121

@@ -154,7 +154,7 @@ func (p *Parser) bindTeamStates() {
154154
})
155155
}
156156

157-
func (p *Parser) bindBombSites() {
157+
func (p *parser) bindBombSites() {
158158
p.stParser.ServerClasses().FindByName("CCSPlayerResource").OnEntityCreated(func(playerResource *st.Entity) {
159159
playerResource.BindProperty("m_bombsiteCenterA", &p.bombsiteA.center, st.ValTypeVector)
160160
playerResource.BindProperty("m_bombsiteCenterB", &p.bombsiteB.center, st.ValTypeVector)
@@ -169,7 +169,7 @@ func (p *Parser) bindBombSites() {
169169
})
170170
}
171171

172-
func (p *Parser) bindPlayers() {
172+
func (p *parser) bindPlayers() {
173173
p.stParser.ServerClasses().FindByName("CCSPlayer").OnEntityCreated(func(player *st.Entity) {
174174
p.bindNewPlayer(player)
175175
})
@@ -179,7 +179,7 @@ func (p *Parser) bindPlayers() {
179179
})
180180
}
181181

182-
func (p *Parser) getOrCreatePlayer(entityID int, rp *playerInfo) (isNew bool, player *common.Player) {
182+
func (p *parser) getOrCreatePlayer(entityID int, rp *playerInfo) (isNew bool, player *common.Player) {
183183
player = p.gameState.playersByEntityID[entityID]
184184

185185
if player == nil {
@@ -215,7 +215,7 @@ func (p *Parser) getOrCreatePlayer(entityID int, rp *playerInfo) (isNew bool, pl
215215
return isNew, player
216216
}
217217

218-
func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
218+
func (p *parser) bindNewPlayer(playerEntity st.IEntity) {
219219
entityID := playerEntity.ID()
220220
rp := p.rawPlayers[entityID-1]
221221

@@ -289,7 +289,7 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
289289
}
290290
}
291291

292-
func (p *Parser) bindPlayerWeapons(playerEntity st.IEntity, pl *common.Player) {
292+
func (p *parser) bindPlayerWeapons(playerEntity st.IEntity, pl *common.Player) {
293293
// Some demos have an additional prefix for player weapons weapon
294294
var wepPrefix string
295295
if playerEntity.Property(playerWeaponPrefix+"000") != nil {
@@ -339,7 +339,7 @@ func (p *Parser) bindPlayerWeapons(playerEntity st.IEntity, pl *common.Player) {
339339
}
340340
}
341341

342-
func (p *Parser) bindWeapons() {
342+
func (p *parser) bindWeapons() {
343343
for _, sc := range p.stParser.ServerClasses() {
344344
for _, bc := range sc.BaseClasses() {
345345
switch bc.Name() {
@@ -358,9 +358,9 @@ func (p *Parser) bindWeapons() {
358358
p.stParser.ServerClasses().FindByName("CInferno").OnEntityCreated(p.bindNewInferno)
359359
}
360360

361-
// bindGrenadeProjectiles keeps track of the location of live grenades (Parser.gameState.grenadeProjectiles), actively thrown by players.
361+
// bindGrenadeProjectiles keeps track of the location of live grenades (parser.gameState.grenadeProjectiles), actively thrown by players.
362362
// It does NOT track the location of grenades lying on the ground, i.e. that were dropped by dead players.
363-
func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
363+
func (p *parser) bindGrenadeProjectiles(entity *st.Entity) {
364364
entityID := entity.ID()
365365

366366
proj := common.NewGrenadeProjectile()
@@ -369,7 +369,7 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
369369

370370
var wep common.EquipmentType
371371
entity.OnCreateFinished(func() {
372-
// copy the weapon so it doesn't get overwritten by a new entity in Parser.weapons
372+
// copy the weapon so it doesn't get overwritten by a new entity in parser.weapons
373373
wepCopy := *(getPlayerWeapon(proj.Thrower, wep))
374374
proj.WeaponInstance = &wepCopy
375375

@@ -421,7 +421,7 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
421421
}
422422

423423
// Separate function because we also use it in round_officially_ended (issue #42)
424-
func (p *Parser) nadeProjectileDestroyed(proj *common.GrenadeProjectile) {
424+
func (p *parser) nadeProjectileDestroyed(proj *common.GrenadeProjectile) {
425425
// If the grenade projectile entity is destroyed AFTER round_officially_ended
426426
// we already executed this code when we received that event.
427427
if _, exists := p.gameState.grenadeProjectiles[proj.Entity.ID()]; !exists {
@@ -448,7 +448,7 @@ func (p *Parser) nadeProjectileDestroyed(proj *common.GrenadeProjectile) {
448448
}
449449
}
450450

451-
func (p *Parser) bindWeapon(entity *st.Entity, wepType common.EquipmentType) {
451+
func (p *parser) bindWeapon(entity *st.Entity, wepType common.EquipmentType) {
452452
entityID := entity.ID()
453453

454454
eq, eqExists := p.gameState.weapons[entityID]
@@ -502,7 +502,7 @@ func (p *Parser) bindWeapon(entity *st.Entity, wepType common.EquipmentType) {
502502
}
503503
}
504504

505-
func (p *Parser) bindNewInferno(entity *st.Entity) {
505+
func (p *parser) bindNewInferno(entity *st.Entity) {
506506
inf := common.NewInferno(p.demoInfoProvider, entity)
507507
p.gameState.infernos[entity.ID()] = inf
508508

@@ -518,7 +518,7 @@ func (p *Parser) bindNewInferno(entity *st.Entity) {
518518
}
519519

520520
// Separate function because we also use it in round_officially_ended (issue #42)
521-
func (p *Parser) infernoExpired(inf *common.Inferno) {
521+
func (p *parser) infernoExpired(inf *common.Inferno) {
522522
// If the inferno entity is destroyed AFTER round_officially_ended
523523
// we already executed this code when we received that event.
524524
if _, exists := p.gameState.infernos[inf.Entity.ID()]; !exists {
@@ -534,7 +534,7 @@ func (p *Parser) infernoExpired(inf *common.Inferno) {
534534
p.gameEventHandler.deleteThrownGrenade(inf.Thrower(), common.EqIncendiary)
535535
}
536536

537-
func (p *Parser) bindGameRules() {
537+
func (p *parser) bindGameRules() {
538538
grPrefix := func(s string) string {
539539
return fmt.Sprintf("%s.%s", gameRulesPrefix, s)
540540
}

pkg/demoinfocs/datatables_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func testPlayerSpotted(t *testing.T, propName string) {
115115
assert.Equal(t, expected, actual)
116116
}
117117

118-
func newParser() *Parser {
119-
p := NewParser(new(DevNullReader))
118+
func newParser() *parser {
119+
p := NewParser(new(DevNullReader)).(*parser)
120120
p.header = &common.DemoHeader{}
121121

122122
return p

pkg/demoinfocs/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Package demoinfocs provides a demo parser for the game Counter-Strike: Global Offensive.
33
It is based on the official demoinfogo tool by Valve as well as Stats Helix's demoinfo.
44
5-
A good entry point to using the library is the Parser type.
5+
A good entry point to using the library is the parser interface.
66
77
Demo events are documented in the events package.
88
*/

pkg/demoinfocs/fake/game_state.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,84 @@ import (
88
st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
99
)
1010

11-
var _ demoinfocs.IGameState = new(GameState)
11+
var _ demoinfocs.GameState = new(GameState)
1212

13-
// GameState is a mock for of demoinfocs.IGameState.
13+
// GameState is a mock for of demoinfocs.GameState.
1414
type GameState struct {
1515
mock.Mock
1616
}
1717

18-
// IngameTick is a mock-implementation of IGameState.IngameTick().
18+
// IngameTick is a mock-implementation of GameState.IngameTick().
1919
func (gs *GameState) IngameTick() int {
2020
return gs.Called().Int(0)
2121
}
2222

23-
// TeamCounterTerrorists is a mock-implementation of IGameState.TeamCounterTerrorists().
23+
// TeamCounterTerrorists is a mock-implementation of GameState.TeamCounterTerrorists().
2424
func (gs *GameState) TeamCounterTerrorists() *common.TeamState {
2525
return gs.Called().Get(0).(*common.TeamState)
2626
}
2727

28-
// TeamTerrorists is a mock-implementation of IGameState.TeamTerrorists().
28+
// TeamTerrorists is a mock-implementation of GameState.TeamTerrorists().
2929
func (gs *GameState) TeamTerrorists() *common.TeamState {
3030
return gs.Called().Get(0).(*common.TeamState)
3131
}
3232

33-
// Team is a mock-implementation of IGameState.Team().
33+
// Team is a mock-implementation of GameState.Team().
3434
func (gs *GameState) Team(team common.Team) *common.TeamState {
3535
return gs.Called().Get(0).(*common.TeamState)
3636
}
3737

38-
// Participants is a mock-implementation of IGameState.Participants().
39-
func (gs *GameState) Participants() demoinfocs.IParticipants {
40-
return gs.Called().Get(0).(demoinfocs.IParticipants)
38+
// Participants is a mock-implementation of GameState.Participants().
39+
func (gs *GameState) Participants() demoinfocs.Participants {
40+
return gs.Called().Get(0).(demoinfocs.Participants)
4141
}
4242

43-
// GrenadeProjectiles is a mock-implementation of IGameState.GrenadeProjectiles().
43+
// GrenadeProjectiles is a mock-implementation of GameState.GrenadeProjectiles().
4444
func (gs *GameState) GrenadeProjectiles() map[int]*common.GrenadeProjectile {
4545
return gs.Called().Get(0).(map[int]*common.GrenadeProjectile)
4646
}
4747

48-
// Infernos is a mock-implementation of IGameState.Infernos().
48+
// Infernos is a mock-implementation of GameState.Infernos().
4949
func (gs *GameState) Infernos() map[int]*common.Inferno {
5050
return gs.Called().Get(0).(map[int]*common.Inferno)
5151
}
5252

53-
// Weapons is a mock-implementation of IGameState.Weapons().
53+
// Weapons is a mock-implementation of GameState.Weapons().
5454
func (gs *GameState) Weapons() map[int]*common.Equipment {
5555
return gs.Called().Get(0).(map[int]*common.Equipment)
5656
}
5757

58-
// Entities is a mock-implementation of IGameState.Entities().
58+
// Entities is a mock-implementation of GameState.Entities().
5959
func (gs *GameState) Entities() map[int]*st.Entity {
6060
return gs.Called().Get(0).(map[int]*st.Entity)
6161
}
6262

63-
// Bomb is a mock-implementation of IGameState.Bomb().
63+
// Bomb is a mock-implementation of GameState.Bomb().
6464
func (gs *GameState) Bomb() *common.Bomb {
6565
return gs.Called().Get(0).(*common.Bomb)
6666
}
6767

68-
// TotalRoundsPlayed is a mock-implementation of IGameState.TotalRoundsPlayed().
68+
// TotalRoundsPlayed is a mock-implementation of GameState.TotalRoundsPlayed().
6969
func (gs *GameState) TotalRoundsPlayed() int {
7070
return gs.Called().Int(0)
7171
}
7272

73-
// GamePhase is a mock-implementation of IGameState.GamePhase().
73+
// GamePhase is a mock-implementation of GameState.GamePhase().
7474
func (gs *GameState) GamePhase() common.GamePhase {
7575
return gs.Called().Get(0).(common.GamePhase)
7676
}
7777

78-
// IsWarmupPeriod is a mock-implementation of IGameState.IsWarmupPeriod().
78+
// IsWarmupPeriod is a mock-implementation of GameState.IsWarmupPeriod().
7979
func (gs *GameState) IsWarmupPeriod() bool {
8080
return gs.Called().Bool(0)
8181
}
8282

83-
// IsMatchStarted is a mock-implementation of IGameState.IsMatchStarted().
83+
// IsMatchStarted is a mock-implementation of GameState.IsMatchStarted().
8484
func (gs *GameState) IsMatchStarted() bool {
8585
return gs.Called().Bool(0)
8686
}
8787

88-
// ConVars is a mock-implementation of IGameState.ConVars().
88+
// ConVars is a mock-implementation of GameState.ConVars().
8989
func (gs *GameState) ConVars() map[string]string {
9090
return gs.Called().Get(0).(map[string]string)
9191
}

0 commit comments

Comments
 (0)