Skip to content

[Feature Request] Support Gob (Encoding & Decoding) #27

@apanag

Description

@apanag

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:

  1. 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
}
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions