qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [42/50] [abbrv] qpid-proton git commit: PROTON-736: Created the UTFString and BinaryString classes for Ruby.
Date Fri, 14 Nov 2014 11:03:51 GMT
PROTON-736: Created the UTFString and BinaryString classes for Ruby.

This allows users to explictly call out that a string is either UTF-8 or
a binary string.

For the UTFString type, it validates that the content provided is
actually UTF-8 and raises a RuntimeError if it's not.

Results returned from a Message object are wrapped in the appropriate
class.

This solution is Ruby 1.8, 1.9 and 2.0 compatible.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/6371e9eb
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/6371e9eb
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/6371e9eb

Branch: refs/heads/examples
Commit: 6371e9eba3b82a4927bcfb06919d4b3ea419a8d8
Parents: bdeaf34
Author: Darryl L. Pierce <mcpierce@gmail.com>
Authored: Mon Nov 10 09:30:47 2014 -0500
Committer: Darryl L. Pierce <mcpierce@gmail.com>
Committed: Mon Nov 10 11:20:15 2014 -0500

----------------------------------------------------------------------
 proton-c/bindings/ruby/lib/qpid_proton.rb       |  1 +
 proton-c/bindings/ruby/lib/qpid_proton/data.rb  |  4 +-
 .../bindings/ruby/lib/qpid_proton/mapping.rb    | 22 +------
 .../bindings/ruby/lib/qpid_proton/strings.rb    | 69 ++++++++++++++++++++
 4 files changed, 75 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton.rb b/proton-c/bindings/ruby/lib/qpid_proton.rb
index e28c684..4da4e11 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton.rb
@@ -22,6 +22,7 @@ require "date"
 
 require "qpid_proton/version"
 require "qpid_proton/described"
+require "qpid_proton/strings"
 require "qpid_proton/mapping"
 require "qpid_proton/array"
 require "qpid_proton/hash"

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton/data.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/data.rb b/proton-c/bindings/ruby/lib/qpid_proton/data.rb
index 9644fb4..1e515c8 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton/data.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton/data.rb
@@ -725,7 +725,7 @@ module Qpid
       # If the current node is binary, returns its value. Otherwise, it returns
       # an empty string ("").
       def binary
-        Cproton.pn_data_get_binary(@data)
+        Qpid::Proton::BinaryString.new(Cproton.pn_data_get_binary(@data))
       end
 
       # Puts a unicode string value.
@@ -742,7 +742,7 @@ module Qpid
       # If the current node is a string, returns its value. Otherwise, it
       # returns an empty string ("").
       def string
-        Cproton.pn_data_get_string(@data)
+        Qpid::Proton::UTFString.new(Cproton.pn_data_get_string(@data))
       end
 
       # Puts a symbolic value.

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb b/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
index 841156c..e7e3322 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton/mapping.rb
@@ -110,26 +110,10 @@ module Qpid # :nodoc:
 
     class << STRING
       def put(data, value)
-        # In Ruby 1.9+ we have encoding methods that can check the content of
-        # the string, so use them to see if what we have is unicode. If so,
-        # good! If not, then just treat is as binary.
-        #
-        # No such thing in Ruby 1.8. So there we need to use Iconv to try and
-        # convert it to unicode. If it works, good! But if it raises an
-        # exception then we'll treat it as binary.
-        if RUBY_VERSION >= "1.9"
-          if value.encoding == "UTF-8" || value.force_encoding("UTF-8").valid_encoding?
-            data.string = value.to_s
-          else
-            data.binary = value.to_s
-          end
+        if value.is_a?(Qpid::Proton::UTFString) || Qpid::Proton.is_valid_utf?(value)
+          data.string = value.to_s
         else
-          begin
-            newval = Iconv.new("UTF8//TRANSLIT//IGNORE", "UTF8").iconv(value.to_s)
-            data.string = newval
-          rescue
-            data.binary = value
-          end
+          data.binary = value.to_s
         end
       end
     end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6371e9eb/proton-c/bindings/ruby/lib/qpid_proton/strings.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton/strings.rb b/proton-c/bindings/ruby/lib/qpid_proton/strings.rb
new file mode 100644
index 0000000..dad96ad
--- /dev/null
+++ b/proton-c/bindings/ruby/lib/qpid_proton/strings.rb
@@ -0,0 +1,69 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+module Qpid # :nodoc:
+
+  module Proton # :nodoc:
+
+    def self.is_valid_utf?(value)
+      # In Ruby 1.9+ we have encoding methods that can check the content of
+      # the string, so use them to see if what we have is unicode. If so,
+      # good! If not, then just treat is as binary.
+      #
+      # No such thing in Ruby 1.8. So there we need to use Iconv to try and
+      # convert it to unicode. If it works, good! But if it raises an
+      # exception then we'll treat it as binary.
+      if RUBY_VERSION >= "1.9"
+        return true if (value.encoding == "UTF-8" ||
+                        value.force_encoding("UTF-8").valid_encoding?)
+
+        return false
+      else
+        begin
+          newval = Iconv.new("UTF8//TRANSLIT//IGNORE", "UTF8").iconv(value.to_s)
+          return true
+        rescue
+          return false
+        end
+      end
+    end
+
+    # UTFString lets an application explicitly state that a
+    # string of characters is to be UTF-8 encoded.
+    #
+    class UTFString < ::String
+
+      def initialize(value)
+        if !Qpid::Proton.is_valid_utf?(value)
+          raise RuntimeError.new("invalid UTF string")
+        end
+
+        super(value)
+      end
+
+    end
+
+    # BinaryString lets an application explicitly declare that
+    # a string value represents arbitrary data.
+    #
+    class BinaryString < ::String; end
+
+  end
+
+end


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message