Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

rotree source pure

Package rotree provides a read-only wrapper for avl.Tree with safe value transformation.

Overview

Package rotree provides a read-only wrapper for avl.Tree with safe value transformation.

It is useful when you want to expose a read-only view of a tree while ensuring that the sensitive data cannot be modified.

Example:

Example
 1// Define a user structure with sensitive data
 2type User struct {
 3	Name     string
 4	Balance  int
 5	Internal string // sensitive field
 6}
 7
 8// Create and populate the original tree
 9privateTree := avl.NewTree()
10privateTree.Set("alice", &User{
11	Name:     "Alice",
12	Balance:  100,
13	Internal: "sensitive",
14})
15
16// Create a safe transformation function that copies the struct
17// while excluding sensitive data
18makeEntrySafeFn := func(v any) any {
19	u := v.(*User)
20	return &User{
21		Name:     u.Name,
22		Balance:  u.Balance,
23		Internal: "", // omit sensitive data
24	}
25}
26
27// Create a read-only view of the tree
28PublicTree := rotree.Wrap(tree, makeEntrySafeFn)
29
30// Safely access the data
31value := roTree.Get("alice")
32user := value.(*User)
33// user.Name == "Alice"
34// user.Balance == 100
35// user.Internal == "" (sensitive data is filtered)

Functions 1

func Wrap

1func Wrap(tree *avl.Tree, makeEntrySafeFn func(any) any) *ReadOnlyTree
source

Wrap creates a new ReadOnlyTree from an existing avl.Tree and a safety transformation function. If makeEntrySafeFn is nil, values will be returned as-is without transformation.

makeEntrySafeFn is a function that transforms a tree entry into a safe version that can be exposed to external users. This function should be implemented based on the specific safety requirements of your use case:

  1. No-op transformation: For primitive types (int, string, etc.) or already safe objects, simply pass nil as the makeEntrySafeFn to return values as-is.

  2. Defensive copying: For mutable types like slices or maps, you should create a deep copy to prevent modification of the original data. Example: func(v any) any { return append([]int{}, v.([]int)...) }

  3. Read-only wrapper: Return a read-only version of the object that implements a limited interface. Example: func(v any) any { return NewReadOnlyObject(v) }

  4. DAO transformation: Transform the object into a data access object that controls how the underlying data can be accessed. Example: func(v any) any { return NewDAO(v) }

The function ensures that the returned object is safe to expose to untrusted code, preventing unauthorized modifications to the original data structure.

Types 2

type IReadOnlyTree

interface
 1type IReadOnlyTree interface {
 2	Size() int
 3	Has(key string) bool
 4	Get(key string) any
 5	GetByIndex(index int) (string, any)
 6	Iterate(start, end string, cb avl.IterCbFn) bool
 7	ReverseIterate(start, end string, cb avl.IterCbFn) bool
 8	IterateByOffset(offset int, count int, cb avl.IterCbFn) bool
 9	ReverseIterateByOffset(offset int, count int, cb avl.IterCbFn) bool
10}
source

IReadOnlyTree defines the read-only operations available on a tree.

type ReadOnlyTree

struct
1type ReadOnlyTree struct {
2	tree            *avl.Tree
3	makeEntrySafeFn func(any) any
4}
source

ReadOnlyTree wraps an avl.Tree and provides read-only access.

Methods on ReadOnlyTree

func Get

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Get(key string) any
source

Get retrieves the value associated with the given key, converted to a safe format. It returns the value if the key exists, or nil if it doesn't. Note that a key stored with a nil value is indistinguishable from an absent key; use Has to check for existence.

func GetByIndex

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) GetByIndex(index int) (string, any)
source

GetByIndex retrieves the key-value pair at the specified index in the tree, with the value converted to a safe format.

func Has

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Has(key string) bool
source

Has checks whether a key exists in the tree.

func Iterate

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Iterate(start, end string, cb avl.IterCbFn) bool
source

Iterate performs an in-order traversal of the tree within the specified key range.

func IterateByOffset

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) IterateByOffset(offset int, count int, cb avl.IterCbFn) bool
source

IterateByOffset performs an in-order traversal of the tree starting from the specified offset.

func Remove

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Remove(key string) (value any, removed bool)
source

Remove is not supported on ReadOnlyTree and will panic.

func RemoveByIndex

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) RemoveByIndex(index int) (key string, value any)
source

RemoveByIndex is not supported on ReadOnlyTree and will panic.

func ReverseIterate

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) ReverseIterate(start, end string, cb avl.IterCbFn) bool
source

ReverseIterate performs a reverse in-order traversal of the tree within the specified key range.

func ReverseIterateByOffset

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) ReverseIterateByOffset(offset int, count int, cb avl.IterCbFn) bool
source

ReverseIterateByOffset performs a reverse in-order traversal of the tree starting from the specified offset.

func Set

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Set(key string, value any) bool
source

Set is not supported on ReadOnlyTree and will panic.

func Size

method on ReadOnlyTree
1func (roTree *ReadOnlyTree) Size() int
source

Size returns the number of key-value pairs in the tree.

Imports 1

Source Files 3