Skip to content

Commit

Permalink
removed Try versions of node functions; doesn't make sense to have tw…
Browse files Browse the repository at this point in the history
…o ways to do the same thing
  • Loading branch information
kkoreilly committed Dec 26, 2023
1 parent f2cf7f1 commit 99332d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 203 deletions.
22 changes: 3 additions & 19 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package ki

import (
"fmt"
"log"
"log/slog"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -38,8 +38,8 @@ func InitNode(this Ki) {
// and inserted.
func ThisCheck(k Ki) error {
if k.This() == nil {
err := fmt.Errorf("Ki Node %v ThisCheck: node has null 'this' pointer -- must call Init or InitName on root nodes!", k.Name())
log.Println(err)
err := fmt.Errorf("ki.Node %v ThisCheck: node has null 'this' pointer; must call Init or InitName on root nodes", k.Name())
slog.Error(err.Error())
return err
}
return nil
Expand Down Expand Up @@ -147,29 +147,13 @@ func ParentByType[T Ki](k Ki, embeds bool) T {
return v
}

// ParentByTypeTry is a generic helper function for [Ki.ParentByTypeTry]
func ParentByTypeTry[T Ki](k Ki, embeds bool) (T, error) {
var n T
vi, err := k.ParentByTypeTry(n.KiType(), embeds)
v, _ := vi.(T)
return v, err
}

// ChildByType is a generic helper function for [Ki.ChildByType]
func ChildByType[T Ki](k Ki, embeds bool, startIdx ...int) T {
var n T
v, _ := k.ChildByType(n.KiType(), embeds, startIdx...).(T)
return v
}

// ChildByTypeTry is a generic helper function for [Ki.ChildByTypeTry]
func ChildByTypeTry[T Ki](k Ki, embeds bool, startIdx ...int) (T, error) {
var n T
vi, err := k.ChildByTypeTry(n.KiType(), embeds, startIdx...)
v, _ := vi.(T)
return v, err
}

// IsRoot tests if this node is the root node -- checks Parent = nil.
func IsRoot(k Ki) bool {
if k.This() == nil || k.Parent() == nil || k.Parent().This() == nil {
Expand Down
79 changes: 16 additions & 63 deletions ki.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,11 @@ type Ki interface {
// given name. Returns nil if not found.
ParentByName(name string) Ki

// ParentByNameTry finds first parent recursively up hierarchy that matches
// given name -- Try version returns error on failure.
ParentByNameTry(name string) (Ki, error)

// ParentByType finds parent recursively up hierarchy, by type, and
// returns nil if not found. If embeds is true, then it looks for any
// type that embeds the given type at any level of anonymous embedding.
ParentByType(t *gti.Type, embeds bool) Ki

// ParentByTypeTry finds parent recursively up hierarchy, by type, and
// returns error if not found. If embeds is true, then it looks for any
// type that embeds the given type at any level of anonymous embedding.
ParentByTypeTry(t *gti.Type, embeds bool) (Ki, error)

//////////////////////////////////////////////////////////////////////////
// Children

Expand All @@ -152,29 +143,17 @@ type Ki interface {
// method on parent node should be used to ensure proper init.
Children() *Slice

// Child returns the child at given index -- will panic if index is invalid.
// See methods on ki.Slice for more ways to access.
// Child returns the child at given index and returns nil if
// the index is out of range.
Child(idx int) Ki

// ChildTry returns the child at given index -- Try version returns
// error if index is invalid.
// See methods on ki.Slice for more ways to access.
ChildTry(idx int) (Ki, error)

// ChildByName returns the first element that has given name, and nil
// if no such element is found. startIdx arg allows for optimized
// bidirectional find if you have an idea where it might be, which
// can be a key speedup for large lists. If no value is specified for
// startIdx, it starts in the middle, which is a good default.
ChildByName(name string, startIdx ...int) Ki

// ChildByNameTry returns the first element that has given name, and an error
// if no such element is found. startIdx arg allows for optimized
// bidirectional find if you have an idea where it might be, which
// can be a key speedup for large lists. If no value is specified for
// startIdx, it starts in the middle, which is a good default.
ChildByNameTry(name string, startIdx ...int) (Ki, error)

// ChildByType returns the first element that has the given type, and nil
// if not found. If embeds is true, then it also looks for any type that
// embeds the given type at any level of anonymous embedding.
Expand All @@ -184,15 +163,6 @@ type Ki interface {
// good default.
ChildByType(t *gti.Type, embeds bool, startIdx ...int) Ki

// ChildByTypeTry returns the first element that has the given type, and an
// error if not found. If embeds is true, then it also looks for any type that
// embeds the given type at any level of anonymous embedding.
// startIdx arg allows for optimized bidirectional find if you have an
// idea where it might be, which can be a key speedup for large lists. If
// no value is specified for startIdx, it starts in the middle, which is a
// good default.
ChildByTypeTry(t *gti.Type, embeds bool, startIdx ...int) (Ki, error)

//////////////////////////////////////////////////////////////////////////
// Paths

Expand Down Expand Up @@ -224,17 +194,6 @@ type Ki interface {
// Returns nil if not found.
FindPath(path string) Ki

// FindPathTry returns Ki object at given path, starting from this node
// (e.g., the root). If this node is not the root, then the path
// to this node is subtracted from the start of the path if present there.
// FindPath only works correctly when names are unique.
// Path has node Names separated by / and fields by .
// Node names escape any existing / and . characters to \\ and \,
// There is also support for [idx] index-based access for any given path
// element, for cases when indexes are more useful than names.
// Returns error if not found.
FindPathTry(path string) (Ki, error)

// FieldByName returns Ki object that is a direct field.
// This must be implemented for any types that have Ki fields that
// are processed as part of the overall Ki tree. This is only used
Expand Down Expand Up @@ -316,20 +275,20 @@ type Ki interface {
//////////////////////////////////////////////////////////////////////////
// Deleting Children

// DeleteChildAtIndex deletes child at given index (returns error for
// invalid index).
// Wraps delete in UpdateStart / End and sets ChildDeleted flag.
DeleteChildAtIndex(idx int, destroy bool) error
// DeleteChildAtIndex deletes child at given index. It returns false
// if there is no child at the given index. Wraps delete in UpdateStart / End
// and sets ChildDeleted flag.
DeleteChildAtIndex(idx int, destroy bool) bool

// DeleteChild deletes child node, returning error if not found in
// Children.
// Wraps delete in UpdateStart / End and sets ChildDeleted flag.
DeleteChild(child Ki, destroy bool) error
// DeleteChild deletes the given child node, returning false if
// it can not find it. Wraps delete in UpdateStart / End and
// sets ChildDeleted flag.
DeleteChild(child Ki, destroy bool) bool

// DeleteChildByName deletes child node by name -- returns child, error
// if not found.
// Wraps delete in UpdateStart / End and sets ChildDeleted flag.
DeleteChildByName(name string, destroy bool) (Ki, error)
// DeleteChildByName deletes child node by name, returning false
// if it can not find it. Wraps delete in UpdateStart / End and
// sets ChildDeleted flag.
DeleteChildByName(name string, destroy bool) bool

// DeleteChildren deletes all children nodes -- destroy will add removed
// children to deleted list, to be destroyed later -- otherwise children
Expand Down Expand Up @@ -386,16 +345,10 @@ type Ki interface {
// map if nil.
SetProp(key string, val any)

// Prop returns property value for key that is known to exist.
// Returns nil if it actually doesn't -- this version allows
// direct conversion of return. See PropTry for version with
// error message if uncertain if property exists.
// Prop returns the property value for the given key.
// It returns nil if it doesn't exist.
Prop(key string) any

// PropTry returns property value for key. Returns error message
// if property with that key does not exist.
PropTry(key string) (any, error)

// PropInherit gets property value from key with options for inheriting
// property from parents. If inherit, then checks all parents.
// Returns false if not set anywhere.
Expand Down
Loading

0 comments on commit 99332d3

Please sign in to comment.