ree_lib 1.0.10 → 1.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -5
- data/lib/ree_lib/packages/ree_json/package/ree_json/functions/from_json.rb +5 -1
- data/lib/ree_lib/packages/ree_json/schemas/ree_json/functions/from_json.schema.json +1 -1
- data/lib/ree_lib/packages/ree_json/spec/ree_json/functions/from_json_spec.rb +4 -0
- data/lib/ree_lib/packages/ree_object/Package.schema.json +8 -0
- data/lib/ree_lib/packages/ree_object/package/ree_object/functions/dump_as_json.rb +103 -0
- data/lib/ree_lib/packages/ree_object/package/ree_object/functions/load_json_dump.rb +77 -0
- data/lib/ree_lib/packages/ree_object/schemas/ree_object/functions/dump_as_json.schema.json +27 -0
- data/lib/ree_lib/packages/ree_object/schemas/ree_object/functions/load_json_dump.schema.json +27 -0
- data/lib/ree_lib/packages/ree_object/spec/ree_object/functions/dump_as_json_spec.rb +56 -0
- data/lib/ree_lib/packages/ree_swagger/spec/functions/build_schema_spec.rb +5 -5
- data/lib/ree_lib/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03420b754f18500299e45b5b611cb72b44693075ec9a93d46481cf891321f69f
|
4
|
+
data.tar.gz: 0b58f43befc8d14d5165b91d741f25fff71965940c735e91ef8440b362d864eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ce6ccf2789d46751b5ba170fa413c0d0ccc4648c4db7cea76e929ea28123bff3dfc427350631432c0297a43d474d79de69e71b6b076861a137b794b6b9f8c6f
|
7
|
+
data.tar.gz: 2cc846873cbc183a9afa1587dff954e2c41fd75a7edbb19929ff2528e18b48c2d74613e482dbf3f80522eccb9a499ef00b094f9a73e368757b27fa9e599938bb
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ree_lib (1.0.
|
4
|
+
ree_lib (1.0.13)
|
5
5
|
binding_of_caller (~> 1.0.0)
|
6
6
|
i18n (~> 1.12.0)
|
7
7
|
loofah (~> 2.18.0)
|
@@ -36,17 +36,15 @@ GEM
|
|
36
36
|
crass (~> 1.0.2)
|
37
37
|
nokogiri (>= 1.5.9)
|
38
38
|
msgpack (1.5.4)
|
39
|
-
nokogiri (1.13.8-x86_64-darwin)
|
40
|
-
racc (~> 1.4)
|
41
39
|
nokogiri (1.13.8-x86_64-linux)
|
42
40
|
racc (~> 1.4)
|
43
|
-
oj (3.13.
|
41
|
+
oj (3.13.21)
|
44
42
|
pg (1.4.2)
|
45
43
|
public_suffix (4.0.7)
|
46
44
|
racc (1.6.0)
|
47
45
|
rainbow (3.1.1)
|
48
46
|
rake (13.0.6)
|
49
|
-
ree (1.0.
|
47
|
+
ree (1.0.4)
|
50
48
|
commander (~> 4.6.0)
|
51
49
|
rexml (3.2.5)
|
52
50
|
rollbar (3.3.1)
|
@@ -7,6 +7,8 @@ class ReeJson::FromJson
|
|
7
7
|
}
|
8
8
|
end
|
9
9
|
|
10
|
+
ParseJsonError = Class.new(StandardError)
|
11
|
+
|
10
12
|
contract(
|
11
13
|
Any,
|
12
14
|
Kwargs[
|
@@ -16,7 +18,7 @@ class ReeJson::FromJson
|
|
16
18
|
symbol_keys?: Bool,
|
17
19
|
RestKeys => Any
|
18
20
|
] => Hash
|
19
|
-
).throws(
|
21
|
+
).throws(ParseJsonError)
|
20
22
|
def call(object, mode: :rails, **opts)
|
21
23
|
options = DEFAULT_OPTIONS
|
22
24
|
.dup
|
@@ -25,5 +27,7 @@ class ReeJson::FromJson
|
|
25
27
|
)
|
26
28
|
|
27
29
|
Oj.load(object, options)
|
30
|
+
rescue ArgumentError, EncodingError
|
31
|
+
raise ParseJsonError.new
|
28
32
|
end
|
29
33
|
end
|
@@ -27,10 +27,18 @@
|
|
27
27
|
"name": "deep_freeze",
|
28
28
|
"schema": "packages/ree_object/schemas/ree_object/functions/deep_freeze.schema.json"
|
29
29
|
},
|
30
|
+
{
|
31
|
+
"name": "dump_as_json",
|
32
|
+
"schema": "packages/ree_object/schemas/ree_object/functions/dump_as_json.schema.json"
|
33
|
+
},
|
30
34
|
{
|
31
35
|
"name": "is_blank",
|
32
36
|
"schema": "packages/ree_object/schemas/ree_object/functions/is_blank.schema.json"
|
33
37
|
},
|
38
|
+
{
|
39
|
+
"name": "load_json_dump",
|
40
|
+
"schema": "packages/ree_object/schemas/ree_object/functions/load_json_dump.schema.json"
|
41
|
+
},
|
34
42
|
{
|
35
43
|
"name": "not_blank",
|
36
44
|
"schema": "packages/ree_object/schemas/ree_object/functions/not_blank.schema.json"
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class ReeObject::DumpAsJson
|
6
|
+
include Ree::FnDSL
|
7
|
+
|
8
|
+
fn :dump_as_json do
|
9
|
+
def_error { RecursiveObjectErr }
|
10
|
+
end
|
11
|
+
|
12
|
+
ARRAY = 'array'
|
13
|
+
HASH = 'hash'
|
14
|
+
PRIMITIVE = 'primitive'
|
15
|
+
OBJECT = 'object'
|
16
|
+
|
17
|
+
BASIC_TYPES = [
|
18
|
+
Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol,
|
19
|
+
Module, Class
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
contract(
|
23
|
+
Any => Or[Hash, ArrayOf[Any], *BASIC_TYPES]
|
24
|
+
).throws(RecursiveObjectErr)
|
25
|
+
def call(obj)
|
26
|
+
recursively_convert(obj, {})
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def recursively_convert(obj, cache)
|
32
|
+
ancestors = obj.class.ancestors
|
33
|
+
|
34
|
+
if ancestors.intersection(BASIC_TYPES).size > 0
|
35
|
+
{
|
36
|
+
'type' => PRIMITIVE,
|
37
|
+
'class' => obj.class.name,
|
38
|
+
'value' => dump_primitive(obj)
|
39
|
+
}
|
40
|
+
elsif obj.is_a?(Array)
|
41
|
+
{
|
42
|
+
'type' => ARRAY,
|
43
|
+
'class' => 'Array',
|
44
|
+
'value' => obj.map { recursively_convert(_1, cache) }
|
45
|
+
}
|
46
|
+
elsif obj.is_a?(Hash)
|
47
|
+
{
|
48
|
+
'type' => HASH,
|
49
|
+
'class' => 'Hash',
|
50
|
+
'value' => convert_hash(obj, cache)
|
51
|
+
}
|
52
|
+
elsif obj.is_a?(Proc)
|
53
|
+
raise ArgumentError, "procs are not supported"
|
54
|
+
else
|
55
|
+
{
|
56
|
+
'type' => OBJECT,
|
57
|
+
'class' => obj.class.name || (raise ArgumentError.new("anonymous classes are not supported")),
|
58
|
+
'value' => convert_object(obj, cache)
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
PRIMITIVE_SET = Set.new([Symbol, Date, Time, DateTime, Module, Class])
|
64
|
+
|
65
|
+
def dump_primitive(val)
|
66
|
+
if PRIMITIVE_SET.include?(val.class)
|
67
|
+
val.to_s
|
68
|
+
else
|
69
|
+
val
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def convert_hash(obj, cache)
|
74
|
+
result = []
|
75
|
+
|
76
|
+
obj.each do |k, v|
|
77
|
+
result << [
|
78
|
+
recursively_convert(k, cache),
|
79
|
+
recursively_convert(v, cache),
|
80
|
+
]
|
81
|
+
end
|
82
|
+
|
83
|
+
result
|
84
|
+
end
|
85
|
+
|
86
|
+
def convert_object(obj, cache)
|
87
|
+
if cache.key?(obj.object_id)
|
88
|
+
raise RecursiveObjectErr, "Recursive object found: #{obj}"
|
89
|
+
end
|
90
|
+
|
91
|
+
cache[obj.object_id] = true
|
92
|
+
result = []
|
93
|
+
|
94
|
+
obj.instance_variables.each do |var|
|
95
|
+
result << [
|
96
|
+
recursively_convert(var, cache),
|
97
|
+
recursively_convert(obj.instance_variable_get(var), cache),
|
98
|
+
]
|
99
|
+
end
|
100
|
+
|
101
|
+
result
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ReeObject::LoadJsonDump
|
4
|
+
include Ree::FnDSL
|
5
|
+
|
6
|
+
fn :load_json_dump
|
7
|
+
|
8
|
+
ARRAY = 'array'
|
9
|
+
HASH = 'hash'
|
10
|
+
PRIMITIVE = 'primitive'
|
11
|
+
OBJECT = 'object'
|
12
|
+
|
13
|
+
contract(Hash => Any)
|
14
|
+
def call(dump)
|
15
|
+
recursively_load(dump)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def recursively_load(dump)
|
21
|
+
case dump['type']
|
22
|
+
when ARRAY
|
23
|
+
dump['value'].map { recursively_load(_1)}
|
24
|
+
when HASH
|
25
|
+
result = {}
|
26
|
+
|
27
|
+
dump['value'].map do |v|
|
28
|
+
result[recursively_load(v[0])] = recursively_load(v[1])
|
29
|
+
end
|
30
|
+
|
31
|
+
result
|
32
|
+
when PRIMITIVE
|
33
|
+
load_primitive(dump['class'], dump['value'])
|
34
|
+
when OBJECT
|
35
|
+
load_object(dump['class'], dump['value'])
|
36
|
+
else
|
37
|
+
raise NotImplementedError, "unsupported type provider '#{dump['type']}'"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def load_primitive(class_str, val)
|
44
|
+
if class_str == 'Class' || class_str == 'Module'
|
45
|
+
return Object.const_get(val)
|
46
|
+
end
|
47
|
+
|
48
|
+
klass = Object.const_get(class_str)
|
49
|
+
|
50
|
+
if klass == Symbol
|
51
|
+
val.to_sym
|
52
|
+
elsif klass == Date
|
53
|
+
Date.parse(val)
|
54
|
+
elsif klass == Time
|
55
|
+
Time.parse(val)
|
56
|
+
elsif klass == DateTime
|
57
|
+
DateTime.parse(val)
|
58
|
+
elsif klass == Module || klass == Class
|
59
|
+
klass
|
60
|
+
else
|
61
|
+
val
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_object(class_str, val)
|
66
|
+
klass = Object.const_get(class_str)
|
67
|
+
obj = klass.allocate
|
68
|
+
|
69
|
+
val.each do |v|
|
70
|
+
var = recursively_load(v[0])
|
71
|
+
value = recursively_load(v[1])
|
72
|
+
obj.instance_variable_set(var, value)
|
73
|
+
end
|
74
|
+
|
75
|
+
obj
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"schema_type": "object",
|
3
|
+
"schema_version": "1.0",
|
4
|
+
"name": "dump_as_json",
|
5
|
+
"path": "packages/ree_object/package/ree_object/functions/dump_as_json.rb",
|
6
|
+
"mount_as": "fn",
|
7
|
+
"class": "ReeObject::DumpAsJson",
|
8
|
+
"factory": null,
|
9
|
+
"methods": [
|
10
|
+
{
|
11
|
+
"doc": "",
|
12
|
+
"throws": [
|
13
|
+
"ReeObject::DumpAsJson::RecursiveObjectErr"
|
14
|
+
],
|
15
|
+
"return": "Or[Hash, ArrayOf[Any], Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol, Module, Class]",
|
16
|
+
"args": [
|
17
|
+
{
|
18
|
+
"arg": "obj",
|
19
|
+
"type": "Any"
|
20
|
+
}
|
21
|
+
]
|
22
|
+
}
|
23
|
+
],
|
24
|
+
"links": [
|
25
|
+
|
26
|
+
]
|
27
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"schema_type": "object",
|
3
|
+
"schema_version": "1.0",
|
4
|
+
"name": "load_json_dump",
|
5
|
+
"path": "packages/ree_object/package/ree_object/functions/load_json_dump.rb",
|
6
|
+
"mount_as": "fn",
|
7
|
+
"class": "ReeObject::LoadJsonDump",
|
8
|
+
"factory": null,
|
9
|
+
"methods": [
|
10
|
+
{
|
11
|
+
"doc": "",
|
12
|
+
"throws": [
|
13
|
+
|
14
|
+
],
|
15
|
+
"return": "Any",
|
16
|
+
"args": [
|
17
|
+
{
|
18
|
+
"arg": "dump",
|
19
|
+
"type": "Hash"
|
20
|
+
}
|
21
|
+
]
|
22
|
+
}
|
23
|
+
],
|
24
|
+
"links": [
|
25
|
+
|
26
|
+
]
|
27
|
+
}
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal = true
|
2
|
+
|
3
|
+
RSpec.describe :dump_as_json do
|
4
|
+
link :dump_as_json, from: :ree_object
|
5
|
+
link :load_json_dump, from: :ree_object
|
6
|
+
|
7
|
+
let(:klass) {
|
8
|
+
class TestObjCLass
|
9
|
+
attr_reader :integer, :string, :array, :hash, :object, :klass, :module
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@integer = 1
|
13
|
+
@string = 'string'
|
14
|
+
@array = [1, 'string', 3, { 'name' => 'John'}]
|
15
|
+
|
16
|
+
@hash = {
|
17
|
+
id: 1,
|
18
|
+
'test' => 2,
|
19
|
+
nested: {
|
20
|
+
some_value: 1,
|
21
|
+
another_value: 2
|
22
|
+
},
|
23
|
+
name: 'name'
|
24
|
+
}
|
25
|
+
|
26
|
+
@object = Object.new
|
27
|
+
@klass = Object
|
28
|
+
@module = Module
|
29
|
+
|
30
|
+
@object.instance_exec do
|
31
|
+
@name = 'John'
|
32
|
+
@last_name = 'Doe'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
TestObjCLass
|
38
|
+
}
|
39
|
+
|
40
|
+
context "object" do
|
41
|
+
it {
|
42
|
+
orig_obj = klass.new
|
43
|
+
result = dump_as_json([orig_obj])
|
44
|
+
obj = load_json_dump(result).first
|
45
|
+
|
46
|
+
expect(obj.integer).to eq(orig_obj.integer)
|
47
|
+
expect(obj.string).to eq(orig_obj.string)
|
48
|
+
expect(obj.array).to eq(orig_obj.array)
|
49
|
+
expect(obj.hash).to eq(orig_obj.hash)
|
50
|
+
expect(obj.klass).to eq(orig_obj.klass)
|
51
|
+
expect(obj.module).to eq(orig_obj.module)
|
52
|
+
expect(obj.object.instance_variable_get(:@name)).to eq(orig_obj.object.instance_variable_get(:@name))
|
53
|
+
expect(obj.object.instance_variable_get(:@last_name)).to eq(orig_obj.object.instance_variable_get(:@last_name))
|
54
|
+
}
|
55
|
+
end
|
56
|
+
end
|
@@ -31,10 +31,10 @@ RSpec.describe :build_schema do
|
|
31
31
|
},
|
32
32
|
components: {
|
33
33
|
securitySchemes: {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
ApiKeyAuth: {
|
35
|
+
in: "header",
|
36
|
+
name: "Authorization",
|
37
|
+
type: "apiKey",
|
38
38
|
}
|
39
39
|
}
|
40
40
|
},
|
@@ -45,7 +45,7 @@ RSpec.describe :build_schema do
|
|
45
45
|
'/version' => {
|
46
46
|
get: {
|
47
47
|
security: [
|
48
|
-
{
|
48
|
+
{ApiKeyAuth: []}
|
49
49
|
],
|
50
50
|
responses: {
|
51
51
|
200 => {
|
data/lib/ree_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ree_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ree
|
@@ -1035,13 +1035,17 @@ files:
|
|
1035
1035
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
|
1036
1036
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_dup.rb
|
1037
1037
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
|
1038
|
+
- lib/ree_lib/packages/ree_object/package/ree_object/functions/dump_as_json.rb
|
1038
1039
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/is_blank.rb
|
1040
|
+
- lib/ree_lib/packages/ree_object/package/ree_object/functions/load_json_dump.rb
|
1039
1041
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/not_blank.rb
|
1040
1042
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/to_obj.rb
|
1041
1043
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/as_json.schema.json
|
1042
1044
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/deep_dup.schema.json
|
1043
1045
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/deep_freeze.schema.json
|
1046
|
+
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/dump_as_json.schema.json
|
1044
1047
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/is_blank.schema.json
|
1048
|
+
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/load_json_dump.schema.json
|
1045
1049
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/not_blank.schema.json
|
1046
1050
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/to_hash.schema.json
|
1047
1051
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/to_obj.schema.json
|
@@ -1049,6 +1053,7 @@ files:
|
|
1049
1053
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/as_json_spec.rb
|
1050
1054
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/deep_dup_spec.rb
|
1051
1055
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/deep_freeze_spec.rb
|
1056
|
+
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/dump_as_json_spec.rb
|
1052
1057
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/is_blank_spec.rb
|
1053
1058
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/not_blank_spec.rb
|
1054
1059
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/to_obj_spec.rb
|