id.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package channelz
  19. import "fmt"
  20. // Identifier is an opaque identifier which uniquely identifies an entity in the
  21. // channelz database.
  22. type Identifier struct {
  23. typ RefChannelType
  24. id int64
  25. str string
  26. pid *Identifier
  27. }
  28. // Type returns the entity type corresponding to id.
  29. func (id *Identifier) Type() RefChannelType {
  30. return id.typ
  31. }
  32. // Int returns the integer identifier corresponding to id.
  33. func (id *Identifier) Int() int64 {
  34. return id.id
  35. }
  36. // String returns a string representation of the entity corresponding to id.
  37. //
  38. // This includes some information about the parent as well. Examples:
  39. // Top-level channel: [Channel #channel-number]
  40. // Nested channel: [Channel #parent-channel-number Channel #channel-number]
  41. // Sub channel: [Channel #parent-channel SubChannel #subchannel-number]
  42. func (id *Identifier) String() string {
  43. return id.str
  44. }
  45. // Equal returns true if other is the same as id.
  46. func (id *Identifier) Equal(other *Identifier) bool {
  47. if (id != nil) != (other != nil) {
  48. return false
  49. }
  50. if id == nil && other == nil {
  51. return true
  52. }
  53. return id.typ == other.typ && id.id == other.id && id.pid == other.pid
  54. }
  55. // NewIdentifierForTesting returns a new opaque identifier to be used only for
  56. // testing purposes.
  57. func NewIdentifierForTesting(typ RefChannelType, id int64, pid *Identifier) *Identifier {
  58. return newIdentifer(typ, id, pid)
  59. }
  60. func newIdentifer(typ RefChannelType, id int64, pid *Identifier) *Identifier {
  61. str := fmt.Sprintf("%s #%d", typ, id)
  62. if pid != nil {
  63. str = fmt.Sprintf("%s %s", pid, str)
  64. }
  65. return &Identifier{typ: typ, id: id, str: str, pid: pid}
  66. }