skit 0.1.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.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +469 -0
  4. data/exe/skit +31 -0
  5. data/lib/active_model/validations/skit_validator.rb +54 -0
  6. data/lib/skit/attribute.rb +63 -0
  7. data/lib/skit/json_schema/class_name_path.rb +67 -0
  8. data/lib/skit/json_schema/cli.rb +166 -0
  9. data/lib/skit/json_schema/code_generator.rb +132 -0
  10. data/lib/skit/json_schema/config.rb +67 -0
  11. data/lib/skit/json_schema/definitions/array_property_type.rb +36 -0
  12. data/lib/skit/json_schema/definitions/const_type.rb +68 -0
  13. data/lib/skit/json_schema/definitions/enum_type.rb +71 -0
  14. data/lib/skit/json_schema/definitions/hash_property_type.rb +36 -0
  15. data/lib/skit/json_schema/definitions/module.rb +54 -0
  16. data/lib/skit/json_schema/definitions/property_type.rb +39 -0
  17. data/lib/skit/json_schema/definitions/property_types.rb +13 -0
  18. data/lib/skit/json_schema/definitions/struct.rb +99 -0
  19. data/lib/skit/json_schema/definitions/struct_property.rb +75 -0
  20. data/lib/skit/json_schema/definitions/union_property_type.rb +40 -0
  21. data/lib/skit/json_schema/naming_utils.rb +25 -0
  22. data/lib/skit/json_schema/schema_analyzer.rb +407 -0
  23. data/lib/skit/json_schema/types/const.rb +69 -0
  24. data/lib/skit/json_schema.rb +77 -0
  25. data/lib/skit/serialization/errors.rb +23 -0
  26. data/lib/skit/serialization/path.rb +69 -0
  27. data/lib/skit/serialization/processor/array.rb +65 -0
  28. data/lib/skit/serialization/processor/base.rb +47 -0
  29. data/lib/skit/serialization/processor/boolean.rb +35 -0
  30. data/lib/skit/serialization/processor/date.rb +40 -0
  31. data/lib/skit/serialization/processor/enum.rb +54 -0
  32. data/lib/skit/serialization/processor/float.rb +36 -0
  33. data/lib/skit/serialization/processor/hash.rb +93 -0
  34. data/lib/skit/serialization/processor/integer.rb +31 -0
  35. data/lib/skit/serialization/processor/json_schema_const.rb +55 -0
  36. data/lib/skit/serialization/processor/nilable.rb +87 -0
  37. data/lib/skit/serialization/processor/simple_type.rb +51 -0
  38. data/lib/skit/serialization/processor/string.rb +31 -0
  39. data/lib/skit/serialization/processor/struct.rb +84 -0
  40. data/lib/skit/serialization/processor/symbol.rb +36 -0
  41. data/lib/skit/serialization/processor/time.rb +40 -0
  42. data/lib/skit/serialization/processor/union.rb +120 -0
  43. data/lib/skit/serialization/registry.rb +33 -0
  44. data/lib/skit/serialization.rb +60 -0
  45. data/lib/skit/version.rb +6 -0
  46. data/lib/skit.rb +46 -0
  47. data/lib/tapioca/dsl/compilers/skit.rb +105 -0
  48. metadata +135 -0
@@ -0,0 +1,105 @@
1
+ # typed: ignore
2
+ # frozen_string_literal: true
3
+
4
+ return unless defined?(Tapioca::Dsl::Compiler)
5
+ return unless defined?(ActiveRecord::Base)
6
+
7
+ module Tapioca
8
+ module Dsl
9
+ module Compilers
10
+ # `Tapioca::Dsl::Compilers::Skit` decorates RBI files for ActiveRecord models
11
+ # that use `Skit::Attribute` for typed JSON attributes.
12
+ #
13
+ # For example, with the following model:
14
+ #
15
+ # ~~~rb
16
+ # class Address < T::Struct
17
+ # const :city, String
18
+ # const :zip, T.nilable(String)
19
+ # end
20
+ #
21
+ # class Customer < ActiveRecord::Base
22
+ # attribute :address, Skit::Attribute[Address]
23
+ # end
24
+ # ~~~
25
+ #
26
+ # this compiler will produce an RBI file with the following content:
27
+ # ~~~rbi
28
+ # # typed: strong
29
+ #
30
+ # class Customer
31
+ # sig { returns(Address) }
32
+ # def address; end
33
+ #
34
+ # sig { params(value: T.untyped).returns(Address) }
35
+ # def address=(value); end
36
+ # end
37
+ # ~~~
38
+ #: [ConstantType = T.class_of(::ActiveRecord::Base)]
39
+ class Skit < Compiler
40
+ extend T::Sig
41
+
42
+ # @override
43
+ #: -> void
44
+ def decorate
45
+ attributes = constant.attribute_types.select { |_name, type| skit_attribute_type?(type) }
46
+
47
+ return if attributes.empty?
48
+
49
+ root.create_path(constant) do |klass|
50
+ attributes.each do |attr_name, type|
51
+ create_attribute_methods(klass, attr_name, type)
52
+ end
53
+ end
54
+ end
55
+
56
+ class << self
57
+ extend T::Sig
58
+
59
+ # @override
60
+ #: -> Enumerable[Module]
61
+ def gather_constants
62
+ descendants = ActiveRecord::Base.descendants.reject(&:abstract_class?)
63
+
64
+ descendants.select do |klass|
65
+ klass.attribute_types.values.any? { |type| skit_attribute_type?(type) }
66
+ rescue ActiveRecord::StatementInvalid
67
+ false
68
+ end
69
+ end
70
+
71
+ #: (untyped type) -> bool
72
+ def skit_attribute_type?(type)
73
+ type.is_a?(::Skit::Attribute)
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ #: (untyped type) -> bool
80
+ def skit_attribute_type?(type)
81
+ self.class.skit_attribute_type?(type)
82
+ end
83
+
84
+ #: (RBI::Scope klass, String attr_name, untyped type) -> void
85
+ def create_attribute_methods(klass, attr_name, type)
86
+ return unless type.is_a?(::Skit::Attribute)
87
+
88
+ type_spec = type.instance_variable_get(:@type_spec)
89
+ type_string = type_spec.name
90
+
91
+ klass.create_method(
92
+ attr_name,
93
+ return_type: type_string
94
+ )
95
+
96
+ klass.create_method(
97
+ "#{attr_name}=",
98
+ parameters: [create_param("value", type: "T.untyped")],
99
+ return_type: type_string
100
+ )
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: skit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Speria, inc.
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activemodel
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: json_schemer
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: sorbet-runtime
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.5'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.5'
54
+ description: Skit provides tools for generating Sorbet T::Struct from JSON Schema,
55
+ serializing/deserializing between JSON and T::Struct, and integrating with ActiveRecord
56
+ JSONB columns.
57
+ email:
58
+ - daichi.sakai@speria.jp
59
+ executables:
60
+ - skit
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - LICENSE
65
+ - README.md
66
+ - exe/skit
67
+ - lib/active_model/validations/skit_validator.rb
68
+ - lib/skit.rb
69
+ - lib/skit/attribute.rb
70
+ - lib/skit/json_schema.rb
71
+ - lib/skit/json_schema/class_name_path.rb
72
+ - lib/skit/json_schema/cli.rb
73
+ - lib/skit/json_schema/code_generator.rb
74
+ - lib/skit/json_schema/config.rb
75
+ - lib/skit/json_schema/definitions/array_property_type.rb
76
+ - lib/skit/json_schema/definitions/const_type.rb
77
+ - lib/skit/json_schema/definitions/enum_type.rb
78
+ - lib/skit/json_schema/definitions/hash_property_type.rb
79
+ - lib/skit/json_schema/definitions/module.rb
80
+ - lib/skit/json_schema/definitions/property_type.rb
81
+ - lib/skit/json_schema/definitions/property_types.rb
82
+ - lib/skit/json_schema/definitions/struct.rb
83
+ - lib/skit/json_schema/definitions/struct_property.rb
84
+ - lib/skit/json_schema/definitions/union_property_type.rb
85
+ - lib/skit/json_schema/naming_utils.rb
86
+ - lib/skit/json_schema/schema_analyzer.rb
87
+ - lib/skit/json_schema/types/const.rb
88
+ - lib/skit/serialization.rb
89
+ - lib/skit/serialization/errors.rb
90
+ - lib/skit/serialization/path.rb
91
+ - lib/skit/serialization/processor/array.rb
92
+ - lib/skit/serialization/processor/base.rb
93
+ - lib/skit/serialization/processor/boolean.rb
94
+ - lib/skit/serialization/processor/date.rb
95
+ - lib/skit/serialization/processor/enum.rb
96
+ - lib/skit/serialization/processor/float.rb
97
+ - lib/skit/serialization/processor/hash.rb
98
+ - lib/skit/serialization/processor/integer.rb
99
+ - lib/skit/serialization/processor/json_schema_const.rb
100
+ - lib/skit/serialization/processor/nilable.rb
101
+ - lib/skit/serialization/processor/simple_type.rb
102
+ - lib/skit/serialization/processor/string.rb
103
+ - lib/skit/serialization/processor/struct.rb
104
+ - lib/skit/serialization/processor/symbol.rb
105
+ - lib/skit/serialization/processor/time.rb
106
+ - lib/skit/serialization/processor/union.rb
107
+ - lib/skit/serialization/registry.rb
108
+ - lib/skit/version.rb
109
+ - lib/tapioca/dsl/compilers/skit.rb
110
+ homepage: https://github.com/speria-jp/skit
111
+ licenses:
112
+ - MIT
113
+ metadata:
114
+ homepage_uri: https://github.com/speria-jp/skit
115
+ source_code_uri: https://github.com/speria-jp/skit
116
+ bug_tracker_uri: https://github.com/speria-jp/skit/issues
117
+ rubygems_mfa_required: 'true'
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: 3.2.0
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubygems_version: 3.6.7
133
+ specification_version: 4
134
+ summary: JSON Schema and Sorbet T::Struct integration toolkit
135
+ test_files: []