nin-jin / tree.d

Tree - simple fast compact user-readable binary-safe extensible structural format
The Unlicense
198 stars 14 forks source link

Encoding tuples and atoms. #5

Open Virviil opened 6 years ago

Virviil commented 6 years ago

Many programming languages have tuples as there data structures and atoms under the hood. No one of existing encoding formats supports tuples and atoms.

Here tuple is a sort of list with predefined length and type. Here atom is a string, that represents itself as a language construction. For example, you can encode enums with atoms, or do another kind of metaprogramming.

If tree will do this - it'll be very generic to encode configs or other internal data structures in many languages. For example you can pass Interface names as atoms, or function parameters with tuples.

Why they should be separated from list and string?

Imagine deep nested structure, without predefined schema. If the schema is not defined, no one know will it be string or atom, will it be list or tuple. After decoding this structures represents different ways from each other.

Programming languages without tuples still can decode them into lists or arrays.

If this feature will be implemented in the specification - I'll create tree serializer/deserializer for Elixir and Erlang languages and will try to make this format a defacto standard for elixir configuration.

nin-jin commented 6 years ago

tree is only AST representation format (like LISP or XML). Semantic of node names may be defined on language level (like XHTML, XSLT and other). 3rd level is program interface. In example let's define object serialization language obj.tree..

TypeScript class and object:

enum Sex { male , female }

class User {
    sex : Sex
    name : String
    location : [ Number , Number ]
}

const jin = new User({
    sex : Sex.male ,    
    name : 'Jin' ,
    location : [ 60 , 30 ]
})

Serialization:

User
    sex male
    name \Jin
    location
        60
        30

We can define separate serialization schema schema.tree:

@Type User
    sex @Enum
        male
        female
    name @String
    location @Tuple
        latitude @Number
        longitude @Number

male and female are atoms that grouped to enum sex. And location is tuple of two numbers.

We can allow to inject schema into objects dump:

@Type User
    sex @Enum
        male
        female
    name @String
    location @Tuple
        latitude @Number
        longitude @Number
User
    sex male
    name \Jin
    location
        60
        30
User
    sex female
    name \Mary
    location
        60
        30
Virviil commented 6 years ago

This works only for strong typed languages.

nin-jin commented 6 years ago

Type hints may be inlined (* - dictionary, / - list , | - tuple , @ - atoms):

users /
    *
        sex @male
        name \Jin
        location |
            60
            30
    *
        sex @female
        name \Mary
        location |
            60
            30