camille 0.5.14 → 0.5.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2c6dd8d04bd084f86bdfb6dc20b8e7272f3bea5f041c39ff5c48f07c1d8184b
4
- data.tar.gz: 7feb388c52715c48fc602bcc58274a9e667c6ac59290d8c3c7b52ff928def0d1
3
+ metadata.gz: c64a49ba269a9488e2990dce715d59e21ec603bb09453bc07b48f1b29cf80183
4
+ data.tar.gz: 482a7672c68154b5ab94c68318b2672f02f76793ad08372570a1b5ec4049e7b8
5
5
  SHA512:
6
- metadata.gz: 6d2e64d5caa85962f42f04d19b9bec5b7573166616b415f899a69e1752698f10d85961ac270c6397f78377d635904dad884fa62135c58c9c8060e0e5a57a4d0a
7
- data.tar.gz: c6920ed8114a4807c837e8deeb813653ccb20efc44fe246cae3139d09068f16497d3ff378d97282207a6f21df7837fdf668a9ccf44d1c918cb9e4d144caec55b
6
+ metadata.gz: d6ba48e7bc89d06d8ad92eb375cac5d02d783fc35c5732215f068630b57918a5b0bfaccc89b76d49983b384fbac31e2bbbe208d14526ea37489864e2cb2cee67
7
+ data.tar.gz: cb52ae9bdfc4445c098040b576d1e6910da8b699a615bfea339ed2bf24b0b180408d67a53b03a1c6259aadf3bda9f407d25c5fc17a12fbd22fb42c0aa0b20637
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.16
4
+
5
+ ### Fixed
6
+
7
+ * Record keys will no longer be case converted
8
+
9
+ ## 0.5.15
10
+
11
+ ### Added
12
+
13
+ * Added key conversion cache
14
+
15
+ ### Changed
16
+
17
+ * When generating TS, endpoints are now sorted by name
18
+
3
19
  ## 0.5.14
4
20
 
5
21
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- camille (0.5.13)
4
+ camille (0.5.15)
5
5
  rails (>= 6.1, < 8)
6
6
 
7
7
  GEM
@@ -90,20 +90,20 @@ GEM
90
90
  net-imap
91
91
  net-pop
92
92
  net-smtp
93
- marcel (1.0.2)
93
+ marcel (1.0.4)
94
94
  method_source (1.0.0)
95
95
  mini_mime (1.1.5)
96
96
  minitest (5.17.0)
97
- net-imap (0.4.7)
97
+ net-imap (0.4.16)
98
98
  date
99
99
  net-protocol
100
100
  net-pop (0.1.2)
101
101
  net-protocol
102
102
  net-protocol (0.2.2)
103
103
  timeout
104
- net-smtp (0.4.0)
104
+ net-smtp (0.5.0)
105
105
  net-protocol
106
- nio4r (2.6.1)
106
+ nio4r (2.7.3)
107
107
  nokogiri (1.14.2-x86_64-linux)
108
108
  racc (~> 1.4)
109
109
  racc (1.6.2)
@@ -0,0 +1,94 @@
1
+ require "benchmark/ips"
2
+
3
+ def array_object
4
+ [1, 2, 3]
5
+ end
6
+
7
+ def hash_object
8
+ {
9
+ a: 1,
10
+ b: 2,
11
+ c: 3
12
+ }
13
+ end
14
+
15
+ class PlainObject
16
+ attr_reader :a, :b, :c
17
+
18
+ def initialize a, b, c
19
+ @a = a
20
+ @b = b
21
+ @c = c
22
+ end
23
+ end
24
+
25
+ def plain_object
26
+ PlainObject.new(1, 2, 3)
27
+ end
28
+
29
+ StructObject = Struct.new(:a, :b, :c)
30
+
31
+ def struct_object
32
+ StructObject.new(1, 2, 3)
33
+ end
34
+
35
+ DataObject = Data.define(:a, :b, :c)
36
+
37
+ def data_object
38
+ DataObject.new(1, 2, 3)
39
+ end
40
+
41
+ Benchmark.ips do |x|
42
+ x.report("parallel assignment") do
43
+ a, b, c = array_object
44
+ end
45
+
46
+ x.report("array access") do
47
+ arr = array_object
48
+ a = arr[0]
49
+ b = arr[1]
50
+ c = arr[2]
51
+ end
52
+
53
+ x.report("hash access") do
54
+ hsh = hash_object
55
+ a = hsh[:a]
56
+ b = hsh[:b]
57
+ c = hsh[:c]
58
+ end
59
+
60
+ x.report("hash destructuring") do
61
+ hash_object => {a:, b:, c:}
62
+ end
63
+
64
+ x.report("plain object") do
65
+ obj = plain_object
66
+ a = obj.a
67
+ b = obj.b
68
+ c = obj.c
69
+ end
70
+
71
+ x.report("struct object") do
72
+ obj = struct_object
73
+ a = obj.a
74
+ b = obj.b
75
+ c = obj.c
76
+ end
77
+
78
+ x.report("struct object destructuring") do
79
+ struct_object => {a:, b:, c:}
80
+ end
81
+
82
+ x.report("data object") do
83
+ obj = data_object
84
+ a = obj.a
85
+ b = obj.b
86
+ c = obj.c
87
+ end
88
+
89
+ x.report("data object destructuring") do
90
+ data_object => {a:, b:, c:}
91
+ end
92
+
93
+ x.compare!
94
+ end
@@ -2,21 +2,30 @@
2
2
  module Camille
3
3
  class Configuration
4
4
  class << self
5
- def ts_header= string
6
- @ts_header = string
7
- end
5
+ attr_reader :response_key_converter, :params_key_converter
6
+ attr_accessor :ts_header, :ts_location
7
+
8
+ def load_default_configurations
9
+ self.response_key_converter = lambda do |string|
10
+ string.camelize(:lower)
11
+ end
8
12
 
9
- def ts_header
10
- @ts_header
13
+ self.params_key_converter = lambda do |string|
14
+ string.underscore
15
+ end
11
16
  end
12
17
 
13
- def ts_location= string
14
- @ts_location = string
18
+ def response_key_converter= lambda
19
+ @response_key_converter = lambda
20
+ Camille::Type.define_method :convert_response_key, &lambda
15
21
  end
16
22
 
17
- def ts_location
18
- @ts_location
23
+ def params_key_converter= lambda
24
+ @params_key_converter = lambda
25
+ Camille::Type.define_method :convert_params_key, &lambda
19
26
  end
27
+
28
+ Camille::Configuration.load_default_configurations
20
29
  end
21
30
  end
22
31
  end
@@ -25,9 +25,6 @@ module Camille
25
25
  Camille::TypeErrorPrinter.new(error).print(string_io)
26
26
  raise TypeError.new("\nType check failed for response.\n#{string_io.string}")
27
27
  else
28
- if transformed.is_a? Hash
29
- transformed.deep_transform_keys!{|k| k.to_s.camelize(:lower)}
30
- end
31
28
  super(json: transformed)
32
29
  end
33
30
  else
@@ -45,7 +42,7 @@ module Camille
45
42
  Camille::Loader.check_and_raise_exception
46
43
  if endpoint = camille_endpoint
47
44
  begin
48
- params.deep_transform_keys!{|key| key.to_s.underscore}
45
+ params.deep_transform_keys!{|key| Camille::Configuration.params_key_converter.call(key.to_s)}
49
46
  result = super
50
47
  # When there's no `render` call, Rails will return status 204
51
48
  if response.status == 204
@@ -16,9 +16,9 @@ module Camille
16
16
  raise UnknownResponseError.new("Endpoint lacking a `response` definition.")
17
17
  end
18
18
  if @params_type
19
- "#{ActiveSupport::Inflector.camelize @name, false}(params: #{@params_type.literal}): Promise<#{@response_type.literal}>"
19
+ "#{Camille::Configuration.response_key_converter.call(@name.to_s)}(params: #{@params_type.literal}): Promise<#{@response_type.literal}>"
20
20
  else
21
- "#{ActiveSupport::Inflector.camelize @name, false}(): Promise<#{@response_type.literal}>"
21
+ "#{Camille::Configuration.response_key_converter.call(@name.to_s)}(): Promise<#{@response_type.literal}>"
22
22
  end
23
23
  end
24
24
 
@@ -0,0 +1,19 @@
1
+ module Camille
2
+ class KeyConverter
3
+ class << self
4
+ RESPONSE_KEY_CACHE = {}
5
+
6
+ def convert_response_key key
7
+ if in_cache = RESPONSE_KEY_CACHE[key]
8
+ in_cache
9
+ else
10
+ RESPONSE_KEY_CACHE[key] = Camille::Configuration.response_key_converter.call(key.to_s)
11
+ end
12
+ end
13
+
14
+ def convert_params_key key
15
+ Camille::Configuration.params_key_converter.call(key)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Camille
2
+ class ObjectHash < Hash
3
+ def as_json options = nil
4
+ transform_keys{|k, _| Camille::KeyConverter.convert_response_key(k)}.as_json
5
+ end
6
+ end
7
+ end
@@ -30,7 +30,7 @@ module Camille
30
30
 
31
31
  private
32
32
  def keys_in_literal
33
- @keys.map{|k| "\"#{k.to_s.camelize(:lower)}\""}.join(' | ')
33
+ @keys.map{|k| "\"#{Camille::Configuration.response_key_converter.call(k.to_s)}\""}.join(' | ')
34
34
  end
35
35
 
36
36
  end
@@ -17,7 +17,7 @@ module Camille
17
17
  def self.literal_lines
18
18
  [
19
19
  Camille::Line.new('{'),
20
- *endpoints.map do |k, e|
20
+ *endpoints.sort_by{|k, e| k}.map do |k, e|
21
21
  Camille::Line.new("#{e.function},")
22
22
  end.map(&:do_indent),
23
23
  Camille::Line.new('}')
@@ -31,13 +31,13 @@ module Camille
31
31
  tree.map do |key, value|
32
32
  if value.is_a? ::Hash
33
33
  [
34
- Camille::Line.new("#{ActiveSupport::Inflector.camelize(key, false)}: {"),
34
+ Camille::Line.new("#{Camille::Configuration.response_key_converter.call(key.to_s)}: {"),
35
35
  tree_literal_lines(value).map(&:do_indent),
36
36
  Camille::Line.new('},')
37
37
  ]
38
38
  else
39
39
  value.literal_lines.tap do |lines|
40
- lines.first.prepend("#{ActiveSupport::Inflector.camelize(key, false)}: ")
40
+ lines.first.prepend("#{Camille::Configuration.response_key_converter.call(key.to_s)}: ")
41
41
  lines.last.append(',')
42
42
  end
43
43
  end
@@ -32,7 +32,7 @@ module Camille
32
32
  transformed = transform_and_check_results.map do |key, (error, transformed)|
33
33
  [key, transformed]
34
34
  end.to_h
35
- [nil, transformed]
35
+ [nil, Camille::ObjectHash[transformed]]
36
36
  else
37
37
  [Camille::TypeError.new(**errors.to_h), nil]
38
38
  end
@@ -48,7 +48,7 @@ module Camille
48
48
  private
49
49
  def normalize_fields fields
50
50
  fields.map do |key, value|
51
- check_case_conversion_safe key
51
+ check_key_conversion_safe key
52
52
  type = Camille::Type.instance(value)
53
53
  if key.end_with?('?')
54
54
  new_key = remove_question_mark(key)
@@ -65,13 +65,13 @@ module Camille
65
65
  end
66
66
 
67
67
  def literal_key key
68
- "#{ActiveSupport::Inflector.camelize key.to_s, false}#{@optional_keys.include?(key) ? '?' : ''}"
68
+ "#{Camille::Configuration.response_key_converter.call(key.to_s)}#{@optional_keys.include?(key) ? '?' : ''}"
69
69
  end
70
70
 
71
- def check_case_conversion_safe sym
71
+ def check_key_conversion_safe sym
72
72
  str = sym.to_s
73
- if str != str.camelize.underscore
74
- raise ArgumentError.new("Only keys satisfying `key.to_s == key.to_s.camelize.underscore` can be used.")
73
+ if str != Camille::Configuration.params_key_converter.call(Camille::Configuration.response_key_converter.call(str))
74
+ raise ArgumentError.new("Only keys satisfying `key == key.to_s.camelize.underscore` can be used.")
75
75
  end
76
76
  end
77
77
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Camille
4
- VERSION = "0.5.14"
4
+ VERSION = "0.5.16"
5
5
  end
data/lib/camille.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "active_support"
4
4
 
5
5
  require_relative "camille/version"
6
+ require_relative "camille/object_hash"
6
7
  require_relative "camille/basic_type"
7
8
  require_relative "camille/types"
8
9
  require_relative "camille/types/number"
@@ -37,6 +38,7 @@ require_relative "camille/loader"
37
38
  require_relative "camille/configuration"
38
39
  require_relative "camille/code_generator"
39
40
  require_relative "camille/main_controller"
41
+ require_relative "camille/key_converter"
40
42
 
41
43
  require "rails/generators"
42
44
  require_relative "camille/generators/install_generator"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.14
4
+ version: 0.5.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alyssa
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-17 00:00:00.000000000 Z
11
+ date: 2024-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -43,6 +43,7 @@ files:
43
43
  - Gemfile.lock
44
44
  - README.md
45
45
  - Rakefile
46
+ - benchmarks/returning_multiple_values.rb
46
47
  - bin/console
47
48
  - bin/setup
48
49
  - gemfiles/rails-6.1
@@ -62,9 +63,11 @@ files:
62
63
  - lib/camille/generators/templates/schema_template.erb
63
64
  - lib/camille/generators/templates/type_template.erb
64
65
  - lib/camille/generators/type_generator.rb
66
+ - lib/camille/key_converter.rb
65
67
  - lib/camille/line.rb
66
68
  - lib/camille/loader.rb
67
69
  - lib/camille/main_controller.rb
70
+ - lib/camille/object_hash.rb
68
71
  - lib/camille/pick_and_omit.rb
69
72
  - lib/camille/railtie.rb
70
73
  - lib/camille/schema.rb
@@ -98,7 +101,7 @@ licenses:
98
101
  metadata:
99
102
  homepage_uri: https://github.com/onyxblade/camille
100
103
  source_code_uri: https://github.com/onyxblade/camille
101
- post_install_message:
104
+ post_install_message:
102
105
  rdoc_options: []
103
106
  require_paths:
104
107
  - lib
@@ -113,8 +116,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
116
  - !ruby/object:Gem::Version
114
117
  version: '0'
115
118
  requirements: []
116
- rubygems_version: 3.2.33
117
- signing_key:
119
+ rubygems_version: 3.5.22
120
+ signing_key:
118
121
  specification_version: 4
119
122
  summary: Typed API schema for Rails with TypeScript codegen
120
123
  test_files: []