-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better handling of malformed messages (#69)
* Refactor the Payload for a small performance boost, and better handling of when the message is malformed. Fixes #67 * Testing if ameba passes without using alpine Crystal * When converting the identifier back to JSON, use the original JSON instead of generating a new one to ensure the result is always the same
- Loading branch information
Showing
7 changed files
with
136 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,118 @@ | ||
module Cable | ||
class Payload | ||
struct Payload | ||
include JSON::Serializable | ||
include JSON::Serializable::Unmapped | ||
alias RESULT = String | Int64 | Hash(String, RESULT) | ||
alias PARAMS = Hash(String, RESULT) | ||
|
||
getter json : String | ||
getter action : String | ||
getter command : String? | ||
getter identifier : String | ||
getter channel : String? | ||
getter channel_params : Hash(String, Cable::Payload::RESULT) = Hash(String, RESULT).new | ||
getter data : Hash(String, Cable::Payload::RESULT) = Hash(String, RESULT).new | ||
|
||
def initialize(@json : String) | ||
@parsed_json = JSON.parse(@json) | ||
@action = "" | ||
@is_action = false | ||
@command = @parsed_json["command"].as_s | ||
@identifier = @parsed_json["identifier"].as_s | ||
@channel = process_channel | ||
@channel_params = process_channel_params | ||
@data = process_data | ||
module IdentifierConverter | ||
def self.from_json(value : JSON::PullParser) : Indentifier | ||
key = value.read_string | ||
i = Indentifier.from_json(key) | ||
i.key = key | ||
i | ||
end | ||
|
||
def self.to_json(value : Indentifier, json : JSON::Builder) : Nil | ||
json.string(value.key) | ||
end | ||
end | ||
|
||
struct Indentifier | ||
include JSON::Serializable | ||
include JSON::Serializable::Unmapped | ||
|
||
property channel : String | ||
|
||
# This is the original JSON used to parse this | ||
# It's used as a unique key to map the different channels | ||
@[JSON::Field(ignore: true)] | ||
property key : String = "" | ||
end | ||
|
||
def action? | ||
@is_action | ||
@[JSON::Field] | ||
getter command : String | ||
|
||
@[JSON::Field(converter: Cable::Payload::IdentifierConverter)] | ||
getter identifier : Indentifier | ||
|
||
@[JSON::Field(ignore: true)] | ||
getter action : String = "" | ||
|
||
# After the Payload is deserialized, parse the data. | ||
# This will ensure we know if it's an action. | ||
def after_initialize | ||
data | ||
end | ||
|
||
private def parsed_identifier | ||
JSON.parse(@parsed_json["identifier"].as_s) | ||
def channel : String | ||
identifier.channel | ||
end | ||
|
||
private def process_channel | ||
parsed_identifier["channel"].as_s | ||
def action? : Bool | ||
!action.presence.nil? | ||
end | ||
|
||
private def json_data | ||
JSON.parse(@parsed_json["data"].as_s) if @parsed_json.as_h.has_key?("data") | ||
@[JSON::Field(ignore: true)] | ||
@_channel_params : Hash(String, RESULT)? = nil | ||
|
||
# These are the additional data sent with the identifier | ||
# e.g. `{channel: "RoomChannel", room_id: 1}` | ||
# ``` | ||
# channel_params["room_id"] # => 1 | ||
# ``` | ||
def channel_params : Hash(String, RESULT) | ||
if @_channel_params.nil? | ||
@_channel_params = process_hash(identifier.json_unmapped) | ||
else | ||
@_channel_params.as(Hash(String, RESULT)) | ||
end | ||
end | ||
|
||
private def process_data | ||
if jsd = json_data | ||
params = jsd.as_h.dup | ||
if deleted_action = params.delete("action") | ||
@action = deleted_action.as_s | ||
@is_action = true | ||
end | ||
@[JSON::Field(ignore: true)] | ||
@_data : Hash(String, RESULT)? = nil | ||
|
||
process_hash(params) | ||
def data : Hash(String, RESULT) | ||
if @_data.nil? | ||
if unmapped_data = json_unmapped["data"]? | ||
@_data = process_data(unmapped_data.as_s) | ||
else | ||
@_data = no_data | ||
end | ||
else | ||
Hash(String, RESULT).new | ||
@_data.as(Hash(String, RESULT)) | ||
end | ||
end | ||
|
||
private def process_channel_params | ||
params = parsed_identifier.as_h.dup | ||
params.delete("channel") | ||
private def no_data : Hash(String, RESULT) | ||
Hash(String, RESULT).new | ||
end | ||
|
||
process_hash(params) | ||
private def process_hash(_params : Nil) | ||
no_data | ||
end | ||
|
||
private def process_hash(params : Hash(String, JSON::Any)) | ||
params_result = Hash(String, RESULT).new | ||
|
||
params.each do |k, v| | ||
if v.as_s? | ||
params_result[k] = v.as_s | ||
elsif v.as_i64? | ||
params_result[k] = v.as_i64 | ||
elsif v.as_h? | ||
params_result[k] = process_hash(v) | ||
if strval = v.as_s? | ||
params_result[k] = strval | ||
elsif intval = v.as_i64? | ||
params_result[k] = intval | ||
elsif hshval = v.as_h? | ||
params_result[k] = process_hash(hshval) | ||
end | ||
end | ||
|
||
params_result | ||
end | ||
|
||
private def process_hash(hash : JSON::Any) | ||
process_hash(hash.as_h) | ||
private def process_data(data_string : String) | ||
json_data = JSON.parse(data_string).as_h? | ||
hash = process_hash(json_data) | ||
@action = hash.delete("action").to_s | ||
hash | ||
end | ||
end | ||
end |