{"id":14985674,"url":"https://github.com/binaryphile/y2s","last_synced_at":"2025-04-11T22:02:25.757Z","repository":{"id":143324991,"uuid":"91019637","full_name":"binaryphile/y2s","owner":"binaryphile","description":"Basic yml-subset parsing in bash","archived":false,"fork":false,"pushed_at":"2017-05-12T18:52:33.000Z","size":23,"stargazers_count":19,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-25T17:53:30.657Z","etag":null,"topics":["bash","yaml","yml"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/binaryphile.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-11T20:16:04.000Z","updated_at":"2024-12-10T17:09:10.000Z","dependencies_parsed_at":"2023-05-14T21:30:22.858Z","dependency_job_id":null,"html_url":"https://github.com/binaryphile/y2s","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.040000000000000036","last_synced_commit":"71840aa4a3914054d626b3372761f2410fefdf31"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binaryphile%2Fy2s","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binaryphile%2Fy2s/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binaryphile%2Fy2s/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/binaryphile%2Fy2s/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/binaryphile","download_url":"https://codeload.github.com/binaryphile/y2s/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248487710,"owners_count":21112188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","yaml","yml"],"created_at":"2024-09-24T14:11:28.025Z","updated_at":"2025-04-11T22:02:25.721Z","avatar_url":"https://github.com/binaryphile.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"y2s [![Build Status](https://travis-ci.org/binaryphile/y2s.svg?branch=master)](https://travis-ci.org/binaryphile/y2s)\n===\n\ny2s stands for \"YAML2struct\". It is a handful of bash functions for\nparsing a limited subset of YAML into a makeshift bash nested data\nstructure.\n\nThe structure, or \"struct\" for short, is a hash (a.k.a. associative\narray) which contains keys for all of the leaf elements in the YAML\nsource document, along with a slightly custom syntax for storing copies\nof all of the intermediate levels of the structure as well.\n\nFor example, consider the following `demo.yml` file:\n\n    myhash:\n      one: 1\n      two: 2\n    myarray:\n      - zero\n      - one\n\nUsing the function `yml2struct` to store the result in a hash called\n`hash` looks like this:\n\n    $ source y2s.bash\n    $ declare -A hash\n    $ yml2struct hash demo.yml\n\nThe resulting `hash` looks like so:\n\n    hash[myhash]='([one]=\"1\" [two]=\"2\" )'\n    hash[myhash.one]=\"1\"\n    hash[myhash.two]=\"2\"\n    hash[myarray]='([0]=\"zero\" [1]=\"one\" )'\n    hash[myarray.0]=\"zero\"\n    hash[myarray.1]=\"one\"\n\nNotice that the scalar values are available via the keys which specify\ntheir full paths (although this is not the recommended usage pattern,\ninstead see below):\n\n    $ echo \"${hash[myarray.0]}\"\n    zero\n\nThe non-scalar values are still stored as strings, however they are\nserialized as the right-hand side of an assignment statement. For\nexample, you could do this (again, not the recommended usage pattern):\n\n    $ eval \"array=${hash[myarray]}\"\n    $ echo \"${array[0]}\"\n    zero\n\nSince these methods of accessing the values are somewhat clunky, there\nis a convenience function, called `lookup`, which allows you to use\ndotted notation to access values and substructures.\n\nIf it is given the name of a variable as its second argument, it stores\nthe value there, otherwise it echoes the value:\n\n    $ lookup hash.myhash.one\n    1\n\n    $ unset -v array\n    $ declare -a array\n    $ lookup hash.myarray array\n    $ echo \"${array[0]}\"\n    zero\n\n    $ declare -A myhash\n    $ lookup hash.myhash myhash\n    $ echo \"${myhash[one]}\"\n    1\n\nUsing `lookup` to instantiate a hash with nested structures also\npreserves the embedded structures, so the result is itself a struct.\nThis means you can instantiate and then use subportions of the data\nstructure with the `lookup` function as well.\n\nInstantiating an array with nested elements would lose the embedded\nstructure data, since the array is not capable of using complex\nstring-based keys. For that reason, `lookup` doesn't support doing so\nwith arbitrary subtrees.\n\nOnly flat array structures can be instantiated into a bash array.\n\nHowever, you may instead use a hash as the variable into which an array\nstructure would be stored, and it will act the same as an array does\n(for the most part) but will also still be a struct.\n\nInstallation\n------------\n\nRequires Bash 4.3 or higher.\n\ny2s depends on the [nano] library. Follow its installation instructions,\nthen clone y2s and put its `lib` directory on your PATH.\n\nThen you may source it in your scripts with `source y2s.bash`.\n\nLimitations\n-----------\n\ny2s:\n\n-   understands hashes and arrays, in addition to scalar values.\n\n-   only allows alphanumerics and underscores in hash keys, so they can\n    be instantiated as bash variables\n\n-   supports nesting arrays in hashes and vice-versa.\n\n-   understands plain (unquoted), single- and double-quoted scalars\n\n    -   it strives for compatibility with Ruby's syck implementation as\n        a guide\n\n-   only accepts indents of two space characters per level\n\n-   only allows printable characters in values (including whitespace)\n\n-   only supports a very limited subset of YAML\n\n    -   values are limited to a single line\n\n    -   double-quoted values do, however, allow the use of escaped\n        characters such as `\\n`\n\n-   does not understand the JSON forms of keys, hashes or arrays\n\n-   does not support converting structs to yml, nor writing yml files\n\n-   has no feedback on errors which occur during parsing (at the moment)\n\n    -   the best thing to try is to pass the same input through a real\n        parser\n\nAlso, because y2s denormalizes the yml data, copying the same value into\nmultiple locations, it is only suitable for reading data. Writing data\ninto a struct would require updating all of the locations in which that\ndata appears. There are no plans for implementing such support.\n\nShouts-Out\n----------\n\ny2s is inspired by [YAY] and the Stack Overflow articles cited by YAY.\n\n  [nano]: https://github.com/binaryphile/nano\n  [YAY]: https://github.com/johnlane/random-toolbox/blob/master/usr/lib/yay\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinaryphile%2Fy2s","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinaryphile%2Fy2s","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinaryphile%2Fy2s/lists"}