code-ruby 3.1.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bin/code +97 -20
- data/lib/code/concerns/shared.rb +331 -15
- data/lib/code/format.rb +15 -1
- data/lib/code/network.rb +87 -0
- data/lib/code/node/call.rb +79 -2
- data/lib/code/node/call_argument.rb +14 -0
- data/lib/code/node/code.rb +5 -4
- data/lib/code/node/function_parameter.rb +7 -4
- data/lib/code/node/list.rb +29 -1
- data/lib/code/object/base_64.rb +132 -6
- data/lib/code/object/boolean.rb +60 -0
- data/lib/code/object/class.rb +138 -2
- data/lib/code/object/code.rb +111 -3
- data/lib/code/object/context.rb +57 -1
- data/lib/code/object/cryptography.rb +63 -0
- data/lib/code/object/date.rb +13339 -462
- data/lib/code/object/decimal.rb +1725 -0
- data/lib/code/object/dictionary.rb +1790 -11
- data/lib/code/object/duration.rb +28 -0
- data/lib/code/object/function.rb +261 -23
- data/lib/code/object/global.rb +534 -1
- data/lib/code/object/html.rb +179 -7
- data/lib/code/object/http.rb +244 -14
- data/lib/code/object/ics.rb +75 -13
- data/lib/code/object/identifier_list.rb +17 -2
- data/lib/code/object/integer.rb +1937 -2
- data/lib/code/object/json.rb +75 -1
- data/lib/code/object/list.rb +3383 -10
- data/lib/code/object/nothing.rb +53 -0
- data/lib/code/object/number.rb +110 -0
- data/lib/code/object/parameter.rb +140 -0
- data/lib/code/object/range.rb +576 -14
- data/lib/code/object/smtp.rb +95 -12
- data/lib/code/object/string.rb +944 -3
- data/lib/code/object/super.rb +10 -1
- data/lib/code/object/time.rb +13358 -498
- data/lib/code/object/url.rb +65 -0
- data/lib/code/object.rb +543 -0
- data/lib/code/parser.rb +161 -24
- data/lib/code-ruby.rb +3 -0
- data/lib/code.rb +30 -3
- metadata +135 -84
- data/.github/dependabot.yml +0 -15
- data/.github/workflows/ci.yml +0 -38
- data/.gitignore +0 -30
- data/.node-version +0 -1
- data/.npm-version +0 -1
- data/.prettierignore +0 -2
- data/.rspec +0 -1
- data/.rubocop.yml +0 -140
- data/.ruby-version +0 -1
- data/.tool-versions +0 -3
- data/AGENTS.md +0 -43
- data/Gemfile +0 -22
- data/Gemfile.lock +0 -292
- data/Rakefile +0 -5
- data/bin/bundle +0 -123
- data/bin/bundle-audit +0 -31
- data/bin/bundler-audit +0 -31
- data/bin/dorian +0 -31
- data/bin/rspec +0 -31
- data/bin/rubocop +0 -31
- data/bin/test +0 -5
- data/code-ruby.gemspec +0 -34
- data/docs/precedence.txt +0 -36
- data/package-lock.json +0 -14
- data/package.json +0 -7
- data/spec/bin/code_spec.rb +0 -48
- data/spec/code/format_spec.rb +0 -153
- data/spec/code/node/call_spec.rb +0 -11
- data/spec/code/object/boolean_spec.rb +0 -18
- data/spec/code/object/cryptography_spec.rb +0 -25
- data/spec/code/object/decimal_spec.rb +0 -50
- data/spec/code/object/dictionary_spec.rb +0 -98
- data/spec/code/object/function_spec.rb +0 -268
- data/spec/code/object/http_spec.rb +0 -33
- data/spec/code/object/ics_spec.rb +0 -50
- data/spec/code/object/integer_spec.rb +0 -42
- data/spec/code/object/list_spec.rb +0 -22
- data/spec/code/object/nothing_spec.rb +0 -14
- data/spec/code/object/range_spec.rb +0 -23
- data/spec/code/object/string_spec.rb +0 -26
- data/spec/code/parser/boolean_spec.rb +0 -11
- data/spec/code/parser/chained_call_spec.rb +0 -16
- data/spec/code/parser/dictionary_spec.rb +0 -18
- data/spec/code/parser/function_spec.rb +0 -16
- data/spec/code/parser/group_spec.rb +0 -11
- data/spec/code/parser/if_modifier_spec.rb +0 -18
- data/spec/code/parser/list_spec.rb +0 -17
- data/spec/code/parser/number_spec.rb +0 -11
- data/spec/code/parser/string_spec.rb +0 -20
- data/spec/code/parser_spec.rb +0 -52
- data/spec/code/type_spec.rb +0 -21
- data/spec/code_spec.rb +0 -717
- data/spec/spec_helper.rb +0 -21
- data/spec/zeitwerk/loader_spec.rb +0 -7
data/lib/code/object/json.rb
CHANGED
|
@@ -3,6 +3,45 @@
|
|
|
3
3
|
class Code
|
|
4
4
|
class Object
|
|
5
5
|
class Json < Object
|
|
6
|
+
CLASS_DOCUMENTATION = {
|
|
7
|
+
name: "Json",
|
|
8
|
+
description: "parses json text and serializes values as json text.",
|
|
9
|
+
examples: [
|
|
10
|
+
"Json.parse(\"{}\")",
|
|
11
|
+
"Json.generate({ a: 1 })",
|
|
12
|
+
"Json.generate([1, 2], { pretty: true })"
|
|
13
|
+
]
|
|
14
|
+
}.freeze
|
|
15
|
+
CLASS_FUNCTIONS = {
|
|
16
|
+
"parse" => {
|
|
17
|
+
name: "parse",
|
|
18
|
+
description: "returns a value parsed from json text, or nothing when parsing fails.",
|
|
19
|
+
examples: [
|
|
20
|
+
"Json.parse(\"{}\")",
|
|
21
|
+
"Json.parse(\"[1,2]\")",
|
|
22
|
+
"Json.parse(\"bad json\")"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"generate" => {
|
|
26
|
+
name: "generate",
|
|
27
|
+
description: "returns json text for a value, optionally formatted for readability.",
|
|
28
|
+
examples: [
|
|
29
|
+
"Json.generate({ a: 1 })",
|
|
30
|
+
"Json.generate([1, 2])",
|
|
31
|
+
"Json.generate({ a: 1 }, { pretty: true })"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
}.freeze
|
|
35
|
+
|
|
36
|
+
def self.function_documentation(scope)
|
|
37
|
+
return CLASS_FUNCTIONS if scope == :class
|
|
38
|
+
|
|
39
|
+
{}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
MAX_NESTING = 100
|
|
43
|
+
MAX_ITEMS = 10_000
|
|
44
|
+
|
|
6
45
|
def self.call(**args)
|
|
7
46
|
code_operator = args.fetch(:operator, nil).to_code
|
|
8
47
|
code_arguments = args.fetch(:arguments, []).to_code
|
|
@@ -12,6 +51,14 @@ class Code
|
|
|
12
51
|
when "parse"
|
|
13
52
|
sig(args) { String }
|
|
14
53
|
code_parse(code_value)
|
|
54
|
+
when "generate"
|
|
55
|
+
sig(args) { [Object, { pretty: Object::Boolean.maybe }] }
|
|
56
|
+
|
|
57
|
+
if code_arguments.code_second.something?
|
|
58
|
+
code_generate(code_value, pretty: code_arguments.code_second.code_get(:pretty))
|
|
59
|
+
else
|
|
60
|
+
code_generate(code_value)
|
|
61
|
+
end
|
|
15
62
|
else
|
|
16
63
|
super
|
|
17
64
|
end
|
|
@@ -19,11 +66,38 @@ class Code
|
|
|
19
66
|
|
|
20
67
|
def self.code_parse(value)
|
|
21
68
|
code_value = value.to_code
|
|
69
|
+
::Code.ensure_input_size!(code_value.raw, label: "json")
|
|
22
70
|
|
|
23
|
-
::JSON.parse(code_value.raw)
|
|
71
|
+
parsed = ::JSON.parse(code_value.raw)
|
|
72
|
+
validate_shape!(parsed)
|
|
73
|
+
parsed.to_code
|
|
24
74
|
rescue JSON::ParserError
|
|
25
75
|
Nothing.new
|
|
26
76
|
end
|
|
77
|
+
|
|
78
|
+
def self.code_generate(value, pretty: nil)
|
|
79
|
+
value.to_code.code_to_json(pretty: pretty)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.validate_shape!(value, depth: 0, count: 0)
|
|
83
|
+
raise Error, "json is too deeply nested" if depth > MAX_NESTING
|
|
84
|
+
raise Error, "json has too many items" if count > MAX_ITEMS
|
|
85
|
+
|
|
86
|
+
case value
|
|
87
|
+
when ::Array
|
|
88
|
+
count += value.size
|
|
89
|
+
value.each do |item|
|
|
90
|
+
count = validate_shape!(item, depth: depth + 1, count: count)
|
|
91
|
+
end
|
|
92
|
+
when ::Hash
|
|
93
|
+
count += value.size
|
|
94
|
+
value.each_value do |item|
|
|
95
|
+
count = validate_shape!(item, depth: depth + 1, count: count)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
count
|
|
100
|
+
end
|
|
27
101
|
end
|
|
28
102
|
end
|
|
29
103
|
end
|