github.com/v2fly/tools@v0.100.0/internal/lsp/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 import ( 8 "io" 9 10 "github.com/v2fly/tools/internal/event/label" 11 ) 12 13 var ( 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. 20 type SessionKey struct { 21 name string 22 description string 23 } 24 25 // NewSessionKey creates a new Key for *Session values. 26 func NewSessionKey(name, description string) *SessionKey { 27 return &SessionKey{name: name, description: description} 28 } 29 30 func (k *SessionKey) Name() string { return k.name } 31 func (k *SessionKey) Description() string { return k.description } 32 33 func (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. 38 func (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. 41 func (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. 49 func (k *SessionKey) From(t label.Label) *Session { 50 err, _ := t.UnpackValue().(*Session) 51 return err 52 }