gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

id.go (2216B)


      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 
     19 package channelz
     20 
     21 import "fmt"
     22 
     23 // Identifier is an opaque identifier which uniquely identifies an entity in the
     24 // channelz database.
     25 type Identifier struct {
     26 	typ RefChannelType
     27 	id  int64
     28 	str string
     29 	pid *Identifier
     30 }
     31 
     32 // Type returns the entity type corresponding to id.
     33 func (id *Identifier) Type() RefChannelType {
     34 	return id.typ
     35 }
     36 
     37 // Int returns the integer identifier corresponding to id.
     38 func (id *Identifier) Int() int64 {
     39 	return id.id
     40 }
     41 
     42 // String returns a string representation of the entity corresponding to id.
     43 //
     44 // This includes some information about the parent as well. Examples:
     45 // Top-level channel: [Channel #channel-number]
     46 // Nested channel:    [Channel #parent-channel-number Channel #channel-number]
     47 // Sub channel:       [Channel #parent-channel SubChannel #subchannel-number]
     48 func (id *Identifier) String() string {
     49 	return id.str
     50 }
     51 
     52 // Equal returns true if other is the same as id.
     53 func (id *Identifier) Equal(other *Identifier) bool {
     54 	if (id != nil) != (other != nil) {
     55 		return false
     56 	}
     57 	if id == nil && other == nil {
     58 		return true
     59 	}
     60 	return id.typ == other.typ && id.id == other.id && id.pid == other.pid
     61 }
     62 
     63 // NewIdentifierForTesting returns a new opaque identifier to be used only for
     64 // testing purposes.
     65 func NewIdentifierForTesting(typ RefChannelType, id int64, pid *Identifier) *Identifier {
     66 	return newIdentifer(typ, id, pid)
     67 }
     68 
     69 func newIdentifer(typ RefChannelType, id int64, pid *Identifier) *Identifier {
     70 	str := fmt.Sprintf("%s #%d", typ, id)
     71 	if pid != nil {
     72 		str = fmt.Sprintf("%s %s", pid, str)
     73 	}
     74 	return &Identifier{typ: typ, id: id, str: str, pid: pid}
     75 }