`CSV::Row` pattern matching `Symbol` assumption
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 42/100
Línea de trabajo
Comienza en CSV::Row#deconstruct_keys y revisa las pruebas existentes de pattern matching para filas creadas manualmente. Reproduce el caso reportado con CSV.parse y encabezados, luego añade cobertura para datos String o File analizados y verifica que el pattern matching con claves de símbolo funcione sin romper el comportamiento existente de las filas.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Ruby
- Estrellas
- 220
- Forks
- 139
- Merge medio
- 1 d 1 h
- PR fusionados (30 d)
- 2
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de ruby/csv
-
Unknown converter name is silently accepted and later fails with "undefined method 'arity' for nil"Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 57/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
-
Timeout on WindowsQuizá libre de nuevo @kou la tomó hace 406 días y no hay ningún pull request abierto. Abierto
-
FiberError/NoMemoryError error related to csv gem occurs when upgrading from ruby 3.1 to 3.3Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
-
Recipes not copied downstreamAbierto
Dificultad 4/5 3-5 días Aptitud para principiantes 25/100
Issues similares
-
🐛 bug 👩💻 équipe CE 🧑🏭 entreprises
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
betagouv/conseillers-entreprises#4808 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
yosiat/panko_serializer#250 ·
-
[Server Submission]: Aspera CLIAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 75/100
cline/mcp-marketplace#3886 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100