model.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package model
  2. import (
  3. "errors"
  4. )
  5. const (
  6. bit1 = int8(1)
  7. bit2 = int8(1) << 1
  8. // PubOnly pub only
  9. PubOnly = bit2 | int8(0)
  10. // SubOnly sub only
  11. SubOnly = int8(0) | bit1
  12. // PubSub pub and sub
  13. PubSub = bit2 | bit1
  14. )
  15. var (
  16. errGroup = errors.New("error group")
  17. errTopic = errors.New("error topic")
  18. errKey = errors.New("error key")
  19. errSecret = errors.New("error secret")
  20. )
  21. // Auth databus auth info accordance with table:bilibili_databus_v2.auth
  22. type Auth struct {
  23. Group string
  24. Topic string
  25. Operation int8
  26. Key string
  27. Secret string
  28. Batch int64
  29. Cluster string
  30. }
  31. // CanPub judge producer auth
  32. func (a *Auth) CanPub() bool {
  33. return a.Operation&bit2 == bit2
  34. }
  35. // CanSub judge consumer auth
  36. func (a *Auth) CanSub() bool {
  37. return a.Operation&bit1 == bit1
  38. }
  39. // Auth judge auth
  40. func (a *Auth) Auth(group, topic, key, secret string) error {
  41. if a.Group != group {
  42. return errGroup
  43. }
  44. if a.Topic != topic {
  45. return errTopic
  46. }
  47. if a.Key != key {
  48. return errKey
  49. }
  50. if a.Secret != secret {
  51. return errSecret
  52. }
  53. return nil
  54. }