-
Notifications
You must be signed in to change notification settings - Fork 8
[Feature Request] Support Gob (Encoding & Decoding) #27
Copy link
Copy link
Open
Description
Hello there and really appreciate your work implementing the adaptive radix trie.
Due to the fact that there are no exported fields in the structs (Tree, Node, Item etc.) it is not possible for the Gob encoder to encode the Tree into binary.
As an example the following simple code:
rt := radixtree.New[string]()
rt.Put("tomato", "TOMATO")
var byt bytes.Buffer
writer := gob.NewEncoder(&byt)
err := writer.Encode(rt)
if err != nil {
panic(err)
}
Exits with a panic error message:
panic: gob: type radixtree.Tree[string] has no exported fields
From my point of view to support gob, there are two ways to achieve that:
- Convert all the fields from unexported to exported and gob will be supported out of the box:
// Tree is a radix tree of bytes keys and any values.
type Tree[T any] struct {
Root radixNode[T] // instead of root
Size int // instead of size
}
- Implement a custom encoder and decoder for each struct (Tree, Node, Item etc.). As an example the Tree struct needs to implement the following two methods (the same should happen for each struct):
func (t *Tree[T any]) GobEncode() ([]byte, error) {
// implement custom logic
return nil, nil
}
func (t *Tree[T any]) GobDecode(data []byte) error {
// implement custom logic
return nil
}
Please let me know your thoughts.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels