diff options
| author | Markus Roberts <Markus@reality.com> | 2009-10-09 15:23:19 -0700 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-10-17 12:23:33 +1100 |
| commit | bca3b70437666a8b840af032cab20fc1ea4f18a2 (patch) | |
| tree | 0cde5cd7b39fc5a5e41b636d1f55a7ed71d6518b /lib/puppet/resource.rb | |
| parent | ce46be5773656f68eddc7edd6212e283b46f9320 (diff) | |
| download | puppet-bca3b70437666a8b840af032cab20fc1ea4f18a2.tar.gz puppet-bca3b70437666a8b840af032cab20fc1ea4f18a2.tar.xz puppet-bca3b70437666a8b840af032cab20fc1ea4f18a2.zip | |
Bundling of pure ruby json lib as "pson"
Bundeling and renaming the pure ruby json library to addresses a
number of cross version serliaization bugs (#2615, et al).
This patch adds a subset of the files from the json_pure gem to
lib/puppet/external/pson (renamed to avoid conflicts with rails) so
that we will always have a known-good erialization format available.
The pure ruby json gem as distibuted defers to the compiled version
if it is installed. This is problematic in some circumstances so the
files that have been brought over have been modified to always and
only use the bundled version.
It's a large patch, so here's a breakdown of the change categories:
The majority of the lines are only marginally interesting:
* The json lib itself (in lib/puppet/external/pson) make up the bulk
of the lines.
* Renaming of json to pson make up the second largest group.
Somewhat more interesting are the following, which can be located by
searching the diffs for the indicated strings:
* Adjusting tests to reflect the changes
* Changing the encoding/decoding behavior so that nested structures
(e.g. resources) don't serialize as escaped strings. This should
make it much easier to process the results with external tools, if
needed. Search for "to_pson" and "to_pson_data_hash"
* Cleaning up the envelope/metadata
* Now provides a document_type (as opposed to a ruby class name) by
using a symple registration scheme instead of constant lookup
(search for "document_type")
* Added an api_version (search for "api_version")
* Added a hash for document metadata (search for "metadata")
* Removing the yaml monkeypatch and instead disabling yaml serialization
on ruby 1.8.1 in favor of pson (search for "yaml")
* Cleaning up the json/rails feature interaction (they're now totally
independent) (search for "feature")
Diffstat (limited to 'lib/puppet/resource.rb')
| -rw-r--r-- | lib/puppet/resource.rb | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb index 3e27e0e00..91dd547fb 100644 --- a/lib/puppet/resource.rb +++ b/lib/puppet/resource.rb @@ -1,35 +1,35 @@ require 'puppet' require 'puppet/util/tagging' require 'puppet/resource/reference' -require 'puppet/util/json' +require 'puppet/util/pson' # The simplest resource class. Eventually it will function as the # base class for all resource-like behaviour. class Puppet::Resource include Puppet::Util::Tagging - extend Puppet::Util::Json + extend Puppet::Util::Pson include Enumerable attr_accessor :file, :line, :catalog, :exported, :virtual attr_writer :type, :title ATTRIBUTES = [:file, :line, :exported] - def self.from_json(json) - raise ArgumentError, "No resource type provided in json data" unless type = json['type'] - raise ArgumentError, "No resource title provided in json data" unless title = json['title'] + def self.from_pson(pson) + raise ArgumentError, "No resource type provided in pson data" unless type = pson['type'] + raise ArgumentError, "No resource title provided in pson data" unless title = pson['title'] resource = new(type, title) - if params = json['parameters'] + if params = pson['parameters'] params.each { |param, value| resource[param] = value } end - if tags = json['tags'] + if tags = pson['tags'] tags.each { |tag| resource.tag(tag) } end ATTRIBUTES.each do |a| - if value = json[a.to_s] + if value = pson[a.to_s] resource.send(a.to_s + "=", value) end end @@ -39,9 +39,7 @@ class Puppet::Resource resource end - def to_json(*args) - raise "Cannot convert to JSON unless the 'json' library is installed" unless Puppet.features.json? - + def to_pson_data_hash data = ([:type, :title, :tags] + ATTRIBUTES).inject({}) do |hash, param| next hash unless value = self.send(param) hash[param.to_s] = value @@ -59,15 +57,13 @@ class Puppet::Resource hash end - unless params.empty? - data["parameters"] = params - end + data["parameters"] = params unless params.empty? + + data + end - res = { - 'json_class' => self.class.name, - 'data' => data - } - res.to_json(*args) + def to_pson(*args) + to_pson_data_hash.to_pson(*args) end # Proxy these methods to the parameters hash. It's likely they'll |
