Converting service state to pointers
This commit is contained in:
@ -11,25 +11,25 @@ import (
|
||||
// The example methods log the requests and return zero values.
|
||||
type inventorysrvc struct {
|
||||
logger *log.Logger
|
||||
inventories map[string]*[]string //key = characterId, value = array of itemIds
|
||||
inventories *map[string]*[]string //key = characterId, value = array of itemIds
|
||||
}
|
||||
|
||||
// NewInventory returns the inventory service implementation.
|
||||
func NewInventory(logger *log.Logger) inventory.Service {
|
||||
inventoryMap := make(map[string]*[]string)
|
||||
return &inventorysrvc{logger, inventoryMap}
|
||||
return &inventorysrvc{logger, &inventoryMap}
|
||||
}
|
||||
|
||||
// AddItem implements addItem.
|
||||
func (s *inventorysrvc) AddItem(ctx context.Context, p *inventory.InventoryRecord) (err error) {
|
||||
s.logger.Print("inventory.addItem")
|
||||
|
||||
itemList := s.inventories[p.CharacterName]
|
||||
itemList := (*s.inventories)[p.CharacterName]
|
||||
if itemList == nil {
|
||||
itemList = s.initCharacterInventory(p.CharacterName)
|
||||
}
|
||||
newItemList := append(*itemList, p.ItemName)
|
||||
s.inventories[p.CharacterName] = &newItemList
|
||||
(*s.inventories)[p.CharacterName] = &newItemList
|
||||
|
||||
return
|
||||
}
|
||||
@ -38,7 +38,7 @@ func (s *inventorysrvc) AddItem(ctx context.Context, p *inventory.InventoryRecor
|
||||
func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRecord) (err error) {
|
||||
s.logger.Print("inventory.removeItem")
|
||||
|
||||
itemList := s.inventories[p.CharacterName]
|
||||
itemList := (*s.inventories)[p.CharacterName]
|
||||
if itemList == nil {
|
||||
s.logger.Printf("inventory for character with id %d not found", p.CharacterName)
|
||||
return errors.New("item not found")
|
||||
@ -47,7 +47,7 @@ func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRe
|
||||
idx := indexOf(p.ItemName, *itemList)
|
||||
if idx != -1 {
|
||||
newItemList := remove(*itemList, idx)
|
||||
s.inventories[p.CharacterName] = &newItemList
|
||||
(*s.inventories)[p.CharacterName] = &newItemList
|
||||
} else {
|
||||
s.logger.Printf("character with id %d does not have item %d in inventory", p.CharacterName, p.ItemName)
|
||||
return errors.New("item not found for character")
|
||||
@ -57,10 +57,10 @@ func (s *inventorysrvc) RemoveItem(ctx context.Context, p *inventory.InventoryRe
|
||||
}
|
||||
|
||||
func (s *inventorysrvc) ListInventory(ctx context.Context, payload *inventory.ListInventoryPayload) (res []string, err error) {
|
||||
if s.inventories[*payload.CharacterName] == nil {
|
||||
if (*s.inventories)[*payload.CharacterName] == nil {
|
||||
return nil, nil
|
||||
}
|
||||
res = *s.inventories[*payload.CharacterName]
|
||||
res = *((*s.inventories)[*payload.CharacterName])
|
||||
|
||||
return
|
||||
}
|
||||
@ -68,7 +68,7 @@ func (s *inventorysrvc) ListInventory(ctx context.Context, payload *inventory.Li
|
||||
func (s *inventorysrvc) initCharacterInventory(characterName string) (itemList *[]string) {
|
||||
list := make([]string, 0)
|
||||
itemList = &list
|
||||
s.inventories[characterName] = itemList
|
||||
(*s.inventories)[characterName] = itemList
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user