-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Dictionary Data Type #66
Comments
That is definitely something worth exploring. It would be even better to make it conform to JSON format - this way we wouldn't need to do conversions of the type... although I'm not sure if this would be the best idea performance wise.... |
perhaps consider some js-like syntax? let obj = {
foo: "bar"
}
echo "foo is " + obj[foo]
echo "0 is " + obj[0] then i guess it should compile into something kind of like this obj_k=( foo )
obj_v=( bar )
get_obj_by_key() {
for i in "${!obj_k[@]}"; do
if [[ ${obj_k[$i]} == "$1" ]]; then
echo ${obj_v[$i]}
return
fi
done
}
get_obj_by_index() {
for i in "${!obj_v[@]}"; do
if [[ "$i" == "$1" ]]; then
echo ${obj_v[$i]}
return
fi
done
}
echo foo is $(get_obj_by_key foo)
echo 0 is $(get_obj_by_index 0) not sure how to handle nested objects, though |
I think that JS-like syntax here is on spot. Having to support more complex data could be harder in bash. We can discuss it on the community discord server |
i feel like we should decide on the syntax we want to adopt. imo there are 3 ways it could go:
also another topic for discussion is how we are going to handle serialization to/from JSON and other types. maybe create an external package like |
Bash arrays have a drawback of slowing down as their size increases.
#!/bin/sh
get_obj_by_key() {
cat "$1" |
grep "$2" |
sed -n '$s/^'"$2"'="*\(.*[^"]\)"*/\1/p'
}
get_obj_by_index() {
num=$2
line=$((num + 1))
cat "$1" |
sed -n ''"$line"'s/^[^=][^=]*="*\(.*[^"]\)"*$/\1/p'
}
obj=$(mktemp)
echo "foo=\"bar\"" >> "${obj}"
echo "baz=\"obj_baz\"" >> "${obj}"
echo "fruits_orange=1" >> "${obj}"
echo "fruits_apple=3" >> "${obj}"
echo "foo is $(get_obj_by_key "${obj}" foo)"
echo "0 is $(get_obj_by_index "${obj}" 0)"
echo "apple is $(get_obj_by_key "${obj}" fruits_apple)" |
I agree that adding ![]() I think that I like the |
About the mktemp version... couldn't that also be a non-temp file, thus giving us a persistent key-value datastore, akin to Python's shelve? (Though for small amounts of data.) And secondarily, would it be conceivable then to support TOML as the format, which would seem to allow multiple sets of key-value pairs in the same file? (Maybe that's going too far but it seems like being able to read TOML files would be good, and could dovetail with the other key-value functionality.) |
are you sure you want to rely on temporary files? afterall, the script's user might not have the permissions to do that, and it is awful from a security perspective - a third program can easily modify the script's memory what if we used bash's variables instead of files? that seems pretty much doable |
It cannot be denied.
I consider the security of the permissions for files created by the
This risk is about the same for general programs or shell scripts that create temporary files.
Your previous post mentioned the following:
If you have any ideas for a clever implementation using variables, an example would be greatly appreciated. Handling large amounts of data without using temporary files can also be difficult. In the future, there may be cases where temporary files (or directories) are used when implementing other features. Since |
i've spent this weekend implementing different appoaches to objects in bash, trying to get as close as possible to something like objects in actual dynamically typed languages. i dont think that we could do much with implementing this thing. like, we are pretty much limited by bash. maintaining our own file specification is overkill, not to even mention how we are going to handle escaped strings and nested objects, and how is this going to affect code readability + emitted program size. someone has mentioned linked arrays in bash. they do not exist in bash that comes with all macos's and cannot be nested, or passed to a function. like, the best we could do is to depend on |
I think it would be good to implement it with |
just to make sure, we are going with this? also we might consider this: https://github.com/kristopolous/TickTick @Ph0enixKM @boushley @brumik what do you think |
We have a couple of routes at this point:
|
@b1ek Sorry to come back late. I had to do some small research on my own. Here are my two cents: I think Amber has to decide what it wants to be. As far as it was going until now it was a wrapper around bash for people who already do scripting in bash, for systems that support bash. With this in mind I do not think it is absolutely necessary to have nested arrays, objects and things that are impossible (by default at least) in bash. I can also see issues with creating a temp file in performance too (and completely agree with @b1ek about security permission and immutable system problems). If I would suggest something probably would be the fact to not to support dictionary type, only arrays (which can be done with simple constants). This seems like it would be an issue for some, but overall more healthy for the project and the expectations. I really think it is important for users and developers to define the scope of the language. For cross platform programming (not scripting) there are other languages (like C and rust for example). |
Thank you @brumik for your insight. 👏 As we've been discussing this issue some other idea arouse #161. We could build a runtime that can get fetched (if not exited) and would extend Amber for more functionality. I think that letting Amber be a shell language and yet letting users use an extension if required needed for their needs. Perhaps they have already built something with Amber and the need just that one little thing that is not really supported by Bash but is pretty common in other programming languages. But honestly I think that ultimately the best way would be to just utilize the Bash's features as well as possible and perhaps provide some other functionalities as a form of a library. |
@Ph0enixKM |
Yes. But not in the scope of this issue. Let’s create a discussion for that. |
Just a minor comment, possibly moot, about the jq possibility: for some people, it's nontrivial to install on MacOS: https://stackoverflow.com/questions/71406984/how-to-instal-jq-without-homebrew |
For jq as dependency we have a @b1ek bash project that we have to integrate so it is a complete different issue. |
the problem remains though: it is not available on all systems |
I think that for any tool we will use in the Bash generated that can be After all in this way it is the same feature that other scripting languages have, only that in the Bash case maybe the stuff/commands already avalaible are less. |
I don't understand what you are trying to say here. I think that Amber should not depend on the |
Parse JSON in pure bash it is something that I don't like at all, I think that we should use tools if they are there otherwise there is an error about the script can't run there. |
https://github.com/h4l/json.bash It is a project to manipulate JSON in pure bash, so we should start thinking to create a system to embed pure bash libraries. |
The data-types in Amber could be even better if they were extended to include a dictionary/map type. Something like
[Text, Num]
.The text was updated successfully, but these errors were encountered: