key_map.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package maps
  2. import (
  3. "f1-game/internal/pb"
  4. cherryLogger "github.com/cherry-game/cherry/logger"
  5. "github.com/spf13/cast"
  6. )
  7. const (
  8. Int32 KeyType = 1
  9. Int64 KeyType = 2
  10. String KeyType = 3
  11. )
  12. type (
  13. // KeyType 属性类型
  14. KeyType int32
  15. // KeyMap key map
  16. KeyMap struct {
  17. maps pb.KeyMap
  18. }
  19. )
  20. func NewKeyMap() *KeyMap {
  21. return &KeyMap{
  22. maps: *NewPBKeyMap(),
  23. }
  24. }
  25. func NewPBKeyMap() *pb.KeyMap {
  26. return &pb.KeyMap{
  27. Int32Maps: make(map[int32]int32),
  28. Int64Maps: make(map[int32]int64),
  29. StringMaps: make(map[int32]string),
  30. }
  31. }
  32. func (p *KeyMap) Set(key int32, typ KeyType, value any) {
  33. switch typ {
  34. case Int32:
  35. {
  36. result, err := cast.ToInt32E(value)
  37. if err != nil {
  38. p.error(key, value, "Set->Int32", err)
  39. return
  40. }
  41. p.maps.Int32Maps[key] = result
  42. break
  43. }
  44. case Int64:
  45. {
  46. result, err := cast.ToInt64E(value)
  47. if err != nil {
  48. p.error(key, value, "Set->Int64", err)
  49. return
  50. }
  51. p.maps.Int64Maps[key] = result
  52. break
  53. }
  54. case String:
  55. {
  56. result, err := cast.ToStringE(value)
  57. if err != nil {
  58. p.error(key, value, "Set->String", err)
  59. return
  60. }
  61. p.maps.StringMaps[key] = result
  62. break
  63. }
  64. }
  65. }
  66. func (p *KeyMap) GetKeys(keys ...int32) *KeyMap {
  67. newKeyMap := NewKeyMap()
  68. for _, key := range keys {
  69. v32, found := p.maps.Int32Maps[key]
  70. if found {
  71. newKeyMap.maps.Int32Maps[key] = v32
  72. continue
  73. }
  74. v64, found := p.maps.Int64Maps[key]
  75. if found {
  76. newKeyMap.maps.Int64Maps[key] = v64
  77. continue
  78. }
  79. vString, found := p.maps.StringMaps[key]
  80. if found {
  81. newKeyMap.maps.StringMaps[key] = vString
  82. continue
  83. }
  84. }
  85. return newKeyMap
  86. }
  87. func (p *KeyMap) GetInt32(key int32) (int32, bool) {
  88. val, found := p.maps.Int32Maps[key]
  89. return val, found
  90. }
  91. func (p *KeyMap) GetInt64(key int32) (int64, bool) {
  92. val, found := p.maps.Int64Maps[key]
  93. return val, found
  94. }
  95. func (p *KeyMap) GetString(key int32) (string, bool) {
  96. val, found := p.maps.StringMaps[key]
  97. return val, found
  98. }
  99. func (p *KeyMap) Int32Maps() map[int32]int32 {
  100. return p.maps.GetInt32Maps()
  101. }
  102. func (p *KeyMap) Int64Maps() map[int32]int64 {
  103. return p.maps.GetInt64Maps()
  104. }
  105. func (p *KeyMap) StringMaps() map[int32]string {
  106. return p.maps.GetStringMaps()
  107. }
  108. func (*KeyMap) error(key int32, value any, info string, err error) {
  109. cherryLogger.Warnf("[key = %v,value = %v] %s fail = %v",
  110. key,
  111. value,
  112. info,
  113. err,
  114. )
  115. }
  116. func (p *KeyMap) ToProtobuf() *pb.KeyMap {
  117. return &p.maps
  118. }