Skip to content

Commit

Permalink
Change JSON.parse to Typecast.from_json
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Nov 13, 2023
1 parent 041840d commit 771c531
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def result_for_gates(feature, gates)
end
when :json
if row = gates.detect { |key, value| !key.nil? && key.to_sym == gate.key }
JSON.parse(row.last)
Typecast.from_json(row.last)
end
when :set
gates.select { |key, value| !key.nil? && key.to_sym == gate.key }.map(&:last).to_set
Expand Down
8 changes: 4 additions & 4 deletions lib/flipper/adapters/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def initialize(options = {})
def get(feature)
response = @client.get("/features/#{feature.key}")
if response.is_a?(Net::HTTPOK)
parsed_response = JSON.parse(response.body)
parsed_response = Typecast.from_json(response.body)
result_for_feature(feature, parsed_response.fetch('gates'))
elsif response.is_a?(Net::HTTPNotFound)
default_config
Expand All @@ -41,7 +41,7 @@ def get_multi(features)
response = @client.get("/features?keys=#{csv_keys}&exclude_gate_names=true")
raise Error, response unless response.is_a?(Net::HTTPOK)

parsed_response = JSON.parse(response.body)
parsed_response = Typecast.from_json(response.body)
parsed_features = parsed_response.fetch('features')
gates_by_key = parsed_features.each_with_object({}) do |parsed_feature, hash|
hash[parsed_feature['key']] = parsed_feature['gates']
Expand All @@ -59,7 +59,7 @@ def get_all
response = @client.get("/features?exclude_gate_names=true")
raise Error, response unless response.is_a?(Net::HTTPOK)

parsed_response = JSON.parse(response.body)
parsed_response = Typecast.from_json(response.body)
parsed_features = parsed_response.fetch('features')
gates_by_key = parsed_features.each_with_object({}) do |parsed_feature, hash|
hash[parsed_feature['key']] = parsed_feature['gates']
Expand All @@ -78,7 +78,7 @@ def features
response = @client.get('/features?exclude_gate_names=true')
raise Error, response unless response.is_a?(Net::HTTPOK)

parsed_response = JSON.parse(response.body)
parsed_response = Typecast.from_json(response.body)
parsed_response['features'].map { |feature| feature['key'] }.to_set
end

Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/adapters/http/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(response)
message = "Failed with status: #{response.code}"

begin
data = JSON.parse(response.body)
data = Typecast.from_json(response.body)

if error_message = data["message"]
message << "\n\n#{data["message"]}"
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/adapters/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def result_for_feature(feature, doc)
doc.fetch(gate.key.to_s) { Set.new }.to_set
when :json
value = doc[gate.key.to_s]
JSON.parse(value) if value
Typecast.from_json(value)
else
unsupported_data_type gate.data_type
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/adapters/pstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def result_for_feature(feature)
set_members key
when :json
value = read(key)
JSON.parse(value) if value
Typecast.from_json(value)
else
raise "#{gate} is not supported by this adapter yet"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def result_for_feature(feature, doc)
fields_to_gate_value fields, gate
when :json
value = doc[gate.key.to_s]
JSON.parse(value) if value
Typecast.from_json(value)
else
unsupported_data_type gate.data_type
end
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/adapters/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def result_for_feature(feature, db_gates)
db_gates.select { |db_gate| db_gate.key == gate.key.to_s }.map(&:value).to_set
when :json
if detected_db_gate = db_gates.detect { |db_gate| db_gate.key == gate.key.to_s }
JSON.parse(detected_db_gate.value)
Typecast.from_json(detected_db_gate.value)
end
else
unsupported_data_type gate.data_type
Expand Down
2 changes: 1 addition & 1 deletion lib/flipper/api/json_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def call(env)
# This method accomplishes similar functionality
def update_params(env, data)
return if data.empty?
parsed_request_body = JSON.parse(data)
parsed_request_body = Typecast.from_json(data)
env["parsed_request_body".freeze] = parsed_request_body
parsed_query_string = parse_query(env[QUERY_STRING])
parsed_query_string.merge!(parsed_request_body)
Expand Down
12 changes: 11 additions & 1 deletion lib/flipper/serializers/gzip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ module Serializers
module Gzip
module_function

class Stream < StringIO
def initialize(*)
super
set_encoding "BINARY"
end
def close; rewind; end
end

def serialize(source)
output = StringIO.new
return if source.nil?
output = Stream.new
gz = Zlib::GzipWriter.new(output)
gz.write(source)
gz.close
output.string
end

def deserialize(source)
return if source.nil?
Zlib::GzipReader.wrap(StringIO.new(source), &:read)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/flipper/serializers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ module Json
module_function

def serialize(source)
return if source.nil?
JSON.generate(source)
end

def deserialize(source)
return if source.nil?
JSON.parse(source)
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/flipper/serializers/gzip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
serialized = described_class.serialize("my data")
expect(described_class.deserialize(serialized)).to eq("my data")
end

it "doesn't fail with nil" do
expect(described_class.serialize(nil)).to be(nil)
expect(described_class.deserialize(nil)).to be(nil)
end
end
5 changes: 5 additions & 0 deletions spec/flipper/serializers/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
serialized = described_class.serialize("my data")
expect(described_class.deserialize(serialized)).to eq("my data")
end

it "doesn't fail with nil" do
expect(described_class.serialize(nil)).to be(nil)
expect(described_class.deserialize(nil)).to be(nil)
end
end

0 comments on commit 771c531

Please sign in to comment.