tree

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 1 Imported by: 0

README

Tree (树)

提供 PostgreSQL ltree 风格的标签树路径操作工具。

API

LTree

用于处理类似 "A.B.C" 的层级路径结构。

  • NewLTree
    • 签名: func NewLTree() *LTree
    • 描述: 创建 LTree 工具实例。
Methods
  • GetLTreeDepth
    • 签名: func (t *LTree) GetLTreeDepth(path string) int
    • 描述: 计算路径深度(即节点数量)。
  • BuildLTreePath
    • 签名: func (t *LTree) BuildLTreePath(parentPath, currentValue string) string
    • 描述: 构建子节点路径。
  • ExtractLastSegment
    • 签名: func (t *LTree) ExtractLastSegment(path string) string
    • 描述: 提取路径最后一段(当前节点名)。
  • IsDescendantPath
    • 签名: func (t *LTree) IsDescendantPath(descendantPath, ancestorPath string) bool
    • 描述: 判断是否为后代路径。
  • ReplacePathPrefix
    • 签名: func (t *LTree) ReplacePathPrefix(oldPath, newPath, fullPath string) string
    • 描述: 替换路径前缀(用于移动子树)。
  • ReplaceCurrentPath
    • 签名: func (t *LTree) ReplaceCurrentPath(oldPath, currentPath string) string
    • 描述: 重命名当前节点。

示例

import (
    "fmt"
    "github.com/fireflycore/go-utils/tree"
)

func main() {
    lt := tree.NewLTree()
    
    path := "Region.China.Beijing"
    fmt.Println(lt.GetLTreeDepth(path)) // 3
    fmt.Println(lt.ExtractLastSegment(path)) // Beijing
    
    newPath := lt.ReplacePathPrefix("Region.China", "Area.Asia", path)
    fmt.Println(newPath) // Area.Asia.Beijing
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LTree

type LTree struct{}

LTree 提供 PostgreSQL ltree 风格的标签树路径操作工具 用于处理类似 "A.B.C" 的层级路径结构

func NewLTree

func NewLTree() *LTree

NewLTree 创建一个新的 LTree 工具实例

func (*LTree) BuildLTreePath

func (t *LTree) BuildLTreePath(parentPath, currentValue string) string

BuildLTreePath 构建子节点路径 parentPath: 父节点路径 currentValue: 当前节点值 返回: "parentPath.currentValue",如果 parentPath 为空则返回 currentValue

func (*LTree) ExtractLastSegment

func (t *LTree) ExtractLastSegment(path string) string

ExtractLastSegment 提取路径的最后一段(即当前节点名) 例如: "A.B.C" -> "C"

func (*LTree) GetLTreeDepth

func (t *LTree) GetLTreeDepth(path string) int

GetLTreeDepth 计算标签树路径的深度 深度定义为路径中层级的数量(即点的数量 + 1) 例如: "A.B.C" 的深度为 3

func (*LTree) IsDescendantPath

func (t *LTree) IsDescendantPath(descendantPath, ancestorPath string) bool

IsDescendantPath 检查路径是否是后代路径

func (*LTree) ReplaceCurrentPath

func (t *LTree) ReplaceCurrentPath(oldPath, currentPath string) string

ReplaceCurrentPath 替换当前路径

func (*LTree) ReplacePathPrefix

func (t *LTree) ReplacePathPrefix(oldPath, newPath, fullPath string) string

ReplacePathPrefix 替换路径前缀,常用于移动子树 oldPath: 旧的前缀(如 "A.B") newPath: 新的前缀(如 "X.Y") fullPath: 包含旧前缀的完整路径(如 "A.B.C.D") 返回: 替换后的路径(如 "X.Y.C.D")。如果 fullPath 不以 oldPath 开头,则原样返回

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL