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 | |
| 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')
| -rw-r--r-- | lib/puppet/resource/catalog.rb | 61 |
1 files changed, 32 insertions, 29 deletions
diff --git a/lib/puppet/resource/catalog.rb b/lib/puppet/resource/catalog.rb index 561be82ff..8ab788ed0 100644 --- a/lib/puppet/resource/catalog.rb +++ b/lib/puppet/resource/catalog.rb @@ -4,7 +4,7 @@ require 'puppet/simple_graph' require 'puppet/transaction' require 'puppet/util/cacher' -require 'puppet/util/json' +require 'puppet/util/pson' require 'puppet/util/tagging' @@ -19,7 +19,7 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph indirects :catalog, :terminus_class => :compiler include Puppet::Util::Tagging - extend Puppet::Util::Json + extend Puppet::Util::Pson include Puppet::Util::Cacher::Expirer # The host name this is a catalog for. @@ -393,7 +393,7 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph @resource_table.keys end - def self.from_json(data) + def self.from_pson(data) result = new(data['name']) if tags = data['tags'] @@ -405,60 +405,63 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph end if resources = data['resources'] - resources = JSON.parse(resources) if resources.is_a?(String) + resources = PSON.parse(resources) if resources.is_a?(String) resources.each do |res| - resource_from_json(result, res) + resource_from_pson(result, res) end end if edges = data['edges'] - edges = JSON.parse(edges) if edges.is_a?(String) + edges = PSON.parse(edges) if edges.is_a?(String) edges.each do |edge| - edge_from_json(result, edge) + edge_from_pson(result, edge) end end result end - def self.edge_from_json(result, edge) - # If no json_class information was presented, we manually find + def self.edge_from_pson(result, edge) + # If no type information was presented, we manually find # the class. - edge = Puppet::Relationship.from_json(edge) if edge.is_a?(Hash) + edge = Puppet::Relationship.from_pson(edge) if edge.is_a?(Hash) unless source = result.resource(edge.source) - raise ArgumentError, "Could not convert from json: Could not find relationship source '%s'" % source + raise ArgumentError, "Could not convert from pson: Could not find relationship source '%s'" % source end edge.source = source unless target = result.resource(edge.target) - raise ArgumentError, "Could not convert from json: Could not find relationship target '%s'" % target + raise ArgumentError, "Could not convert from pson: Could not find relationship target '%s'" % target end edge.target = target result.add_edge(edge) end - def self.resource_from_json(result, res) - # If no json_class information was presented, we manually find - # the class. - if res.is_a?(Hash) - res = res['data'] if res['json_class'] - res = Puppet::Resource.from_json(res) - end + def self.resource_from_pson(result, res) + res = Puppet::Resource.from_pson(res) if res.is_a? Hash result.add_resource(res) end - def to_json(*args) + PSON.register_document_type('Catalog',self) + def to_pson_data_hash { - 'json_class' => 'Puppet::Resource::Catalog', - 'data' => { - 'tags' => tags, - 'name' => name, - 'version' => version, - 'resources' => vertices.to_json(*args), - 'edges' => edges.to_json(*args) - } - }.to_json(*args) + 'document_type' => 'Catalog', + 'data' => { + 'tags' => tags, + 'name' => name, + 'version' => version, + 'resources' => vertices.collect { |v| v.to_pson_data_hash }, + 'edges' => edges. collect { |e| e.to_pson_data_hash } + }, + 'metadata' => { + 'api_version' => 1 + } + } + end + + def to_pson(*args) + to_pson_data_hash.to_pson(*args) end # Convert our catalog into a RAL catalog. |
