thrift-utf8_json 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e29a73f29d8dd1a398ef60a9fdc96fa72746350f
4
+ data.tar.gz: 2ad28e968450ac8a1187808256134247977e05ca
5
+ SHA512:
6
+ metadata.gz: d9fb7bedf7ff932eae33b737a34c56e4f683e6d493b560d4db80cbeff1167c36b5ad9597a8353f8a163e8aef9437ea2987171f1daa960c0cabcb9f691b40c6b3
7
+ data.tar.gz: abd75a801da112034e8d072a11859189860929b3d3613dabe1d3a7c2149ed9db278b33c499f8cf7148decd5662de29f238e134e7298b30f0a633666d4fe87bbc
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thrift-json.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 ahawkins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ THRIFT:=vendor/gen-rb/test_types.rb
2
+
3
+ $(THRIFT): test.thrift
4
+ mkdir -p $(@D)
5
+ thrift -o vendor --gen rb test.thrift
6
+
7
+ .PHONY: test
8
+ test: $(THRIFT)
9
+ bundle exec rake test
10
+
11
+ .PHONY:
12
+ release:
13
+ bundle exec rake release
@@ -0,0 +1,54 @@
1
+ # thrift-ut8\_json
2
+
3
+ This gem contains patches for working with [thrift][] struts & JSON
4
+ serialization/deserialization. Most importantly it contains a fix for
5
+ encoding handling when using generated thrift JSON blobs inside other
6
+ objects (e.g. persisting in a data store). This bug is documented in
7
+ the [acceptance test][].
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'thrift-utf8_json'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install thrift-utf8_json
24
+
25
+ ## Usage
26
+
27
+ `thrift-utf8_json` adds two classes to the `Thrift` module:
28
+ `Thrift::JsonDerserializer` & `Thrift::JsonSerializer`. They mimic the
29
+ interface of `Thirft::Deserializer` & `Thrift::Serializer` except they
30
+ require no arguments to `initialize`. Here's an example:
31
+
32
+ ```ruby
33
+ require 'thrift-utf8_json'
34
+
35
+ struct = SomeThriftStruct.new
36
+
37
+ json = Thrift::JsonSerializer.new.serialize(struct)
38
+ deserialized = Thrift::JsonDeserializer.new.deserialize(SomeThriftStruct.new, json)
39
+ ```
40
+
41
+ ## Testing
42
+
43
+ $ make test
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/saltside/thrift-utf8_json-ruby/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
52
+
53
+ [thrift]: https://thrift.apache.org
54
+ [acceptance test]: test/acceptance_test.rb
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = Rake::FileList['test/**/*_test.rb']
6
+ end
7
+
8
+ task default: :test
@@ -0,0 +1 @@
1
+ require 'thrift/utf8_json'
@@ -0,0 +1,32 @@
1
+ require 'thrift/utf8_json/version'
2
+ require 'thrift'
3
+
4
+ module Thrift
5
+ class Utf8JsonProtocol < ::Thrift::JsonProtocol
6
+ def read_string
7
+ super.force_encoding('UTF-8')
8
+ end
9
+ end
10
+
11
+ class Utf8JsonProtocolFactory
12
+ def get_protocol(trans)
13
+ return Utf8JsonProtocol.new(trans)
14
+ end
15
+ end
16
+
17
+ class JsonSerializer < Serializer
18
+ def initialize
19
+ super Utf8JsonProtocolFactory.new
20
+ end
21
+
22
+ def serialize(base)
23
+ super.force_encoding('UTF-8')
24
+ end
25
+ end
26
+
27
+ class JsonDeserializer < Deserializer
28
+ def initialize
29
+ super Utf8JsonProtocolFactory.new
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Thrift
2
+ module Utf8Json
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ struct SimpleStruct {
2
+ 1: required string message
3
+ }
@@ -0,0 +1,71 @@
1
+ require_relative 'test_helper'
2
+
3
+ class AcceptanceTest < MiniTest::Unit::TestCase
4
+ def test_round_trips_itself_with_proper_encodings
5
+ struct = SimpleStruct.new message: utf8_string
6
+ assert_utf8 struct.message
7
+
8
+ json = Thrift::JsonSerializer.new.serialize(struct)
9
+ assert_utf8 json
10
+
11
+ deserialized = Thrift::JsonDeserializer.new.deserialize(SimpleStruct.new, json)
12
+ assert_equal struct.message, deserialized.message
13
+ assert_utf8 struct.message
14
+ end
15
+
16
+ def test_bug_exists
17
+ struct = SimpleStruct.new message: utf8_string
18
+ assert_utf8 struct.message
19
+
20
+ serializer = Thrift::Serializer.new Thrift::JsonProtocolFactory.new
21
+ json = serializer.serialize struct
22
+
23
+ # Thrift uses a binary encoding when generating the serialized string.
24
+ # Then resulting generated string is then in a different encoding.
25
+ assert_equal Encoding::ASCII_8BIT, json.encoding
26
+
27
+ # This causes problems for other things that require correct encodings.
28
+ # e.g. JSON.dump. The previously generated string contains bytes that
29
+ # are not represented in ASCII 8 bit.
30
+ assert_raises Encoding::UndefinedConversionError do
31
+ JSON.dump([json])
32
+ end
33
+ end
34
+
35
+ def test_bug_fix
36
+ struct = SimpleStruct.new message: utf8_string
37
+ assert_utf8 struct.message
38
+
39
+ serializer = Thrift::Serializer.new Thrift::JsonProtocolFactory.new
40
+ json = serializer.serialize struct
41
+
42
+ # This fixes the problem mentioned in the previous test. Previously
43
+ # unrecognized bytes will be propertly handled in UTF8
44
+ utf8_json = json.force_encoding('UTF-8')
45
+
46
+ # The correctly encoded JSON string should now work with encoding
47
+ # specific code
48
+ dumped = JSON.dump([ utf8_json ])
49
+
50
+ # Take the thrift JSON blob and load it. This blog should deserialize
51
+ # into the original thrift object with correct string encodings.
52
+ # The UTF8 protocol factory will read strings in json back in the correct
53
+ # encoding as well.
54
+ serialized = JSON.load(dumped).first
55
+ deserializer = Thrift::Deserializer.new Thrift::Utf8JsonProtocolFactory.new
56
+ deserialized = deserializer.deserialize(SimpleStruct.new, serialized)
57
+
58
+ assert_equal struct.message, deserialized.message
59
+ assert_utf8 deserialized.message
60
+ end
61
+
62
+ private
63
+
64
+ def assert_utf8(string)
65
+ assert_equal Encoding::UTF_8, string.encoding, 'Non UTF-8 string'
66
+ end
67
+
68
+ def utf8_string
69
+ "Hi, I'am a ☃ !!"
70
+ end
71
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+
3
+ root = File.expand_path '../..', __FILE__
4
+
5
+ $LOAD_PATH << "#{root}/vendor/gen-rb"
6
+
7
+ require 'thrift-utf8_json'
8
+
9
+ Thrift.type_checking = true
10
+
11
+ require 'json'
12
+
13
+ require 'test_types'
14
+ require 'minitest/autorun'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thrift/utf8_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thrift-utf8_json"
8
+ spec.version = Thrift::Utf8Json::VERSION
9
+ spec.authors = ["ahawkins"]
10
+ spec.email = ["adam@hawkins.io"]
11
+ spec.summary = %q{Helpful patches for serializing/deserializing Thrift objeects}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/saltside/thrift-utf_8json-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thrift"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,9 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.2)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'test_types'
9
+
@@ -0,0 +1,25 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.2)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+
9
+ class SimpleStruct
10
+ include ::Thrift::Struct, ::Thrift::Struct_Union
11
+ MESSAGE = 1
12
+
13
+ FIELDS = {
14
+ MESSAGE => {:type => ::Thrift::Types::STRING, :name => 'message'}
15
+ }
16
+
17
+ def struct_fields; FIELDS; end
18
+
19
+ def validate
20
+ raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field message is unset!') unless @message
21
+ end
22
+
23
+ ::Thrift::Struct.generate_accessors self
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrift-utf8_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ahawkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thrift
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: ''
56
+ email:
57
+ - adam@hawkins.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - Makefile
66
+ - README.md
67
+ - Rakefile
68
+ - lib/thrift-utf8_json.rb
69
+ - lib/thrift/utf8_json.rb
70
+ - lib/thrift/utf8_json/version.rb
71
+ - test.thrift
72
+ - test/acceptance_test.rb
73
+ - test/test_helper.rb
74
+ - thrift-json.gemspec
75
+ - vendor/gen-rb/test_constants.rb
76
+ - vendor/gen-rb/test_types.rb
77
+ homepage: https://github.com/saltside/thrift-utf_8json-ruby
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Helpful patches for serializing/deserializing Thrift objeects
101
+ test_files:
102
+ - test/acceptance_test.rb
103
+ - test/test_helper.rb