본문으로 건너뛰기
AI NEWS JOURNAL
Hacker News Show HNAI NEWS / JOURNAL

Show HN: String#to_proc` to Help Traversing JSON

I've resuscitated a project I was working on years ago.I was tired of having nested `flat_map` making my code basically unreadable just to fetch a simple value. The idea is to make simple & readable the traversing of deeply nested structures like JSON responses, while staying in Ruby & keeping its flexibility. Here it goes: graphql_response.then(&'[data][repository][issues][edges]*([node][title])') # => ["Support negative indexes", "Document aggregates"] github_prs_response.then(&'*[draft].any?') # => true It's different from jq or JSONPath in that: - it's a Ruby block so you can use it with `map`, `find`, etc. - you can call Ruby method from it - it doesn't automatically flatten arrays, you decide - it raises on `nil` by default (unless you use `&`, i.e. `[customer][address]&[city]`)Obviously, it has drawbacks: - it's yet another syntax to learn - it monkey-patches `String` which might be a blocker for someI'm really looking for feedback here, what do you think? Is it worth releasing a gem with this value proposition? Comments URL: https://news.ycombinator.com/item?id=50491929 Points: 2 # Comments: 0

번역 서비스가 일시 중지되어 원문을 표시합니다.

Readable paths through nested hashes and arrays, usable as blocks. Installation Schmuck isn't released on RubyGems.org yet. Install it from GitHub and add it to the application's Gemfile by executing: bundle add schmuck --git https://github.com/samleb/schmuck Parsed JSON is made of hashes inside arrays inside hashes. In plain Ruby, every array level needs another map with another block and another variable name, and the path you care about gets lost in them. Schmuck adds String#to_proc: you write the path as a string and pass it as a block, to then or to any method that takes a block: response.then(&'[data]*([customer][address][city])') pull_requests.map(&'[user][login]') Without ?, an accessor called on nil raises, just like plain Ruby. * applies to the next accessor only, so *[a][b] maps [a] and then calls [b] on the resulting array: use *([a][b]) to apply both to each element. A paginated REST response { "data": [ { "id": "ord_1001", "customer": { "name": "Alice Martin", "address": { "city": "Lyon", "country": "FR" } }, "items": [ { "sku": "BOOK-001", "quantity": 2, "price": { "amount": 1500, "currency": "EUR" } }, { "sku": "PEN-042", "quantity": 1, "price": { "amount": 300, "currency": "EUR" } } ] }, { "id": "ord_1002", "customer": { "name": "Bob Durand", "address": { "city": "Paris", "country": "FR" } }, "items": [ { "sku": "BOOK-001", "quantity": 1, "price": { "amount": 1500, "currency": "EUR" } } ] } ], "meta": { "page": 1, "per_page": 2, "total": 2 } } Plain Ruby: response["data"].map { |order| order.dig("customer", "address", "city") } response["data"].flat_map { |order| order["items"].map { |item| item["sku"] } }.uniq response["data"].sum { |order| order["items"].sum { |item| item["quantity"] } } Schmuck: response.then(&'[data]*([customer][address][city])') # => ["Lyon", "Paris"] response.then(&'[data]*([items]*[sku]).flatten.uniq') # => ["BOOK-001", "PEN-042"] response.then(&'[data]*([items]*[quantity]).flatten.sum') # => 4 A top-level array response [ { "number": 42, "draft": false, "user": { "login": "octocat" }, "labels": [{ "name": "enhancement" }, { "name": "parser" }], "head": { "ref": "optional-accessors", "repo": { "full_name": "octocat/schmuck" } } }, { "number": 43, "draft": true, "user": { "login": "hubot" }, "labels": [], "head": { "ref": "patch-1", "repo": null } } ] Plain Ruby: pull_requests.map { |pr| pr.dig("user", "login") } pull_requests.map { |pr| pr["labels"].map { |label| label["name"] } } pull_requests.any? { |pr| pr["draft"] } pull_requests.map { |pr| pr["head"]["repo"]&.fetch("full_name") } Schmuck: pull_requests.map(&'[user][login]') # => ["octocat", "hubot"] pull_requests.map(&'[labels]*[name]') # => [["enhancement", "parser"], []] pull_requests.then(&'*[draft]."any?"') # => true pull_requests.map(&'[head][repo]?[full_name]') # => ["octocat/schmuck", nil] A deeply nested GraphQL response { "data": { "repository": { "issues": { "totalCount": 2, "edges": [ { "node": { "title": "Support negative indexes", "labels": { "edges": [{ "node": { "name": "parser" } }, { "node": { "name": "good first issue" } }] } } }, { "node": { "title": "Document aggregates", "labels": { "edges": [{ "node": { "name": "docs" } }] } } } ] } } } } Plain Ruby: response.dig("data", "repository", "issues", "edges").map { |edge| edge.dig("node", "title") } response.dig("data", "repository", "issues", "edges").flat_map do |edge| edge.dig("node", "labels", "edges").map { |label| label.dig("node", "name") } end Schmuck: response.then(&'[data][repository][issues][edges]*([node][title])') # => ["Support negative indexes", "Document aggregates"] response.then(&'[data][repository][issues][edges]*([node][labels][edges]*([node][name])).flatten') # => ["parser", "good first issue", "docs"] What sets schmuck apart from other JSON query languages Here are the unique SKUs of the REST example in the best-known JSON query languages: It's plain Ruby. A path is a block, so it works with map, select, sort_by, then or any other method that takes one. It runs on the objects you already have and returns Ruby objects: there is no JSON to serialize and parse again, and no native library to install (Ruby gems for jq wrap its C library). Any Ruby method can appear in a path. .flatten.uniq, .sum or ."any?" replace the fixed set of functions that query languages provide. Nested arrays keep their structure until you call .flatten, while jq, JSONPath and JSONata flatten results automatically. It's strict by default. Accessing anything on nil raises unless you write ?, where query languages usually return nothing. The syntax feels familiar. It extends Ruby's Symbol#to_proc (map(&:name)) to strings, and borrows Groovy's spread operator (*.) and JavaScript's optional chaining (?.). The trade-off is that a path is a string parsed at runtime (and cached), so a mistake in it only shows up when that code runs, and editors can't check or autocomplete it. Development After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org. Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/samleb/schmuck. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct. The gem is available as open source under the terms of the MIT License. Code of Conduct Everyone interacting in the Schmuck project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

자료 출처

원문 읽기 ↗

관련 주제

← 전체 소식 이어 읽기