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