func Wrap
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:
-
No-op transformation: For primitive types (int, string, etc.) or already safe objects, simply pass nil as the makeEntrySafeFn to return values as-is.
-
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)...) }
-
Read-only wrapper: Return a read-only version of the object that implements a limited interface. Example: func(v any) any { return NewReadOnlyObject(v) }
-
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.