File: keys.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (54 lines) | stat: -rw-r--r-- 1,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cache

// session event tracing

import (
	"io"

	"cuelang.org/go/internal/golangorgx/tools/event/label"
)

var (
	KeyCreateSession   = NewSessionKey("create_session", "A new session was added")
	KeyUpdateSession   = NewSessionKey("update_session", "Updated information about a session")
	KeyShutdownSession = NewSessionKey("shutdown_session", "A session was shut down")
)

// SessionKey represents an event label key that has a *Session value.
type SessionKey struct {
	name        string
	description string
}

// NewSessionKey creates a new Key for *Session values.
func NewSessionKey(name, description string) *SessionKey {
	return &SessionKey{name: name, description: description}
}

func (k *SessionKey) Name() string        { return k.name }
func (k *SessionKey) Description() string { return k.description }

func (k *SessionKey) Format(w io.Writer, buf []byte, l label.Label) {
	io.WriteString(w, k.From(l).ID())
}

// Of creates a new Label with this key and the supplied session.
func (k *SessionKey) Of(v *Session) label.Label { return label.OfValue(k, v) }

// Get can be used to get the session for the key from a label.Map.
func (k *SessionKey) Get(lm label.Map) *Session {
	if t := lm.Find(k); t.Valid() {
		return k.From(t)
	}
	return nil
}

// From can be used to get the session value from a Label.
func (k *SessionKey) From(t label.Label) *Session {
	err, _ := t.UnpackValue().(*Session)
	return err
}