Skip to content

Commit

Permalink
(maint) Refactor Sidecar Protocol serialisation
Browse files Browse the repository at this point in the history
Previously the Sidecar Protocol had the serialisation methods on the
BasePuppetObject however this meant anything that wanted to use the
serialisation methods also inherited the parameters like key and calling_source.

This commit:
* Moves the serialisation methods (to_json, from_json!) to BaseClass which
  BasePuppetObject now inherits from
* Adds equality methods on the BaseClass to make it possible to compare
  Sidecar objects correctly.  This is mainly required in spec tests to test for
  equality (either == or eql?)
* Adds a hash method to generate the hash number required when Sidecar objects
  are put into hashtables

There are no test changes as this is a refactor only.
  • Loading branch information
glennsarti committed Jun 10, 2019
1 parent b1dc150 commit 8ba9d58
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions lib/puppet-languageserver/sidecar_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,61 @@ def from_json!(json_string)
end
end

class BaseClass
include Base

def to_json(*options)
to_h.to_json(options)
end

def from_json!(json_string)
from_h!(JSON.parse(json_string))
end

def ==(other)
return false unless other.class == self.class
self.class
.instance_methods(false)
.reject { |name| name.to_s.end_with?('=') || name.to_s.end_with?('!') }
.reject { |name| %i[to_h to_json].include?(name) }
.each do |method_name|
return false unless send(method_name) == other.send(method_name)
end
true
end

def eql?(other)
return false unless other.class == self.class
self.class
.instance_methods(false)
.reject { |name| name.to_s.end_with?('=') || name.to_s.end_with?('!') }
.reject { |name| %i[to_h to_json].include?(name) }
.each do |method_name|
return false unless send(method_name).eql?(other.send(method_name))
end
true
end

def hash
props = []
self.class
.instance_methods(false)
.reject { |name| name.to_s.end_with?('=') || name.to_s.end_with?('!') }
.reject { |name| %i[to_h to_json].include?(name) }
.each do |method_name|
props << send(method_name).hash
end
props.hash
end
end

# key => Unique name of the object
# calling_source => The file that was invoked to create the object
# source => The file that _actually_ created the object
# line => The line number in the source file where the object was created
# char => The character number in the source file where the object was created
# length => The length of characters from `char` in the source file where the object was created
class BasePuppetObject
include Base
class BasePuppetObject < BaseClass
attr_accessor :key
attr_accessor :calling_source
attr_accessor :source
Expand Down Expand Up @@ -64,14 +111,6 @@ def from_h!(value)
self.length = value['length']
self
end

def to_json(*options)
to_h.to_json(options)
end

def from_json!(json_string)
from_h!(JSON.parse(json_string))
end
end

class BasePuppetObjectList < Array
Expand Down

0 comments on commit 8ba9d58

Please sign in to comment.