session.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package mhayaProto
  2. import (
  3. cconst "github.com/mhaya/const"
  4. cstring "github.com/mhaya/extend/string"
  5. )
  6. func (x *Session) IsBind() bool {
  7. return x.Uid > 0
  8. }
  9. func (x *Session) ActorPath() string {
  10. return x.AgentPath + cconst.DOT + x.Sid
  11. }
  12. func (x *Session) Add(key string, value interface{}) {
  13. x.Data[key] = cstring.ToString(value)
  14. }
  15. func (x *Session) Remove(key string) {
  16. delete(x.Data, key)
  17. }
  18. func (x *Session) Set(key string, value string) {
  19. if key == "" || value == "" {
  20. return
  21. }
  22. x.Data[key] = value
  23. }
  24. func (x *Session) ImportAll(data map[string]string) {
  25. for k, v := range data {
  26. x.Set(k, v)
  27. }
  28. }
  29. func (x *Session) Contains(key string) bool {
  30. _, found := x.Data[key]
  31. return found
  32. }
  33. func (x *Session) Restore(data map[string]string) {
  34. x.Clear()
  35. for k, v := range data {
  36. x.Set(k, v)
  37. }
  38. }
  39. // Clear releases all settings related to current sc
  40. func (x *Session) Clear() {
  41. for k := range x.Data {
  42. delete(x.Data, k)
  43. }
  44. }
  45. func (x *Session) GetUint(key string) uint {
  46. v, ok := x.Data[key]
  47. if !ok {
  48. return 0
  49. }
  50. value, ok := cstring.ToUint(v)
  51. if !ok {
  52. return 0
  53. }
  54. return value
  55. }
  56. func (x *Session) GetInt(key string) int {
  57. v, ok := x.Data[key]
  58. if !ok {
  59. return 0
  60. }
  61. value, ok := cstring.ToInt(v)
  62. if !ok {
  63. return 0
  64. }
  65. return value
  66. }
  67. // GetInt32 returns the value associated with the key as a int32.
  68. func (x *Session) GetInt32(key string) int32 {
  69. v, ok := x.Data[key]
  70. if !ok {
  71. return 0
  72. }
  73. value, ok := cstring.ToInt32(v)
  74. if !ok {
  75. return 0
  76. }
  77. return value
  78. }
  79. // GetInt64 returns the value associated with the key as a int64.
  80. func (x *Session) GetInt64(key string) int64 {
  81. v, ok := x.Data[key]
  82. if !ok {
  83. return 0
  84. }
  85. value, ok := cstring.ToInt64(v)
  86. if !ok {
  87. return 0
  88. }
  89. return value
  90. }
  91. // GetString returns the value associated with the key as a string.
  92. func (x *Session) GetString(key string) string {
  93. v, ok := x.Data[key]
  94. if !ok {
  95. return ""
  96. }
  97. return v
  98. }