sober_swag 0.9.0 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sober_swag/compiler/type.rb +13 -10
- data/lib/sober_swag/input_object.rb +7 -0
- data/lib/sober_swag/output_object.rb +4 -0
- data/lib/sober_swag/output_object/view.rb +4 -0
- data/lib/sober_swag/parser.rb +2 -1
- data/lib/sober_swag/serializer/meta.rb +15 -4
- data/lib/sober_swag/version.rb +1 -1
- metadata +1 -2
- data/bin/console +0 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 234da6bca56fd440edae30e4fc9c34c50a37a6ab26b41739276e57ab43c73408
|
4
|
+
data.tar.gz: 103e61429365ceb04d6d0225822ddaa4012f95a043a5bb97c6b8a2fd26671ce5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58f27a7deebfcb50ec6279084e0a0e872fd0c2a447fca922549d7d3d9822a7e13e010372376c1a079472ba807845d267327275fe453efb99662f685321041e28
|
7
|
+
data.tar.gz: 97ecdb6352db7545004fba2103bb505b3730973a22689f05dedd5b6a305115a63f302f0f248edd3f70e6377ebe470c9ef4f734b2e38af031bf762d229c71bcc0
|
@@ -126,7 +126,9 @@ module SoberSwag
|
|
126
126
|
|
127
127
|
case type
|
128
128
|
when Class
|
129
|
-
|
129
|
+
# refs have to be standalone
|
130
|
+
# so to not interefere with our other stuff, do this horrible garbage
|
131
|
+
{ oneOf: [{ '$ref'.to_sym => self.class.get_ref(type) }] }
|
130
132
|
when Dry::Types::Constrained
|
131
133
|
self.class.new(type.type).schema_stub
|
132
134
|
when Dry::Types::Array::Member
|
@@ -220,19 +222,20 @@ module SoberSwag
|
|
220
222
|
else
|
221
223
|
[name, value]
|
222
224
|
end
|
223
|
-
# can't match on value directly as ruby uses `===` to match,
|
224
|
-
# and classes use `===` to mean `is an instance of`, as
|
225
|
-
# opposed to direct equality lmao
|
226
225
|
when Nodes::Primitive
|
227
226
|
value = object.value
|
228
227
|
metadata = object.metadata
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
228
|
+
type_def =
|
229
|
+
if self.class.primitive?(value)
|
230
|
+
self.class.primitive_def(value)
|
231
|
+
else
|
232
|
+
metadata.merge!(value.meta)
|
233
|
+
# refs have to be on their own, this is the stupid workaround
|
234
|
+
# so you can add descriptions and stuff
|
235
|
+
{ oneOf: [{ '$ref'.to_sym => self.class.get_ref(value) }] }
|
233
236
|
end
|
234
|
-
|
235
|
-
|
237
|
+
METADATA_KEYS.select(&metadata.method(:key?)).reduce(type_def) do |definition, key|
|
238
|
+
definition.merge(key => metadata[key])
|
236
239
|
end
|
237
240
|
else
|
238
241
|
raise ArgumentError, "Got confusing node #{object} (#{object.class})"
|
@@ -13,9 +13,16 @@ module SoberSwag
|
|
13
13
|
# The name to use for this type in external documentation.
|
14
14
|
def identifier(arg = nil)
|
15
15
|
@identifier = arg if arg
|
16
|
+
|
16
17
|
@identifier || name.to_s.gsub('::', '.')
|
17
18
|
end
|
18
19
|
|
20
|
+
def meta(*args)
|
21
|
+
super(*args).tap do |result|
|
22
|
+
result.identifier(identifier) if result.is_a?(Class) # pass on identifier
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
19
26
|
def primitive(sym)
|
20
27
|
SoberSwag::Types.const_get(sym)
|
21
28
|
end
|
data/lib/sober_swag/parser.rb
CHANGED
@@ -46,8 +46,9 @@ module SoberSwag
|
|
46
46
|
when Dry::Types::Constrained
|
47
47
|
bind(Parser.new(@node.type))
|
48
48
|
when Dry::Types::Nominal
|
49
|
+
old_meta = @node.primitive.respond_to?(:meta) ? @node.primitive.meta : {}
|
49
50
|
# start off with the moral equivalent of NodeTree[String]
|
50
|
-
Nodes::Primitive.new(@node.primitive, @node.meta)
|
51
|
+
Nodes::Primitive.new(@node.primitive, old_meta.merge(@node.meta))
|
51
52
|
else
|
52
53
|
# Inside of this case we have a class that is some user-defined type
|
53
54
|
# We put it in our array of found types, and consider it a primitive
|
@@ -6,25 +6,36 @@ module SoberSwag
|
|
6
6
|
class Meta < Base
|
7
7
|
def initialize(base, meta)
|
8
8
|
@base = base
|
9
|
-
@
|
9
|
+
@metadata = meta
|
10
|
+
@identifier = @base.identifier
|
10
11
|
end
|
11
12
|
|
12
|
-
attr_reader :base, :
|
13
|
+
attr_reader :base, :metadata
|
13
14
|
|
14
15
|
def serialize(args, opts = {})
|
15
16
|
base.serialize(args, opts)
|
16
17
|
end
|
17
18
|
|
19
|
+
def meta(hash)
|
20
|
+
self.class.new(base, metadata.merge(hash))
|
21
|
+
end
|
22
|
+
|
18
23
|
def lazy_type
|
19
|
-
@base.lazy_type.meta(**
|
24
|
+
@lazy_type ||= @base.lazy_type.meta(**metadata)
|
20
25
|
end
|
21
26
|
|
22
27
|
def type
|
23
|
-
@base.type.meta(**
|
28
|
+
@type ||= @base.type.meta(**metadata)
|
24
29
|
end
|
25
30
|
|
26
31
|
def finalize_lazy_type!
|
27
32
|
@base.finalize_lazy_type!
|
33
|
+
# Using .meta on dry-struct returns a *new type* that wraps the old one.
|
34
|
+
# As such, we need to be a bit clever about when we tack on the identifier
|
35
|
+
# for this type.
|
36
|
+
%i[lazy_type type].each do |sym|
|
37
|
+
public_send(sym).identifier(@base.public_send(sym).identifier) if @base.public_send(sym).respond_to?(:identifier)
|
38
|
+
end
|
28
39
|
end
|
29
40
|
|
30
41
|
def lazy_type?
|
data/lib/sober_swag/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sober_swag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.14.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Super
|
@@ -183,7 +183,6 @@ files:
|
|
183
183
|
- LICENSE.txt
|
184
184
|
- README.md
|
185
185
|
- Rakefile
|
186
|
-
- bin/console
|
187
186
|
- bin/setup
|
188
187
|
- docs/serializers.md
|
189
188
|
- example/.gitignore
|
data/bin/console
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'sober_swag'
|
6
|
-
|
7
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
-
# with your gem easier. You can also use a different console, if you like.
|
9
|
-
module Types
|
10
|
-
include Dry.Types()
|
11
|
-
end
|
12
|
-
|
13
|
-
Bio = SoberSwag.input_object do
|
14
|
-
attribute :name, primitive(:String).meta(description: 'A very basic bio name')
|
15
|
-
end
|
16
|
-
|
17
|
-
Person = SoberSwag.input_object do
|
18
|
-
attribute :name, primitive(:String).meta(description: 'The full name description')
|
19
|
-
attribute :age, param(:Integer).constrained(gt: 0).optional.meta(description: 'My cool age')
|
20
|
-
attribute? :mood, Types::String
|
21
|
-
end
|
22
|
-
|
23
|
-
##
|
24
|
-
# Demonstration of subclass-style.
|
25
|
-
class PersonSearch < SoberSwag::InputObject
|
26
|
-
attribute? :name, Types::String
|
27
|
-
attribute? :age, Types::Integer
|
28
|
-
end
|
29
|
-
|
30
|
-
##
|
31
|
-
# Demonstration of subclass-style *and* recursion in structs.
|
32
|
-
class LinkedList < SoberSwag::InputObject
|
33
|
-
attribute :value, Types::String
|
34
|
-
attribute? :next, LinkedList
|
35
|
-
end
|
36
|
-
|
37
|
-
Foo = SoberSwag::OutputObject.define do
|
38
|
-
field :name, primitive(:String)
|
39
|
-
field :age, primitive(:String)
|
40
|
-
|
41
|
-
view :foo do
|
42
|
-
field :bar, primitive(:String).optional
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
47
|
-
require 'pry'
|
48
|
-
Pry.start
|