`CSV::Row` pattern matching `Symbol` assumption
まだ誰も着手していません。
評価
調査の方向性
CSV::Row#deconstruct_keys から始め、手動で作成した行に対する既存のパターンマッチングテストを確認します。CSV.parse とヘッダーを使って報告されたケースを再現し、解析済みの String または File データのカバレッジを追加して、シンボルキーによるパターンマッチングが既存の行の動作を壊さずに機能することを確認します。
索引モデルが issue の本文から書いたものです。
説明
There's an assumption made in the pattern matching code which will make it very hard to use, and that's the presumption of Symbol keys on CSV::Row.
If you were to make a PM interface and look at the keys:
class Testing
def deconstruct_keys(keys)
p(keys:)
{}
end
end
# => :deconstruct_keys
Testing.new in a: 1, b: 2, c: 3
# {:keys=>[:a, :b, :c]}
# => false
You will receive Symbol keys. Why is this a problem? Because if you are loading CSV data it will most likely be from a file or String which we can take directly from the documentation:
# Headers are part of data
data = CSV.parse(<<~ROWS, headers: true)
Name,Department,Salary
Bob,Engineering,1000
Jane,Sales,2000
John,Management,5000
ROWS
data.class
#=> CSV::Table
data.first
#=> #<CSV::Row "Name":"Bob" "Department":"Engineering" "Salary":"1000">
If we were to test this against pattern matching (and adding a keys debugging statement):
data.select { _1 in Name: /^J/ }
{:keys=>[:Name]}
{:keys=>[:Name]}
{:keys=>[:Name]}
=> []
As it exists today this feature cannot be used except for manually created data like in the tests which will not be the experience of end users who either parse from a String or File.
I would like to propose that we modify the method to treat the keys as keyword arguments, rather than as Symbols, which will allow this feature to be used for common usecases:
class CSV::Row
def deconstruct_keys(keys)
if keys.nil?
to_h.transform_keys(&:to_sym)
else
keys.to_h do |key|
value = if self.key?(key)
self[key]
elsif self.key?(key.to_s)
self[key.to_s]
else
nil
end
[key, value]
end
end
end
end
This does, in a sense, conflate Symbol and String keys, but String keys are impossible in pattern matching:
CSV::Row.new(%w(A B), [1, 2], true) in "A" => 1
/Users/lemur/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/irb/workspace.rb:119:in `eval': (irb):14: syntax error, unexpected integer literal, expecting local variable or method (SyntaxError)
...A B), [1, 2], true) in "A" => 1
So the options are either have an interface which will only work with test data and users who manually create Symbol keys (which is a minority case), or consider a keyword-like approach that can handle the ambiguity of keys types.
I believe this is a reasonable assumption of coercion to make, as Ruby has done this in other cases with converting between Symbol and String whenever it is considered reasonable input from the user. I believe this would qualify as reasonable user input.
If this is of interest I can submit a PR, but wanted to run the idea by people first.
- 主要言語
- Ruby
- スター
- 220
- フォーク
- 139
- 平均マージ
- 1日 1時間
- マージ済み PR(30日)
- 2
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ruby/csv のほかの issue
-
Unknown converter name is silently accepted and later fails with "undefined method 'arity' for nil"オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 57/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
-
Timeout on Windows再び着手できるかも @kou が 406 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
似ている issue
-
🐛 bug 👩💻 équipe CE 🧑🏭 entreprises
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
betagouv/conseillers-entreprises#4808 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
yosiat/panko_serializer#250 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 75/100
cline/mcp-marketplace#3886 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100