gitstack

grindlemire/go-tui code browser

939 B Go 29 lines 2026-06-12 · bb82b57 raw
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
package tui

import (
	"fmt"
	"reflect"
)

// mountKeyNode chains an outer key with one loop level's key value. Distinct
// (site, parts...) inputs never compare equal, so call sites cannot collide.
type mountKeyNode struct {
	parent any
	part   any
}

// MountKey builds a comparable cache key for a mounted component from its
// generated call-site id and the enclosing loops' key values, outermost
// first. Called by generated code. Parts must be comparable values (ints,
// strings, comparable structs, pointers); MountKey panics on a
// non-comparable part such as a slice or map.
func MountKey(site int, parts ...any) any {
	var key any = site
	for _, part := range parts {
		if part != nil && !reflect.TypeOf(part).Comparable() {
			panic(fmt.Sprintf("tui.MountKey: key part of type %T is not comparable; use an int, string, pointer, or comparable struct", part))
		}
		key = mountKeyNode{parent: key, part: part}
	}
	return key
}