madlambda / nash

Nash stands for Nash shell.
Apache License 2.0
157 stars 11 forks source link

structured data #155

Open errordeveloper opened 7 years ago

errordeveloper commented 7 years ago

One of the major downsides of traditional shells is the lack of direct support for any of well-known structured data formats (e.g. JSON, YAML, or even XML example). As a modern implementation, Nash seems to be well positioned to implement support for such a data format, namely it'd be a great win if there was a way to read, modify, query and compose JSON in a syntactically meaningful way.

i4ki commented 7 years ago

I agree with you that it would be very nice to work with such data structures in a meaningful way in nash scripts (without external tools like jq), but I'm not sure this will ever happen... I'm not against someone implementing this as long as it's not built in. Nash libraries could be created for that and we can ship this nashscript libraries alongside nash, but it'll not be easy to develop with current nash, mostly because nash language is very minimal and this data formats are pretty complex. You may think that json is simple, but not so much for a language that only has string, list and function as data types. JSON is achievable, but YAML and XML are definitely very very hard (if not impossible to do).

For example, nash doesn't have an hash table data structure, nor the concept of objects/structs, then the json/yaml/etc must be parsed into a list representing an object. A list is a powerful data structure that you can store almost everything, but a tree/object or a hash table is very inefficient and hard to work with on top of it. Schemers (r5rs ones) create such libraries this way, but it sucks.

As I do think this would be great, we can discuss improvements in nash supported data structures and syntax-constructs to easy creating that in the future (if more people involved see value, of course). Adding a hash table data type is something I'm thinking for a long time, but adding other types as integers, floats, and so on, bring some interesting trade offs. @katcipis @geyslan @vitorarins @lborguetti

Apart from that, there are one missing features in nash to enable creating such libraries in scripts. A typeof-like built in function must be created, it will be needed to marshal the list (representing a tree) into a string (json output for example).

An external tool can be used to generate the lexical tokens of the input, but even the lexer could be done in nash script if we add support to range over the characters/runes of a string in a for-loop.

I do use json on some of my scripts, but they rely on jq. Jq supports some dynamic way to compose json, but it sucks...

geyslan commented 7 years ago

@tiago4orion, do you mean a proper hash table or an abstract map implementation?

i4ki commented 7 years ago

I meant a built in abstract map implementation because a hash table implemented in shell should be a disaster (in terms of performance).

vitorarins commented 7 years ago

IMO JSON/Yaml handling would add a considerable amount of complexity to nash. I agree that in some cases would be nice to have a simple way to generate JSON files, I just don't think it's a good idea to make this a built in feature for nash.

errordeveloper commented 7 years ago

It makes sense to have a built-in map, or hash, whatever would help this, and users can write a library for parsing and/or generating whatever structured data format they need. For example, even if one wanted to write a library wrapping jq, it would be great to be able to store input/output data in a native structure.

On Thu, 12 Jan 2017, 00:51 Vitor Arins, notifications@github.com wrote:

IMO JSON/Yaml handling would add a considerable amount of complexity to nash. I agree that in some cases would be nice to have a simple way to generate JSON files, I just don't think it's a good idea to make this a built in feature for nash.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/NeowayLabs/nash/issues/155#issuecomment-272043500, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPWS9WLorCs_wN5p8HvUr04yrHRiMEmks5rRXj1gaJpZM4LeyG5 .

katcipis commented 7 years ago

Added #202