blank ident is no ident

This commit is contained in:
Josh Deprez 2021-09-16 10:34:54 +10:00
parent 60d5df92d0
commit 97c5939483
2 changed files with 15 additions and 13 deletions

View file

@ -330,12 +330,13 @@ func (g *Game) Register(component, parent interface{}) error {
func (g *Game) register(component, parent interface{}) error { func (g *Game) register(component, parent interface{}) error {
// register in g.byID if needed // register in g.byID if needed
if i, ok := component.(Identifier); ok { if i, ok := component.(Identifier); ok {
id := i.Ident() if id := i.Ident(); id != "" {
if _, exists := g.byID[id]; exists { if _, exists := g.byID[id]; exists {
return fmt.Errorf("duplicate id %q", id) return fmt.Errorf("duplicate id %q", id)
} }
g.byID[id] = i g.byID[id] = i
} }
}
// register in g.par // register in g.par
if parent != nil { if parent != nil {
@ -360,11 +361,11 @@ func (g *Game) register(component, parent interface{}) error {
} }
// TODO: better than O(len(path)^2) time and memory? // TODO: better than O(len(path)^2) time and memory?
for p := component; p != nil; p = g.par[p] { for p := component; p != nil; p = g.par[p] {
i, ok := p.(Identifier) id, ok := p.(Identifier)
if !ok { if !ok || id.Ident() == "" {
continue continue
} }
k := abKey{i.Ident(), b} k := abKey{id.Ident(), b}
if g.byAB[k] == nil { if g.byAB[k] == nil {
g.byAB[k] = make(map[interface{}]struct{}) g.byAB[k] = make(map[interface{}]struct{})
} }
@ -397,11 +398,11 @@ func (g *Game) unregister(component interface{}) {
continue continue
} }
for p := component; p != nil; p = g.par[p] { for p := component; p != nil; p = g.par[p] {
i, ok := p.(Identifier) id, ok := p.(Identifier)
if !ok { if !ok || id.Ident() == "" {
continue continue
} }
k := abKey{i.Ident(), b} k := abKey{id.Ident(), b}
if g.byAB[k] == nil { if g.byAB[k] == nil {
continue continue
} }
@ -421,8 +422,8 @@ func (g *Game) unregister(component interface{}) {
} }
// unregister from g.byID if needed // unregister from g.byID if needed
if i, ok := component.(Identifier); ok { if id, ok := component.(Identifier); ok && id.Ident() != "" {
delete(g.byID, i.Ident()) delete(g.byID, id.Ident())
} }
} }

View file

@ -84,7 +84,8 @@ type Hider interface {
} }
// Identifier components have a sense of self. This makes it easier for // Identifier components have a sense of self. This makes it easier for
// components to find and interact with one another. // components to find and interact with one another. Returning the empty string
// is treated as having no identifier.
type Identifier interface { type Identifier interface {
Ident() string Ident() string
} }