jsonapi-resources-anchor 2.11.1 → 2.13.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/lib/anchor/concerns/typeable.rb +26 -0
- data/lib/anchor/config.rb +5 -1
- data/lib/anchor/inference/active_record/infer/base.rb +12 -0
- data/lib/anchor/inference/active_record/infer/columns.rb +42 -0
- data/lib/anchor/inference/active_record/infer/enums.rb +17 -0
- data/lib/anchor/inference/active_record/infer/model.rb +64 -0
- data/lib/anchor/inference/active_record/infer/rbs.rb +30 -0
- data/lib/anchor/inference/active_record/infer.rb +8 -0
- data/lib/anchor/inference/active_record/types/base.rb +9 -0
- data/lib/anchor/inference/active_record/types/column_comments.rb +29 -0
- data/lib/anchor/inference/active_record/types/defaulted.rb +17 -0
- data/lib/anchor/inference/active_record/types/overridden.rb +23 -0
- data/lib/anchor/inference/active_record/types/presence_required.rb +33 -0
- data/lib/anchor/inference/active_record/types/serialized.rb +13 -0
- data/lib/anchor/inference/active_record/types.rb +8 -0
- data/lib/anchor/inference/jsonapi/infer/anchor_def.rb +86 -0
- data/lib/anchor/inference/jsonapi/infer/base.rb +12 -0
- data/lib/anchor/inference/jsonapi/infer/rbs.rb +30 -0
- data/lib/anchor/inference/jsonapi/infer/relationship_references.rb +38 -0
- data/lib/anchor/inference/jsonapi/infer/resource.rb +72 -0
- data/lib/anchor/inference/jsonapi/infer/shell.rb +24 -0
- data/lib/anchor/inference/jsonapi/infer.rb +8 -0
- data/lib/anchor/inference/jsonapi/read_type.rb +47 -0
- data/lib/anchor/inference/jsonapi/types/active_record_relationships_wrapper.rb +44 -0
- data/lib/anchor/inference/jsonapi/types/anchor_comments.rb +23 -0
- data/lib/anchor/inference/jsonapi/types/base.rb +9 -0
- data/lib/anchor/inference/jsonapi/types/empty.rb +9 -0
- data/lib/anchor/inference/jsonapi/types/overridden.rb +14 -0
- data/lib/anchor/inference/jsonapi/types/readable.rb +22 -0
- data/lib/anchor/inference/jsonapi/types/relationships_wrapper.rb +29 -0
- data/lib/anchor/inference/jsonapi/types.rb +8 -0
- data/lib/anchor/json_schema/resource.rb +8 -8
- data/lib/anchor/json_schema/schema_generator.rb +1 -3
- data/lib/anchor/json_schema/serializer.rb +2 -0
- data/lib/anchor/schema.rb +1 -1
- data/lib/anchor/schema_generator.rb +1 -2
- data/lib/anchor/type_script/file_structure.rb +4 -3
- data/lib/anchor/type_script/multifile_schema_generator.rb +1 -4
- data/lib/anchor/type_script/resource.rb +17 -15
- data/lib/anchor/type_script/schema_generator.rb +1 -3
- data/lib/anchor/type_script/serializer.rb +2 -0
- data/lib/anchor/types/inference/active_record.rb +11 -35
- data/lib/anchor/types/inference/rbs.rb +95 -0
- data/lib/anchor/types.rb +123 -10
- data/lib/anchor/version.rb +1 -1
- data/lib/jsonapi-resources-anchor.rb +36 -2
- metadata +34 -5
- data/lib/anchor/resource.rb +0 -201
- data/lib/anchor/types/inference/jsonapi.rb +0 -14
data/lib/anchor/types.rb
CHANGED
|
@@ -7,22 +7,131 @@ module Anchor
|
|
|
7
7
|
class Boolean; end
|
|
8
8
|
class Null; end
|
|
9
9
|
class Unknown; end
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
def
|
|
17
|
-
|
|
10
|
+
Identity = Data.define(:type)
|
|
11
|
+
Record = Data.define(:value_type)
|
|
12
|
+
Maybe = Data.define(:type)
|
|
13
|
+
Array = Data.define(:type)
|
|
14
|
+
Literal = Data.define(:value)
|
|
15
|
+
Union = Data.define(:types) do
|
|
16
|
+
def |(other)
|
|
17
|
+
self.class.new(types + [other])
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
|
-
|
|
20
|
+
Intersection = Data.define(:types)
|
|
21
|
+
Reference = Data.define(:name) do
|
|
22
|
+
def anchor_schema_name = name
|
|
23
|
+
|
|
24
|
+
def |(other)
|
|
25
|
+
Anchor::Types::Union.new([self, other])
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
Property = Data.define(:name, :type, :optional, :description) do
|
|
30
|
+
def initialize(name:, type:, optional: false, description: nil)
|
|
31
|
+
super
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def dup(name: nil, type: nil, optional: nil, description: nil)
|
|
35
|
+
self.class.new(
|
|
36
|
+
name: name || self.name,
|
|
37
|
+
type: type || self.type,
|
|
38
|
+
optional: optional.nil? ? self.optional : optional,
|
|
39
|
+
description: description || self.description,
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
21
44
|
class Object
|
|
22
|
-
attr_reader :properties
|
|
45
|
+
attr_reader :properties, :properties_hash
|
|
46
|
+
|
|
47
|
+
delegate :[], :keys, :key?, to: :properties_hash
|
|
23
48
|
|
|
24
49
|
def initialize(properties)
|
|
25
50
|
@properties = properties || []
|
|
51
|
+
@properties_hash = properties.index_by(&:name) || []
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def pick(keys)
|
|
55
|
+
picked = properties_hash.slice(*keys).values
|
|
56
|
+
self.class.new(picked)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def omit(keys)
|
|
60
|
+
omitted = properties_hash.except(*keys).values
|
|
61
|
+
self.class.new(omitted)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def pick_by_value(t)
|
|
65
|
+
props = properties.filter { |prop| prop.type.is_a?(t) }
|
|
66
|
+
self.class.new(props)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def untype(names = nil)
|
|
70
|
+
names ||= keys
|
|
71
|
+
pick(names).overwrite_values(Anchor::Types::Unknown) + omit(names)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def overwrite_values(type)
|
|
75
|
+
props = properties.map { |prop| prop.dup(type:) }
|
|
76
|
+
self.class.new(props)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def apply_higher(other, keep_description: :right)
|
|
80
|
+
props = properties.filter_map do |prop|
|
|
81
|
+
if (other_prop = other[prop.name])
|
|
82
|
+
desc = keep_description == :right ? other_prop.description : property.description
|
|
83
|
+
Property.new(prop.name, other_prop.type.new(prop.type), prop.optional, desc)
|
|
84
|
+
else
|
|
85
|
+
prop
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
self.class.new(props)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def overwrite(other, keep_description: :right)
|
|
92
|
+
props = properties.map do |prop|
|
|
93
|
+
if (other_prop = other[prop.name])
|
|
94
|
+
description = keep_description == :left ? prop.description : other_prop.description
|
|
95
|
+
other_prop.dup(description:)
|
|
96
|
+
else
|
|
97
|
+
prop.dup
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
self.class.new(props)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# left-based union
|
|
104
|
+
def +(other)
|
|
105
|
+
self.class.new(properties + other.omit(keys).properties)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def transform_keys
|
|
109
|
+
props = properties.map { |prop| prop.dup(name: yield(prop.name)) }
|
|
110
|
+
self.class.new(props)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def camelize
|
|
114
|
+
transform_keys { |name| Anchor::Types.camelize_without_inflection(name) }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def convert_case
|
|
118
|
+
transform_keys { |name| Anchor::Types.convert_case(name) }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def nonnullable
|
|
122
|
+
props = properties.map do |prop|
|
|
123
|
+
next prop unless prop.type.is_a?(Anchor::Types::Maybe)
|
|
124
|
+
prop.dup(type: prop.type.type)
|
|
125
|
+
end
|
|
126
|
+
self.class.new(props)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def nullable_to_optional
|
|
130
|
+
props = properties.map do |prop|
|
|
131
|
+
next prop unless prop.type.is_a?(Anchor::Types::Maybe)
|
|
132
|
+
prop.dup(type: prop.type.type, optional: true)
|
|
133
|
+
end
|
|
134
|
+
self.class.new(props)
|
|
26
135
|
end
|
|
27
136
|
|
|
28
137
|
class << self
|
|
@@ -34,6 +143,10 @@ module Anchor
|
|
|
34
143
|
@properties ||= []
|
|
35
144
|
@properties.push(Property.new(name, type, optional, description))
|
|
36
145
|
end
|
|
146
|
+
|
|
147
|
+
def camelize
|
|
148
|
+
new(properties.map { |prop| prop.dup(name: Anchor::Types.camelize_without_inflection(prop.name)) })
|
|
149
|
+
end
|
|
37
150
|
end
|
|
38
151
|
end
|
|
39
152
|
|
data/lib/anchor/version.rb
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
require "anchor/types"
|
|
2
2
|
require "anchor/anchor"
|
|
3
3
|
require "anchor/config"
|
|
4
|
-
require "anchor/resource"
|
|
5
4
|
require "anchor/schema_generator"
|
|
6
5
|
require "anchor/concerns/annotatable"
|
|
6
|
+
require "anchor/concerns/typeable"
|
|
7
7
|
require "anchor/concerns/custom_linkable"
|
|
8
8
|
require "anchor/concerns/custom_meta"
|
|
9
9
|
require "anchor/concerns/static_context"
|
|
10
10
|
require "anchor/concerns/type_inferable"
|
|
11
11
|
require "anchor/concerns/schema_serializable"
|
|
12
12
|
require "anchor/schema"
|
|
13
|
-
require "anchor/types/inference/jsonapi"
|
|
14
13
|
require "anchor/types/inference/active_record"
|
|
14
|
+
require "anchor/types/inference/rbs"
|
|
15
15
|
require "anchor/type_script/file_structure"
|
|
16
16
|
require "anchor/type_script/types"
|
|
17
17
|
require "anchor/type_script/schema_generator"
|
|
@@ -22,3 +22,37 @@ require "anchor/type_script/resource"
|
|
|
22
22
|
require "anchor/json_schema/serializer"
|
|
23
23
|
require "anchor/json_schema/schema_generator"
|
|
24
24
|
require "anchor/json_schema/resource"
|
|
25
|
+
|
|
26
|
+
require "anchor/inference/active_record/types"
|
|
27
|
+
require "anchor/inference/active_record/types/base"
|
|
28
|
+
require "anchor/inference/active_record/types/column_comments"
|
|
29
|
+
require "anchor/inference/active_record/types/presence_required"
|
|
30
|
+
require "anchor/inference/active_record/types/serialized"
|
|
31
|
+
require "anchor/inference/active_record/types/defaulted"
|
|
32
|
+
require "anchor/inference/active_record/types/overridden"
|
|
33
|
+
|
|
34
|
+
require "anchor/inference/active_record/infer"
|
|
35
|
+
require "anchor/inference/active_record/infer/base"
|
|
36
|
+
require "anchor/inference/active_record/infer/columns"
|
|
37
|
+
require "anchor/inference/active_record/infer/model"
|
|
38
|
+
require "anchor/inference/active_record/infer/enums"
|
|
39
|
+
require "anchor/inference/active_record/infer/rbs"
|
|
40
|
+
|
|
41
|
+
require "anchor/inference/jsonapi/types"
|
|
42
|
+
require "anchor/inference/jsonapi/types/base"
|
|
43
|
+
require "anchor/inference/jsonapi/types/active_record_relationships_wrapper"
|
|
44
|
+
require "anchor/inference/jsonapi/types/relationships_wrapper"
|
|
45
|
+
require "anchor/inference/jsonapi/types/empty"
|
|
46
|
+
require "anchor/inference/jsonapi/types/overridden"
|
|
47
|
+
require "anchor/inference/jsonapi/types/anchor_comments"
|
|
48
|
+
require "anchor/inference/jsonapi/types/readable"
|
|
49
|
+
|
|
50
|
+
require "anchor/inference/jsonapi/infer"
|
|
51
|
+
require "anchor/inference/jsonapi/infer/base"
|
|
52
|
+
require "anchor/inference/jsonapi/infer/anchor_def"
|
|
53
|
+
require "anchor/inference/jsonapi/infer/shell"
|
|
54
|
+
require "anchor/inference/jsonapi/infer/rbs"
|
|
55
|
+
require "anchor/inference/jsonapi/infer/relationship_references"
|
|
56
|
+
require "anchor/inference/jsonapi/infer/resource"
|
|
57
|
+
|
|
58
|
+
require "anchor/inference/jsonapi/read_type"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jsonapi-resources-anchor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.13.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matt
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-
|
|
11
|
+
date: 2025-10-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: jsonapi-resources
|
|
@@ -93,11 +93,40 @@ files:
|
|
|
93
93
|
- lib/anchor/concerns/schema_serializable.rb
|
|
94
94
|
- lib/anchor/concerns/static_context.rb
|
|
95
95
|
- lib/anchor/concerns/type_inferable.rb
|
|
96
|
+
- lib/anchor/concerns/typeable.rb
|
|
96
97
|
- lib/anchor/config.rb
|
|
98
|
+
- lib/anchor/inference/active_record/infer.rb
|
|
99
|
+
- lib/anchor/inference/active_record/infer/base.rb
|
|
100
|
+
- lib/anchor/inference/active_record/infer/columns.rb
|
|
101
|
+
- lib/anchor/inference/active_record/infer/enums.rb
|
|
102
|
+
- lib/anchor/inference/active_record/infer/model.rb
|
|
103
|
+
- lib/anchor/inference/active_record/infer/rbs.rb
|
|
104
|
+
- lib/anchor/inference/active_record/types.rb
|
|
105
|
+
- lib/anchor/inference/active_record/types/base.rb
|
|
106
|
+
- lib/anchor/inference/active_record/types/column_comments.rb
|
|
107
|
+
- lib/anchor/inference/active_record/types/defaulted.rb
|
|
108
|
+
- lib/anchor/inference/active_record/types/overridden.rb
|
|
109
|
+
- lib/anchor/inference/active_record/types/presence_required.rb
|
|
110
|
+
- lib/anchor/inference/active_record/types/serialized.rb
|
|
111
|
+
- lib/anchor/inference/jsonapi/infer.rb
|
|
112
|
+
- lib/anchor/inference/jsonapi/infer/anchor_def.rb
|
|
113
|
+
- lib/anchor/inference/jsonapi/infer/base.rb
|
|
114
|
+
- lib/anchor/inference/jsonapi/infer/rbs.rb
|
|
115
|
+
- lib/anchor/inference/jsonapi/infer/relationship_references.rb
|
|
116
|
+
- lib/anchor/inference/jsonapi/infer/resource.rb
|
|
117
|
+
- lib/anchor/inference/jsonapi/infer/shell.rb
|
|
118
|
+
- lib/anchor/inference/jsonapi/read_type.rb
|
|
119
|
+
- lib/anchor/inference/jsonapi/types.rb
|
|
120
|
+
- lib/anchor/inference/jsonapi/types/active_record_relationships_wrapper.rb
|
|
121
|
+
- lib/anchor/inference/jsonapi/types/anchor_comments.rb
|
|
122
|
+
- lib/anchor/inference/jsonapi/types/base.rb
|
|
123
|
+
- lib/anchor/inference/jsonapi/types/empty.rb
|
|
124
|
+
- lib/anchor/inference/jsonapi/types/overridden.rb
|
|
125
|
+
- lib/anchor/inference/jsonapi/types/readable.rb
|
|
126
|
+
- lib/anchor/inference/jsonapi/types/relationships_wrapper.rb
|
|
97
127
|
- lib/anchor/json_schema/resource.rb
|
|
98
128
|
- lib/anchor/json_schema/schema_generator.rb
|
|
99
129
|
- lib/anchor/json_schema/serializer.rb
|
|
100
|
-
- lib/anchor/resource.rb
|
|
101
130
|
- lib/anchor/schema.rb
|
|
102
131
|
- lib/anchor/schema_generator.rb
|
|
103
132
|
- lib/anchor/type_script/file_structure.rb
|
|
@@ -109,7 +138,7 @@ files:
|
|
|
109
138
|
- lib/anchor/type_script/types.rb
|
|
110
139
|
- lib/anchor/types.rb
|
|
111
140
|
- lib/anchor/types/inference/active_record.rb
|
|
112
|
-
- lib/anchor/types/inference/
|
|
141
|
+
- lib/anchor/types/inference/rbs.rb
|
|
113
142
|
- lib/anchor/version.rb
|
|
114
143
|
- lib/jsonapi-resources-anchor.rb
|
|
115
144
|
homepage:
|
|
@@ -117,7 +146,7 @@ licenses:
|
|
|
117
146
|
- MIT
|
|
118
147
|
metadata:
|
|
119
148
|
changelog_uri: https://github.com/mattkhan/jsonapi-resources-anchor/blob/main/CHANGELOG.md
|
|
120
|
-
documentation_uri: https://anchor
|
|
149
|
+
documentation_uri: https://jsonapi-resources-anchor.up.railway.app/docs
|
|
121
150
|
source_code_uri: https://github.com/mattkhan/jsonapi-resources-anchor
|
|
122
151
|
github_repo: ssh://github.com/mattkhan/jsonapi-resources-anchor
|
|
123
152
|
post_install_message:
|
data/lib/anchor/resource.rb
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
module Anchor
|
|
2
|
-
class Resource
|
|
3
|
-
delegate_missing_to :@resource_klass
|
|
4
|
-
attr_reader :resource_klass
|
|
5
|
-
|
|
6
|
-
# resource_klass#anchor_attributes, #anchor_relationships, #anchor_attributes_descriptions,
|
|
7
|
-
# #anchor_relationships_descriptions are optional methods from Anchor::Annotatable.
|
|
8
|
-
# @param [JSONAPI::Resource] Must include Anchor::TypeInferable
|
|
9
|
-
def initialize(resource_klass)
|
|
10
|
-
@resource_klass = resource_klass
|
|
11
|
-
@anchor_attributes = resource_klass.try(:anchor_attributes) || {}
|
|
12
|
-
@anchor_relationships = resource_klass.try(:anchor_relationships) || {}
|
|
13
|
-
@anchor_attributes_descriptions = resource_klass.try(:anchor_attributes_descriptions) || {}
|
|
14
|
-
@anchor_relationships_descriptions = resource_klass.try(:anchor_relationships_descriptions) || {}
|
|
15
|
-
@anchor_method_added_count = resource_klass.anchor_method_added_count || Hash.new(0)
|
|
16
|
-
@anchor_links_schema = resource_klass.try(:anchor_links_schema) || nil
|
|
17
|
-
@anchor_meta_schema = resource_klass.try(:anchor_meta_schema) || nil
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def express(...)
|
|
21
|
-
raise NotImplementedError
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
private
|
|
25
|
-
|
|
26
|
-
delegate :convert_case, to: Anchor::Types
|
|
27
|
-
|
|
28
|
-
def schema_fetchable_fields(context:, include_all_fields:)
|
|
29
|
-
return fields unless statically_determinable_fetchable_fields? && !include_all_fields
|
|
30
|
-
@resource_klass.anchor_fetchable_fields(context)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def statically_determinable_fetchable_fields?
|
|
34
|
-
@resource_klass.singleton_class.method_defined?(:anchor_fetchable_fields)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
# @return [Anchor::Types::Property]
|
|
38
|
-
def id_property
|
|
39
|
-
# TODO: resource_key_type can also return a proc
|
|
40
|
-
res_key_type = case resource_key_type
|
|
41
|
-
when :integer then Anchor::Types::Integer
|
|
42
|
-
else Anchor::Types::String
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
Anchor::Types::Property.new(:id, res_key_type)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# @return [Anchor::Types::Property]
|
|
49
|
-
def type_property
|
|
50
|
-
Anchor::Types::Property.new(:type, Anchor::Types::Literal.new(_type))
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
# @param included_fields [Array<Symbol>]
|
|
54
|
-
# @return [Array<Anchor::Types::Property>]
|
|
55
|
-
def anchor_attributes_properties(included_fields:)
|
|
56
|
-
_attributes.except(:id).filter_map do |attr, options|
|
|
57
|
-
next if included_fields.exclude?(attr.to_sym)
|
|
58
|
-
description = @anchor_attributes_descriptions[attr]
|
|
59
|
-
next Anchor::Types::Property.new(
|
|
60
|
-
convert_case(attr),
|
|
61
|
-
@anchor_attributes[attr],
|
|
62
|
-
false,
|
|
63
|
-
description,
|
|
64
|
-
) if @anchor_attributes.key?(attr)
|
|
65
|
-
|
|
66
|
-
type = begin
|
|
67
|
-
model_method = options[:delegate] || attr
|
|
68
|
-
resource_method = attr
|
|
69
|
-
|
|
70
|
-
model_method_defined = _model_class.try(
|
|
71
|
-
:method_defined?,
|
|
72
|
-
model_method.to_sym,
|
|
73
|
-
) && !_model_class.instance_method(model_method.to_sym)
|
|
74
|
-
.owner.is_a?(ActiveRecord::AttributeMethods::GeneratedAttributeMethods)
|
|
75
|
-
resource_method_defined = @anchor_method_added_count[resource_method.to_sym] > 1
|
|
76
|
-
serializer_defined = (_model_class.try(:attribute_types) || {})[model_method.to_s].respond_to?(:coder)
|
|
77
|
-
method_defined = model_method_defined || resource_method_defined || serializer_defined
|
|
78
|
-
|
|
79
|
-
enum = Anchor.config.infer_ar_enums && !method_defined && _model_class.try(:defined_enums).try(:[], model_method.to_s)
|
|
80
|
-
column = !method_defined && _model_class.try(:columns_hash).try(:[], model_method.to_s)
|
|
81
|
-
|
|
82
|
-
if column
|
|
83
|
-
type = Anchor::Types::Inference::ActiveRecord::SQL.from(column)
|
|
84
|
-
|
|
85
|
-
if enum
|
|
86
|
-
enum_type = Anchor::Types::Union.new(enum.map { |_key, val| Anchor::Types::Literal.new(val) })
|
|
87
|
-
type = type.is_a?(Anchor::Types::Maybe) ? Anchor::Types::Maybe.new(enum_type) : enum_type
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
unless description
|
|
91
|
-
description = column.comment if Anchor.config.use_active_record_comment
|
|
92
|
-
if description && !Anchor.config.ar_comment_to_string.nil?
|
|
93
|
-
description = Anchor.config.ar_comment_to_string.call(description)
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
check_presence = type.is_a?(Anchor::Types::Maybe) && Anchor.config.use_active_record_validations
|
|
97
|
-
if check_presence && _model_class.validators_on(model_method).any? do |v|
|
|
98
|
-
if v.is_a?(ActiveRecord::Validations::NumericalityValidator)
|
|
99
|
-
opts = v.options.with_indifferent_access
|
|
100
|
-
!(opts[:allow_nil] || opts[:if] || opts[:unless] || opts[:on])
|
|
101
|
-
elsif v.is_a?(ActiveRecord::Validations::PresenceValidator)
|
|
102
|
-
opts = v.options.with_indifferent_access
|
|
103
|
-
!(opts[:if] || opts[:unless] || opts[:on])
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
type.type
|
|
107
|
-
elsif type.is_a?(Anchor::Types::Maybe) && Anchor.config.infer_default_as_non_null
|
|
108
|
-
column.default.present? || column.default_function.present? && column.instance_variable_get(:@generated).blank? ? type.type : type
|
|
109
|
-
else
|
|
110
|
-
type
|
|
111
|
-
end
|
|
112
|
-
else
|
|
113
|
-
Anchor::Types::Unknown
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
Anchor::Types::Property.new(convert_case(attr), type, false, description)
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
# @param included_fields [Array<Symbol>]
|
|
122
|
-
# @return [Anchor::Types::Property, NilClass]
|
|
123
|
-
def anchor_relationships_property(included_fields:)
|
|
124
|
-
anchor_relationships_properties(included_fields:).then do |properties|
|
|
125
|
-
break if properties.blank?
|
|
126
|
-
Anchor::Types::Property.new(:relationships, Anchor::Types::Object.new(properties))
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def anchor_links_property
|
|
131
|
-
if @anchor_links_schema
|
|
132
|
-
Anchor::Types::Property.new("links", @anchor_links_schema, false)
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def anchor_meta_property
|
|
137
|
-
if @anchor_meta_schema
|
|
138
|
-
Anchor::Types::Property.new("meta", @anchor_meta_schema, false)
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# @param included_fields [Array<Symbol>]
|
|
143
|
-
# @return [Array<Anchor::Types::Property>]
|
|
144
|
-
def anchor_relationships_properties(included_fields:)
|
|
145
|
-
_relationships.filter_map do |name, rel|
|
|
146
|
-
next if included_fields.exclude?(name.to_sym)
|
|
147
|
-
description = @anchor_relationships_descriptions[name]
|
|
148
|
-
relationship_type = relationship_type_for(rel, rel.resource_klass, name) if @anchor_relationships.exclude?(name)
|
|
149
|
-
|
|
150
|
-
relationship_type ||= begin
|
|
151
|
-
anchor_relationship = @anchor_relationships[name]
|
|
152
|
-
|
|
153
|
-
type = if (resources = anchor_relationship.resources)
|
|
154
|
-
references = resources.map do |resource_klass|
|
|
155
|
-
Anchor::Types::Reference.new(resource_klass.anchor_schema_name)
|
|
156
|
-
end
|
|
157
|
-
null_type = Array.wrap(anchor_relationship.null_elements.presence && Anchor::Types::Null)
|
|
158
|
-
Anchor::Types::Union.new(references + null_type)
|
|
159
|
-
else
|
|
160
|
-
Anchor::Types::Reference.new(anchor_relationship.resource.anchor_schema_name)
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
type = Anchor::Types::Inference::JSONAPI.wrapper_from_relationship(rel).call(type)
|
|
164
|
-
anchor_relationship.null.present? ? Anchor::Types::Maybe.new(type) : type
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
use_optional = Anchor.config.infer_nullable_relationships_as_optional
|
|
168
|
-
if use_optional && relationship_type.is_a?(Anchor::Types::Maybe)
|
|
169
|
-
Anchor::Types::Property.new(convert_case(name), relationship_type.type, true, description)
|
|
170
|
-
else
|
|
171
|
-
Anchor::Types::Property.new(convert_case(name), relationship_type, false, description)
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
# @param rel [Relationship]
|
|
177
|
-
# @param resource_klass [Anchor::Resource]
|
|
178
|
-
# @param name [String, Symbol]
|
|
179
|
-
# @return [Anchor::Types::Reference, Anchor::Types::Array<Anchor::Types::Reference>, Anchor::Types::Maybe<Anchor::Types::Reference>, Anchor::Types::Union<Anchor::Types::Reference>]
|
|
180
|
-
def relationship_type_for(rel, resource_klass, name)
|
|
181
|
-
rel_type = if rel.polymorphic? && rel.respond_to?(:polymorphic_types) # 0.11.0.beta2
|
|
182
|
-
resource_klasses = rel.polymorphic_types.map { |t| resource_klass_for(t) }
|
|
183
|
-
Anchor::Types::Union.new(resource_klasses.map { |rk| Anchor::Types::Reference.new(rk.anchor_schema_name) })
|
|
184
|
-
elsif rel.polymorphic? && rel.class.respond_to?(:polymorphic_types) # TODO: < 0.11.0.beta2
|
|
185
|
-
resource_klasses = rel.class.polymorphic_types.map { |t| resource_klass_for(t) }
|
|
186
|
-
Anchor::Types::Union.new(resource_klasses.map { |rk| Anchor::Types::Reference.new(rk.anchor_schema_name) })
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
rel_type ||= Anchor::Types::Reference.new(resource_klass.anchor_schema_name)
|
|
190
|
-
model_relationship_name = (rel.options[:relation_name] || name).to_s
|
|
191
|
-
reflection = _model_class.try(:reflections).try(:[], model_relationship_name)
|
|
192
|
-
wrapper = if reflection
|
|
193
|
-
Anchor::Types::Inference::ActiveRecord.wrapper_from_reflection(reflection)
|
|
194
|
-
else
|
|
195
|
-
Anchor::Types::Inference::JSONAPI.wrapper_from_relationship(rel)
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
wrapper.call(rel_type)
|
|
199
|
-
end
|
|
200
|
-
end
|
|
201
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
module Anchor::Types::Inference
|
|
2
|
-
module JSONAPI
|
|
3
|
-
class << self
|
|
4
|
-
# @return [Proc{Type => Type, Anchor::Types::Array<Type>}]
|
|
5
|
-
def wrapper_from_relationship(relationship)
|
|
6
|
-
case relationship
|
|
7
|
-
when ::JSONAPI::Relationship::ToOne then ->(type) { type }
|
|
8
|
-
when ::JSONAPI::Relationship::ToMany then ->(type) { Anchor::Types::Array.new(type) }
|
|
9
|
-
else raise "#{relationship.class.name} not supported"
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|