tmt96/rs-object-space

Support Enum

Closed this issue · 3 comments

tmt96 commented

Currently TreeObjectSpace does not support Enum. As Enum is an integral part of Rust (e.g: Option, Result, etc), supporting for Enum is crucial.

This might require rethinking field name checking in Entry as different subtypes in an enum may contain different field names. Refer https://serde.rs/json.html for more information on how enum is represented in serde-json

tmt96 commented

Currently there are two approaches:

  1. Remove the path validity checking code for all types. This is easy to achieve, yet make code for non-enum type less safe: we could refer to an invalid path without receiving an error.
  2. Check whether a type is an enum or not. How to achieve this is still unclear.
tmt96 commented

Current implementation of Entry is unsuitable for supporting Enum. Currently an Entry is in essence a map of path & path value to the collection of corresponding Arc<Value>. This creates three problems:

  1. A lot of duplication of Arc<Value> causing "multiple source of truth" issue.
  2. Different Enums serialized into differnt path. e.g:
enum MyEnum {
    Str(String),
    Person {name: String, age: i32}
}

serialized into either "Str": <string> or "Person": {"name": <string>, "age": <int>}
3. Many functions however rely on the similarity of the Arc<Value> collection of different path. For example, if different paths have different collections of Arc<Value>, read_all and take_all will be very troublesome to implement as we have to get Value from each path and then remove duplicates, which is inefficient.

A new approach is being explored in #7

tmt96 commented

close with #7