golang.org/x/tools/gopls@v0.15.3/internal/cache/keys.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cache
     6  
     7  // session event tracing
     8  
     9  import (
    10  	"io"
    11  
    12  	"golang.org/x/tools/internal/event/label"
    13  )
    14  
    15  var (
    16  	KeyCreateSession   = NewSessionKey("create_session", "A new session was added")
    17  	KeyUpdateSession   = NewSessionKey("update_session", "Updated information about a session")
    18  	KeyShutdownSession = NewSessionKey("shutdown_session", "A session was shut down")
    19  )
    20  
    21  // SessionKey represents an event label key that has a *Session value.
    22  type SessionKey struct {
    23  	name        string
    24  	description string
    25  }
    26  
    27  // NewSessionKey creates a new Key for *Session values.
    28  func NewSessionKey(name, description string) *SessionKey {
    29  	return &SessionKey{name: name, description: description}
    30  }
    31  
    32  func (k *SessionKey) Name() string        { return k.name }
    33  func (k *SessionKey) Description() string { return k.description }
    34  
    35  func (k *SessionKey) Format(w io.Writer, buf []byte, l label.Label) {
    36  	io.WriteString(w, k.From(l).ID())
    37  }
    38  
    39  // Of creates a new Label with this key and the supplied session.
    40  func (k *SessionKey) Of(v *Session) label.Label { return label.OfValue(k, v) }
    41  
    42  // Get can be used to get the session for the key from a label.Map.
    43  func (k *SessionKey) Get(lm label.Map) *Session {
    44  	if t := lm.Find(k); t.Valid() {
    45  		return k.From(t)
    46  	}
    47  	return nil
    48  }
    49  
    50  // From can be used to get the session value from a Label.
    51  func (k *SessionKey) From(t label.Label) *Session {
    52  	err, _ := t.UnpackValue().(*Session)
    53  	return err
    54  }