typelizer 0.13.1 → 0.15.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/CHANGELOG.md +31 -1
- data/README.md +5 -5
- data/lib/typelizer/config.rb +1 -1
- data/lib/typelizer/dsl/hooks/blueprinter.rb +14 -0
- data/lib/typelizer/dsl/hooks.rb +3 -0
- data/lib/typelizer/interface.rb +2 -2
- data/lib/typelizer/model_plugins/active_record.rb +12 -3
- data/lib/typelizer/openapi.rb +2 -0
- data/lib/typelizer/property.rb +4 -2
- data/lib/typelizer/route_generator.rb +11 -3
- data/lib/typelizer/route_writer.rb +6 -2
- data/lib/typelizer/serializer_plugins/alba.rb +1 -1
- data/lib/typelizer/serializer_plugins/auto.rb +2 -0
- data/lib/typelizer/serializer_plugins/blueprinter.rb +71 -0
- data/lib/typelizer/serializer_plugins/panko.rb +54 -22
- data/lib/typelizer/serializer_plugins/{alba/trait_interface.rb → trait_interface.rb} +5 -4
- data/lib/typelizer/version.rb +1 -1
- data/lib/typelizer/writer.rb +7 -4
- data/lib/typelizer.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6f1a3eec70cb7c226441194e16ed081b2898466dc1ab1ac7dea3dfeebc1fd3bd
|
|
4
|
+
data.tar.gz: dbc20344b6cabd942c45d9334a7e374e62107c04dd756d84fa1495e68bb050a0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3c82931cc7b7a9a89da19e3ee28e09e5ca50c84842b0aed6c5a4e991b692a7cb9923009f3510ea7e067489cbd994d6b8482889f18c33bed96e8bd4b1ec4724d6
|
|
7
|
+
data.tar.gz: e3360969ba9bb962a4809a93fb01285891b3da0e6c9ecd12130154678755513e7e1d4b25cb9ad0f19d2f379d5f3670caf8765aba97fe61c62df243add1efc8ca
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,31 @@ and this project adheres to [Semantic Versioning].
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.15.0] - 2026-11-16
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **[BREAKING]** Enum types of namespaced models keep the namespace: `Course::Build` with a `state` enum gives `CourseBuildState`, not `BuildState`. Models with the same class name no longer overwrite each other's enums. ([@skryukov])
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- Panko 1.0 support. ([@benjaminwols])
|
|
19
|
+
|
|
20
|
+
## [0.14.0] - 2026-10-03
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- [Blueprinter](https://github.com/procore-oss/blueprinter) support, including views. ([@skryukov])
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- Associations and nested attributes no longer take the type of a model column with the same name. ([@lukasedw])
|
|
29
|
+
- Generated route files no longer have duplicate keys when a resource is routed more than once, e.g. nested under several parents. ([@visini])
|
|
30
|
+
- OpenAPI: a string literal mixed with other types (`'auto' | number`) now becomes a string enum instead of a broken `$ref`. ([@skryukov])
|
|
31
|
+
- Conditional attributes (`if:`/`unless:`) stay optional under `null_strategy :optional` and `:nullable_and_optional`, even when the column is NOT NULL. ([@skryukov])
|
|
32
|
+
- Generation no longer crashes for a serializer whose class name contains neither `Serializer` nor `Resource`. ([@skryukov])
|
|
33
|
+
- Keys that aren't valid identifiers (e.g. from Alba's `transform_keys :dash`) are now quoted, and quotes in enum values are escaped. Run `rails typelizer:generate:refresh` once to fix existing files. ([@skryukov])
|
|
34
|
+
|
|
10
35
|
## [0.13.1] - 2026-05-18
|
|
11
36
|
|
|
12
37
|
### Added
|
|
@@ -500,12 +525,14 @@ and this project adheres to [Semantic Versioning].
|
|
|
500
525
|
|
|
501
526
|
- Initial release ([@skryukov])
|
|
502
527
|
|
|
528
|
+
[@benjaminwols]: https://github.com/benjaminwols
|
|
503
529
|
[@davidrunger]: https://github.com/davidrunger
|
|
504
530
|
[@Envek]: https://github.com/Envek
|
|
505
531
|
[@hkamberovic]: https://github.com/hkamberovic
|
|
506
532
|
[@jonmarkgo]: https://github.com/jonmarkgo
|
|
507
533
|
[@julik]: https://github.com/julik
|
|
508
534
|
[@kristinemcbride]: https://github.com/kristinemcbride
|
|
535
|
+
[@lukasedw]: https://github.com/lukasedw
|
|
509
536
|
[@nkriege]: https://github.com/nkriege
|
|
510
537
|
[@NOX73]: https://github.com/NOX73
|
|
511
538
|
[@okuramasafumi]: https://github.com/okuramasafumi
|
|
@@ -517,8 +544,11 @@ and this project adheres to [Semantic Versioning].
|
|
|
517
544
|
[@rdavid1099]: https://github.com/rdavid1099
|
|
518
545
|
[@skryukov]: https://github.com/skryukov
|
|
519
546
|
[@ventsislaf]: https://github.com/ventsislaf
|
|
547
|
+
[@visini]: https://github.com/visini
|
|
520
548
|
|
|
521
|
-
[Unreleased]: https://github.com/skryukov/typelizer/compare/v0.
|
|
549
|
+
[Unreleased]: https://github.com/skryukov/typelizer/compare/v0.15.0...HEAD
|
|
550
|
+
[0.15.0]: https://github.com/skryukov/typelizer/compare/v0.14.0...v0.15.0
|
|
551
|
+
[0.14.0]: https://github.com/skryukov/typelizer/compare/v0.13.1...v0.14.0
|
|
522
552
|
[0.13.1]: https://github.com/skryukov/typelizer/compare/v0.13.0...v0.13.1
|
|
523
553
|
[0.13.0]: https://github.com/skryukov/typelizer/compare/v0.12.0...v0.13.0
|
|
524
554
|
[0.12.0]: https://github.com/skryukov/typelizer/compare/v0.11.0...v0.12.0
|
data/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Typelizer generates TypeScript types, route helpers, and OpenAPI schemas from yo
|
|
|
8
8
|
|
|
9
9
|
- Automatic TypeScript interface generation from serializers
|
|
10
10
|
- Type-safe route helpers from Rails routes
|
|
11
|
-
- Supports Alba, ActiveModel::Serializer, Oj::Serializer, Panko::Serializer
|
|
11
|
+
- Supports Alba, ActiveModel::Serializer, Oj::Serializer, Panko::Serializer, Blueprinter
|
|
12
12
|
- OpenAPI 3.0/3.1 schema generation
|
|
13
13
|
- Multiple output writers with layered configuration
|
|
14
14
|
- File watching with automatic regeneration in development
|
|
@@ -60,7 +60,7 @@ cd spec/app && RAILS_ENV=test bundle exec rails db:create db:migrate && cd ../..
|
|
|
60
60
|
bundle exec rspec
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
The test suite uses a dummy Rails app in `spec/app/` with models, migrations, and serializers for all
|
|
63
|
+
The test suite uses a dummy Rails app in `spec/app/` with models, migrations, and serializers for all five supported frameworks (Alba, AMS, OjSerializers, Panko, Blueprinter). Linting is done with StandardRB:
|
|
64
64
|
|
|
65
65
|
```bash
|
|
66
66
|
bundle exec standardrb
|
|
@@ -72,9 +72,9 @@ bundle exec standardrb
|
|
|
72
72
|
|
|
73
73
|
Typelizer is inspired by [types_from_serializers](https://github.com/ElMassimo/types_from_serializers), [js-routes](https://github.com/railsware/js-routes), and [Wayfinder](https://github.com/nicholasvansanten/wayfinder).
|
|
74
74
|
|
|
75
|
-
<
|
|
76
|
-
|
|
77
|
-
</a>
|
|
75
|
+
<br/>
|
|
76
|
+
|
|
77
|
+
<img src="https://cdn.evilmartians.com/badges/logo-no-label.svg" alt="Evil Martians logo" width="22" height="16" /> <b>Typelizer</b> is built by <b><a href="https://evilmartians.com/">Evil Martians</a></b>, an American design and engineering consultancy for <b>developer tools, AI, and cybersecurity startups</b>.
|
|
78
78
|
|
|
79
79
|
## License
|
|
80
80
|
|
data/lib/typelizer/config.rb
CHANGED
data/lib/typelizer/dsl/hooks.rb
CHANGED
|
@@ -18,6 +18,9 @@ module Typelizer
|
|
|
18
18
|
elsif defined?(::Panko::Serializer) && base.ancestors.include?(::Panko::Serializer)
|
|
19
19
|
require_relative "hooks/panko"
|
|
20
20
|
base.singleton_class.prepend(Panko)
|
|
21
|
+
elsif defined?(::Blueprinter::Base) && base.ancestors.include?(::Blueprinter::Base)
|
|
22
|
+
require_relative "hooks/blueprinter"
|
|
23
|
+
base.singleton_class.prepend(Blueprinter)
|
|
21
24
|
end
|
|
22
25
|
end
|
|
23
26
|
|
data/lib/typelizer/interface.rb
CHANGED
|
@@ -148,7 +148,7 @@ module Typelizer
|
|
|
148
148
|
# Collect enum type names from properties
|
|
149
149
|
enum_imports = all_properties.filter_map(&:enum_type_name)
|
|
150
150
|
|
|
151
|
-
result = (custom_type_imports + serializer_types + trait_imports + enum_imports + Array(parent_interface&.name)).uniq - [self_type_name, name]
|
|
151
|
+
result = (custom_type_imports + serializer_types + trait_imports + enum_imports + Array(parent_interface&.name)).uniq - [self_type_name, name, *trait_interfaces.map(&:name)]
|
|
152
152
|
ImportSorter.sort(result, config.imports_sort_order)
|
|
153
153
|
end
|
|
154
154
|
end
|
|
@@ -190,7 +190,7 @@ module Typelizer
|
|
|
190
190
|
end
|
|
191
191
|
|
|
192
192
|
def self_type_name
|
|
193
|
-
serializer.name
|
|
193
|
+
serializer.name[/(\w+::)?(\w+)(Serializer|Resource|Blueprint)/, 2]
|
|
194
194
|
end
|
|
195
195
|
|
|
196
196
|
def extract_typescript_types(type)
|
|
@@ -10,6 +10,7 @@ module Typelizer
|
|
|
10
10
|
|
|
11
11
|
def infer_types(prop)
|
|
12
12
|
infer_types_for_association(prop) ||
|
|
13
|
+
serializer_resolved_type?(prop) ||
|
|
13
14
|
infer_types_for_column(prop) ||
|
|
14
15
|
infer_types_for_association_ids(prop) ||
|
|
15
16
|
infer_types_for_delegate(prop) ||
|
|
@@ -26,15 +27,22 @@ module Typelizer
|
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
def enum_for(prop)
|
|
30
|
+
return if serializer_resolved_type?(prop)
|
|
29
31
|
return unless model_class&.defined_enums&.key?(prop.column_name.to_s)
|
|
30
32
|
|
|
31
33
|
prop.enum = model_class.defined_enums[prop.column_name.to_s].keys
|
|
32
|
-
prop.enum_type_name = "#{model_class.name.
|
|
34
|
+
prop.enum_type_name = "#{model_class.name.delete(":")}#{prop.column_name.to_s.camelize}"
|
|
33
35
|
prop.enum
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
private
|
|
37
39
|
|
|
40
|
+
# An association or nested attribute is already typed by the serializer;
|
|
41
|
+
# a same-named column describes a different value.
|
|
42
|
+
def serializer_resolved_type?(prop)
|
|
43
|
+
prop.type.is_a?(Interface) || prop.type.is_a?(Shape)
|
|
44
|
+
end
|
|
45
|
+
|
|
38
46
|
def columns_hash
|
|
39
47
|
return nil unless model_class
|
|
40
48
|
return nil if model_class.abstract_class?
|
|
@@ -55,6 +63,7 @@ module Typelizer
|
|
|
55
63
|
association = model_class&.reflect_on_association(prop.column_name.to_sym)
|
|
56
64
|
return nil unless association
|
|
57
65
|
|
|
66
|
+
prop.multi = association.collection? if prop.multi.nil?
|
|
58
67
|
case association.macro
|
|
59
68
|
when :belongs_to
|
|
60
69
|
foreign_key = association.foreign_key
|
|
@@ -89,10 +98,10 @@ module Typelizer
|
|
|
89
98
|
when :nullable
|
|
90
99
|
prop.nullable = column.null
|
|
91
100
|
when :optional
|
|
92
|
-
prop.optional
|
|
101
|
+
prop.optional ||= column.null
|
|
93
102
|
when :nullable_and_optional
|
|
94
103
|
prop.nullable = column.null
|
|
95
|
-
prop.optional
|
|
104
|
+
prop.optional ||= column.null
|
|
96
105
|
else
|
|
97
106
|
raise "Unknown null strategy: #{config.null_strategy}"
|
|
98
107
|
end
|
data/lib/typelizer/openapi.rb
CHANGED
|
@@ -101,6 +101,8 @@ module Typelizer
|
|
|
101
101
|
def union_member_schema(type)
|
|
102
102
|
if type.respond_to?(:properties)
|
|
103
103
|
{"$ref" => "#/components/schemas/#{type.name}"}
|
|
104
|
+
elsif string_literal?(type)
|
|
105
|
+
{type: :string, enum: [unquote_string_literal(type)]}
|
|
104
106
|
else
|
|
105
107
|
sym = type.to_sym
|
|
106
108
|
if OPENAPI_TYPES.include?(sym)
|
data/lib/typelizer/property.rb
CHANGED
|
@@ -53,7 +53,7 @@ module Typelizer
|
|
|
53
53
|
# Add nullable at the end (null should always be last in sorted output)
|
|
54
54
|
type_str = "#{type_str} | null" if nullable
|
|
55
55
|
|
|
56
|
-
"#{name}#{"?" if optional}: #{type_str}"
|
|
56
|
+
"#{js_key(name.to_s, prefer_double_quotes)}#{"?" if optional}: #{type_str}"
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def fingerprint
|
|
@@ -95,7 +95,9 @@ module Typelizer
|
|
|
95
95
|
end
|
|
96
96
|
|
|
97
97
|
def quote_string(str, prefer_double_quotes)
|
|
98
|
-
prefer_double_quotes ? "
|
|
98
|
+
quote = prefer_double_quotes ? '"' : "'"
|
|
99
|
+
escaped = str.to_s.gsub(/[\\#{quote}]/) { |char| "\\#{char}" }
|
|
100
|
+
"#{quote}#{escaped}#{quote}"
|
|
99
101
|
end
|
|
100
102
|
|
|
101
103
|
def js_key(str, prefer_double_quotes)
|
|
@@ -49,6 +49,12 @@ module Typelizer
|
|
|
49
49
|
.map { |r| [r[:controller], r[:action], r[:path]] }.to_set
|
|
50
50
|
routes.reject! { |r| r[:verb] == "put" && patch_keys.include?([r[:controller], r[:action], r[:path]]) }
|
|
51
51
|
|
|
52
|
+
# Skip unnamed duplicates of a named route (Rails repeats member routes under each parent for `shallow: true`)
|
|
53
|
+
named_keys = routes.select { |r| r[:named] }
|
|
54
|
+
.map { |r| [r[:controller], r[:verb], r[:path]] }.to_set
|
|
55
|
+
routes.reject! { |r| !r[:named] && named_keys.include?([r[:controller], r[:verb], r[:path]]) }
|
|
56
|
+
routes.uniq!
|
|
57
|
+
|
|
52
58
|
if config.include
|
|
53
59
|
patterns = Array(config.include)
|
|
54
60
|
routes = routes.select { |r| patterns.any? { |p| match_route?(r, p) } }
|
|
@@ -70,12 +76,12 @@ module Typelizer
|
|
|
70
76
|
end
|
|
71
77
|
|
|
72
78
|
def build_named_paths(named_routes, path_prefix: "")
|
|
73
|
-
named_routes.each_with_object(
|
|
79
|
+
named_routes.each_with_object({}) do |(name, route), paths|
|
|
74
80
|
controller = route.requirements[:controller]
|
|
75
81
|
action = route.requirements[:action]
|
|
76
82
|
next unless controller && action
|
|
77
83
|
|
|
78
|
-
paths
|
|
84
|
+
paths[[controller, path_prefix + strip_format(route.path.spec.to_s)]] ||= name.to_s
|
|
79
85
|
end
|
|
80
86
|
end
|
|
81
87
|
|
|
@@ -91,7 +97,8 @@ module Typelizer
|
|
|
91
97
|
|
|
92
98
|
if controller.present? && action.present?
|
|
93
99
|
# Match by [controller, path] so unnamed aliases at distinct paths (e.g. ActiveStorage representations) don't inherit a sibling's name
|
|
94
|
-
|
|
100
|
+
path_name = named_paths[[controller, path]]
|
|
101
|
+
name = route.name || (action if path_name)
|
|
95
102
|
elsif route.name.present?
|
|
96
103
|
name = route.name.to_s
|
|
97
104
|
controller = "_routes"
|
|
@@ -109,6 +116,7 @@ module Typelizer
|
|
|
109
116
|
{
|
|
110
117
|
name: name,
|
|
111
118
|
named: !!route.name,
|
|
119
|
+
path_name: path_name,
|
|
112
120
|
controller: controller,
|
|
113
121
|
action: action,
|
|
114
122
|
verb: verb,
|
|
@@ -145,8 +145,12 @@ module Typelizer
|
|
|
145
145
|
end
|
|
146
146
|
|
|
147
147
|
def route_key(route, controller_routes)
|
|
148
|
-
|
|
149
|
-
camelize_key(
|
|
148
|
+
same_action = controller_routes.select { |r| r[:action] == route[:action] }
|
|
149
|
+
return camelize_key(route[:action]) if same_action.one?
|
|
150
|
+
return camelize_key(route[:name]) if route[:named]
|
|
151
|
+
return camelize_key(route[:action]) if same_action.one? { |r| !r[:named] }
|
|
152
|
+
|
|
153
|
+
camelize_key("#{route[:action]}_#{route[:path_name]}")
|
|
150
154
|
end
|
|
151
155
|
|
|
152
156
|
def camelize_key(key)
|
|
@@ -15,6 +15,8 @@ module Typelizer
|
|
|
15
15
|
AMS
|
|
16
16
|
elsif defined?(::Panko::Serializer) && serializer.ancestors.include?(::Panko::Serializer)
|
|
17
17
|
Panko
|
|
18
|
+
elsif defined?(::Blueprinter::Base) && serializer.ancestors.include?(::Blueprinter::Base)
|
|
19
|
+
Blueprinter
|
|
18
20
|
else
|
|
19
21
|
raise "Can't guess serializer plugin for #{serializer}. " \
|
|
20
22
|
"Please specify it with `config.serializer_plugin`."
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require_relative "base"
|
|
2
|
+
require_relative "trait_interface"
|
|
3
|
+
|
|
4
|
+
module Typelizer
|
|
5
|
+
module SerializerPlugins
|
|
6
|
+
class Blueprinter < Base
|
|
7
|
+
CONDITIONAL_OPTIONS = %i[if unless exclude_if_nil].freeze
|
|
8
|
+
|
|
9
|
+
def properties
|
|
10
|
+
view_properties(:default)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Each extra view becomes its own full type, since views can also exclude fields
|
|
14
|
+
def trait_interfaces
|
|
15
|
+
@trait_interfaces ||= (serializer.reflections.keys - %i[default identifier]).map do |view|
|
|
16
|
+
TraitInterface.new(serializer: serializer, trait_name: view, context: context, plugin: self, suffix: "View")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def trait_properties(view)
|
|
21
|
+
typelizes = serializer.respond_to?(:_typelizer_attributes) ? serializer._typelizer_attributes : {}
|
|
22
|
+
[view_properties(view), typelizes]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def view_properties(view)
|
|
28
|
+
reflection = serializer.reflections.fetch(view)
|
|
29
|
+
reflection.fields.values.map { |field| field_property(field) } +
|
|
30
|
+
reflection.associations.values.map { |association| association_property(association) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def field_property(field)
|
|
34
|
+
Property.new(
|
|
35
|
+
name: field.display_name,
|
|
36
|
+
optional: optional?(field.options),
|
|
37
|
+
nullable: false,
|
|
38
|
+
multi: false,
|
|
39
|
+
column_name: field.name
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# `multi` stays nil: Blueprinter renders a list or a single object depending
|
|
44
|
+
# on the value, so the model's association decides
|
|
45
|
+
def association_property(association)
|
|
46
|
+
Property.new(
|
|
47
|
+
name: association.display_name,
|
|
48
|
+
type: association_type(association),
|
|
49
|
+
optional: optional?(association.options),
|
|
50
|
+
nullable: false,
|
|
51
|
+
multi: nil,
|
|
52
|
+
column_name: association.name
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def association_type(association)
|
|
57
|
+
blueprint = association.blueprint
|
|
58
|
+
return unless blueprint.is_a?(Class)
|
|
59
|
+
|
|
60
|
+
interface = context.interface_for(blueprint)
|
|
61
|
+
return interface if association.view == :default
|
|
62
|
+
|
|
63
|
+
interface.trait_interfaces.find { |view| view.trait_name == association.view }&.name
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def optional?(options)
|
|
67
|
+
CONDITIONAL_OPTIONS.any? { |key| options.key?(key) }
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -4,46 +4,78 @@ module Typelizer
|
|
|
4
4
|
module SerializerPlugins
|
|
5
5
|
class Panko < Base
|
|
6
6
|
def properties
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
has_one_associations = descriptor.has_one_associations
|
|
12
|
-
|
|
13
|
-
attributes.map do |att|
|
|
14
|
-
attribute_property(att)
|
|
15
|
-
end + methods_attributes.map do |att|
|
|
16
|
-
attribute_property(att)
|
|
17
|
-
end + has_many_associations.map do |assoc|
|
|
18
|
-
association_property(assoc, multi: true)
|
|
19
|
-
end + has_one_associations.map do |assoc|
|
|
20
|
-
association_property(assoc, multi: false)
|
|
7
|
+
if public_descriptor?
|
|
8
|
+
properties_from_public_descriptor
|
|
9
|
+
else
|
|
10
|
+
properties_from_internal_descriptor
|
|
21
11
|
end
|
|
22
12
|
end
|
|
23
13
|
|
|
24
14
|
private
|
|
25
15
|
|
|
26
|
-
def
|
|
16
|
+
def public_descriptor?
|
|
17
|
+
defined?(::Panko::Descriptor)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def properties_from_public_descriptor
|
|
21
|
+
# The instance descriptor applies the class-level `filters_for`, as Panko < 1.0 did
|
|
22
|
+
descriptor = serializer.new.descriptor
|
|
23
|
+
|
|
24
|
+
attributes = (descriptor.attributes + descriptor.method_attributes).map do |att|
|
|
25
|
+
build_attribute_property(name: att.name.to_s, column_name: (att.source || att.name).to_s)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# has_many before has_one, matching the property order of Panko < 1.0
|
|
29
|
+
has_many, has_one = descriptor.associations.partition { |assoc| assoc.kind == :has_many }
|
|
30
|
+
associations = (has_many + has_one).map do |assoc|
|
|
31
|
+
build_association_property(
|
|
32
|
+
name: assoc.name.to_s,
|
|
33
|
+
serializer: assoc.descriptor.serializer,
|
|
34
|
+
multi: assoc.kind == :has_many
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
attributes + associations
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Panko < 1.0 keeps its descriptor in an instance variable of the C extension
|
|
42
|
+
def properties_from_internal_descriptor
|
|
43
|
+
descriptor = serializer.new.instance_variable_get(:@descriptor)
|
|
44
|
+
|
|
45
|
+
attributes = (descriptor.attributes + descriptor.method_fields).map do |att|
|
|
46
|
+
build_attribute_property(name: att.alias_name || att.name, column_name: att.name)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
has_many = descriptor.has_many_associations.map do |assoc|
|
|
50
|
+
build_association_property(name: assoc.name_str, serializer: assoc.descriptor.type, multi: true)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
has_one = descriptor.has_one_associations.map do |assoc|
|
|
54
|
+
build_association_property(name: assoc.name_str, serializer: assoc.descriptor.type, multi: false)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
attributes + has_many + has_one
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def build_attribute_property(name:, column_name:)
|
|
27
61
|
Property.new(
|
|
28
|
-
name:
|
|
62
|
+
name: name,
|
|
29
63
|
optional: false,
|
|
30
64
|
nullable: false,
|
|
31
65
|
multi: false,
|
|
32
|
-
column_name:
|
|
66
|
+
column_name: column_name
|
|
33
67
|
)
|
|
34
68
|
end
|
|
35
69
|
|
|
36
|
-
def
|
|
37
|
-
key = assoc.name_str
|
|
38
|
-
serializer = assoc.descriptor.type
|
|
70
|
+
def build_association_property(name:, serializer:, multi:)
|
|
39
71
|
type = serializer ? context.interface_for(serializer) : nil
|
|
40
72
|
Property.new(
|
|
41
|
-
name:
|
|
73
|
+
name: name,
|
|
42
74
|
type: type,
|
|
43
75
|
optional: false,
|
|
44
76
|
nullable: false,
|
|
45
77
|
multi: multi,
|
|
46
|
-
column_name:
|
|
78
|
+
column_name: name
|
|
47
79
|
)
|
|
48
80
|
end
|
|
49
81
|
end
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "
|
|
3
|
+
require_relative "../type_inference"
|
|
4
4
|
|
|
5
5
|
module Typelizer
|
|
6
6
|
module SerializerPlugins
|
|
7
|
-
class
|
|
7
|
+
class TraitInterface
|
|
8
8
|
include TypeInference
|
|
9
9
|
|
|
10
10
|
attr_reader :serializer, :trait_name, :context, :plugin
|
|
11
11
|
|
|
12
|
-
def initialize(serializer:, trait_name:, context:, plugin:)
|
|
12
|
+
def initialize(serializer:, trait_name:, context:, plugin:, suffix: "Trait")
|
|
13
13
|
@serializer = serializer
|
|
14
14
|
@trait_name = trait_name
|
|
15
15
|
@context = context
|
|
16
16
|
@plugin = plugin
|
|
17
|
+
@suffix = suffix
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
def config
|
|
@@ -22,7 +23,7 @@ module Typelizer
|
|
|
22
23
|
|
|
23
24
|
def name
|
|
24
25
|
base_name = config.serializer_name_mapper.call(serializer).tr_s(":", "")
|
|
25
|
-
"#{base_name}#{trait_name.to_s.camelize}
|
|
26
|
+
"#{base_name}#{trait_name.to_s.camelize}#{@suffix}"
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
def properties
|
data/lib/typelizer/version.rb
CHANGED
data/lib/typelizer/writer.rb
CHANGED
|
@@ -53,10 +53,13 @@ module Typelizer
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def collect_enums(interfaces)
|
|
56
|
-
interfaces
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
interfaces.flat_map(&:enum_types).group_by(&:enum_type_name).sort.map do |name, enums|
|
|
57
|
+
if enums.map { |enum| enum.enum.to_set }.uniq.size > 1
|
|
58
|
+
Typelizer.logger.warn("Typelizer: enum type #{name} has conflicting values, only the first is generated")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
enums.first
|
|
62
|
+
end
|
|
60
63
|
end
|
|
61
64
|
|
|
62
65
|
def write_enums(enums)
|
data/lib/typelizer.rb
CHANGED
|
@@ -29,6 +29,7 @@ require_relative "typelizer/serializer_plugins/oj_serializers"
|
|
|
29
29
|
require_relative "typelizer/serializer_plugins/alba"
|
|
30
30
|
require_relative "typelizer/serializer_plugins/ams"
|
|
31
31
|
require_relative "typelizer/serializer_plugins/panko"
|
|
32
|
+
require_relative "typelizer/serializer_plugins/blueprinter"
|
|
32
33
|
|
|
33
34
|
require_relative "typelizer/model_plugins/active_record"
|
|
34
35
|
require_relative "typelizer/model_plugins/poro"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: typelizer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.15.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Svyatoslav Kryukov
|
|
@@ -56,6 +56,7 @@ files:
|
|
|
56
56
|
- lib/typelizer/dsl/hooks.rb
|
|
57
57
|
- lib/typelizer/dsl/hooks/alba.rb
|
|
58
58
|
- lib/typelizer/dsl/hooks/ams.rb
|
|
59
|
+
- lib/typelizer/dsl/hooks/blueprinter.rb
|
|
59
60
|
- lib/typelizer/dsl/hooks/oj_serializers.rb
|
|
60
61
|
- lib/typelizer/dsl/hooks/panko.rb
|
|
61
62
|
- lib/typelizer/generator.rb
|
|
@@ -77,12 +78,13 @@ files:
|
|
|
77
78
|
- lib/typelizer/serializer_config_layer.rb
|
|
78
79
|
- lib/typelizer/serializer_plugins/alba.rb
|
|
79
80
|
- lib/typelizer/serializer_plugins/alba/block_attribute_collector.rb
|
|
80
|
-
- lib/typelizer/serializer_plugins/alba/trait_interface.rb
|
|
81
81
|
- lib/typelizer/serializer_plugins/ams.rb
|
|
82
82
|
- lib/typelizer/serializer_plugins/auto.rb
|
|
83
83
|
- lib/typelizer/serializer_plugins/base.rb
|
|
84
|
+
- lib/typelizer/serializer_plugins/blueprinter.rb
|
|
84
85
|
- lib/typelizer/serializer_plugins/oj_serializers.rb
|
|
85
86
|
- lib/typelizer/serializer_plugins/panko.rb
|
|
87
|
+
- lib/typelizer/serializer_plugins/trait_interface.rb
|
|
86
88
|
- lib/typelizer/shape.rb
|
|
87
89
|
- lib/typelizer/templates/comment.ts.erb
|
|
88
90
|
- lib/typelizer/templates/enums.ts.erb
|
|
@@ -124,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
124
126
|
- !ruby/object:Gem::Version
|
|
125
127
|
version: '0'
|
|
126
128
|
requirements: []
|
|
127
|
-
rubygems_version:
|
|
129
|
+
rubygems_version: 4.0.20
|
|
128
130
|
specification_version: 4
|
|
129
131
|
summary: A TypeScript type generator for Ruby serializers.
|
|
130
132
|
test_files: []
|