-
Notifications
You must be signed in to change notification settings - Fork 5
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
add json parser #140
base: main
Are you sure you want to change the base?
add json parser #140
Changes from all commits
981b15d
b950040
b111d4f
1600632
2be0b96
e0e564f
21d8eb4
11b9209
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Decanter | ||
module Parser | ||
class JsonParser < ValueParser | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
parser do |val, options| | ||
raise Decanter::ParseError.new 'Expects a JSON string' if val.is_a?(Array) || val.is_a?(Hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgive my ignorance here – does ActiveRecord require that the input to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the benefit of checking for an Array or a Hash as opposed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This line in particular is difficult since JSON can be many things. I almost decided to remove this entirely, but wanted to keep some error message here. I could make an argument for having no error message here, but that felt wrong? I leaned into the fact the client will send it as a string, I will check that, and then the code on line 8 will parse it to become a JSON object for Rails to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From some quick testing in the console, it looks like Because of that, to me the most readable option seems like |
||
next if (val.nil? || val === '') | ||
JSON.parse(val) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'spec_helper' | ||
|
||
describe 'JsonParser' do | ||
|
||
let(:name) { :foo } | ||
|
||
let(:parser) { Decanter::Parser::JsonParser } | ||
|
||
describe '#parse' do | ||
it 'parses string value and returns a parsed JSON' do | ||
expect(parser.parse(name, '{"key": "value"}')).to match({name => {"key" => "value"}}) | ||
end | ||
|
||
context 'with empty string' do | ||
it 'returns nil' do | ||
expect(parser.parse(name, '')).to match({name => nil}) | ||
end | ||
end | ||
|
||
context 'with nil' do | ||
it 'returns nil' do | ||
expect(parser.parse(name, nil)).to match({name => nil}) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this should inherit from
HashParser
See https://github.com/LaunchPadLab/decanter#custom-parser-base-classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that doesn't account for the fact that it could be an array. NVM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea it turns out that JSON is anything lol