sober_swag 0.19.0 → 0.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/benchmark.yml +39 -0
- data/.github/workflows/lint.yml +2 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.gitignore +3 -0
- data/.rubocop.yml +6 -1
- data/.yardopts +7 -0
- data/CHANGELOG.md +18 -0
- data/Gemfile +12 -0
- data/README.md +1 -1
- data/bench/benchmark.rb +34 -0
- data/bench/benchmarks/basic_field_serializer.rb +21 -0
- data/bench/benchmarks/view_selection.rb +47 -0
- data/bin/console +30 -10
- data/docs/reporting.md +190 -0
- data/docs/serializers.md +4 -1
- data/example/Gemfile +2 -2
- data/example/Gemfile.lock +94 -101
- data/example/app/controllers/application_controller.rb +4 -0
- data/example/app/controllers/people_controller.rb +44 -28
- data/example/app/output_objects/identified_output.rb +7 -0
- data/example/app/output_objects/person_output_object.rb +37 -11
- data/example/app/output_objects/post_output_object.rb +0 -4
- data/example/app/output_objects/reporting_post_output.rb +18 -0
- data/example/bin/rspec +29 -0
- data/example/config/environments/production.rb +1 -1
- data/example/spec/requests/people/create_spec.rb +3 -2
- data/example/spec/requests/people/index_spec.rb +1 -1
- data/lib/sober_swag/compiler/path.rb +45 -4
- data/lib/sober_swag/compiler/paths.rb +20 -0
- data/lib/sober_swag/compiler/primitive.rb +17 -0
- data/lib/sober_swag/compiler/type.rb +105 -22
- data/lib/sober_swag/compiler.rb +87 -15
- data/lib/sober_swag/controller/route.rb +147 -28
- data/lib/sober_swag/controller.rb +57 -17
- data/lib/sober_swag/input_object.rb +124 -7
- data/lib/sober_swag/nodes/array.rb +19 -0
- data/lib/sober_swag/nodes/attribute.rb +45 -4
- data/lib/sober_swag/nodes/base.rb +27 -7
- data/lib/sober_swag/nodes/binary.rb +30 -13
- data/lib/sober_swag/nodes/enum.rb +16 -1
- data/lib/sober_swag/nodes/list.rb +20 -0
- data/lib/sober_swag/nodes/nullable_primitive.rb +3 -0
- data/lib/sober_swag/nodes/object.rb +4 -1
- data/lib/sober_swag/nodes/one_of.rb +11 -3
- data/lib/sober_swag/nodes/primitive.rb +34 -2
- data/lib/sober_swag/nodes/sum.rb +8 -0
- data/lib/sober_swag/output_object/definition.rb +57 -1
- data/lib/sober_swag/output_object/field.rb +31 -11
- data/lib/sober_swag/output_object/field_syntax.rb +19 -3
- data/lib/sober_swag/output_object/view.rb +46 -1
- data/lib/sober_swag/output_object.rb +40 -19
- data/lib/sober_swag/parser.rb +7 -1
- data/lib/sober_swag/reporting/compiler.rb +39 -0
- data/lib/sober_swag/reporting/input/base.rb +11 -0
- data/lib/sober_swag/reporting/input/bool.rb +19 -0
- data/lib/sober_swag/reporting/input/converting/bool.rb +24 -0
- data/lib/sober_swag/reporting/input/converting/date.rb +30 -0
- data/lib/sober_swag/reporting/input/converting/date_time.rb +28 -0
- data/lib/sober_swag/reporting/input/converting/decimal.rb +24 -0
- data/lib/sober_swag/reporting/input/converting/integer.rb +19 -0
- data/lib/sober_swag/reporting/input/converting.rb +16 -0
- data/lib/sober_swag/reporting/input/defer.rb +29 -0
- data/lib/sober_swag/reporting/input/described.rb +38 -0
- data/lib/sober_swag/reporting/input/dictionary.rb +37 -0
- data/lib/sober_swag/reporting/input/either.rb +51 -0
- data/lib/sober_swag/reporting/input/enum.rb +44 -0
- data/lib/sober_swag/reporting/input/format.rb +39 -0
- data/lib/sober_swag/reporting/input/in_range.rb +61 -0
- data/lib/sober_swag/reporting/input/interface.rb +113 -0
- data/lib/sober_swag/reporting/input/list.rb +44 -0
- data/lib/sober_swag/reporting/input/mapped.rb +36 -0
- data/lib/sober_swag/reporting/input/merge_objects.rb +72 -0
- data/lib/sober_swag/reporting/input/multiple_of.rb +36 -0
- data/lib/sober_swag/reporting/input/null.rb +34 -0
- data/lib/sober_swag/reporting/input/number.rb +19 -0
- data/lib/sober_swag/reporting/input/object/property.rb +53 -0
- data/lib/sober_swag/reporting/input/object.rb +100 -0
- data/lib/sober_swag/reporting/input/pattern.rb +46 -0
- data/lib/sober_swag/reporting/input/referenced.rb +38 -0
- data/lib/sober_swag/reporting/input/struct.rb +271 -0
- data/lib/sober_swag/reporting/input/text.rb +42 -0
- data/lib/sober_swag/reporting/input.rb +56 -0
- data/lib/sober_swag/reporting/invalid_schema_error.rb +21 -0
- data/lib/sober_swag/reporting/output/base.rb +25 -0
- data/lib/sober_swag/reporting/output/bool.rb +25 -0
- data/lib/sober_swag/reporting/output/defer.rb +69 -0
- data/lib/sober_swag/reporting/output/described.rb +42 -0
- data/lib/sober_swag/reporting/output/dictionary.rb +46 -0
- data/lib/sober_swag/reporting/output/enum.rb +47 -0
- data/lib/sober_swag/reporting/output/interface.rb +89 -0
- data/lib/sober_swag/reporting/output/list.rb +54 -0
- data/lib/sober_swag/reporting/output/merge_objects.rb +97 -0
- data/lib/sober_swag/reporting/output/null.rb +25 -0
- data/lib/sober_swag/reporting/output/number.rb +25 -0
- data/lib/sober_swag/reporting/output/object/property.rb +45 -0
- data/lib/sober_swag/reporting/output/object.rb +54 -0
- data/lib/sober_swag/reporting/output/partitioned.rb +77 -0
- data/lib/sober_swag/reporting/output/pattern.rb +50 -0
- data/lib/sober_swag/reporting/output/referenced.rb +42 -0
- data/lib/sober_swag/reporting/output/struct.rb +262 -0
- data/lib/sober_swag/reporting/output/text.rb +25 -0
- data/lib/sober_swag/reporting/output/via_map.rb +67 -0
- data/lib/sober_swag/reporting/output/viewed.rb +72 -0
- data/lib/sober_swag/reporting/output.rb +55 -0
- data/lib/sober_swag/reporting/report/base.rb +57 -0
- data/lib/sober_swag/reporting/report/either.rb +36 -0
- data/lib/sober_swag/reporting/report/error.rb +15 -0
- data/lib/sober_swag/reporting/report/list.rb +28 -0
- data/lib/sober_swag/reporting/report/merged_object.rb +25 -0
- data/lib/sober_swag/reporting/report/object.rb +29 -0
- data/lib/sober_swag/reporting/report/output.rb +14 -0
- data/lib/sober_swag/reporting/report/value.rb +28 -0
- data/lib/sober_swag/reporting/report.rb +16 -0
- data/lib/sober_swag/reporting.rb +11 -0
- data/lib/sober_swag/serializer/array.rb +27 -3
- data/lib/sober_swag/serializer/base.rb +75 -25
- data/lib/sober_swag/serializer/conditional.rb +33 -1
- data/lib/sober_swag/serializer/field_list.rb +23 -5
- data/lib/sober_swag/serializer/hash.rb +53 -0
- data/lib/sober_swag/serializer/mapped.rb +10 -1
- data/lib/sober_swag/serializer/optional.rb +18 -1
- data/lib/sober_swag/serializer/primitive.rb +3 -0
- data/lib/sober_swag/serializer.rb +1 -0
- data/lib/sober_swag/server.rb +5 -1
- data/lib/sober_swag/type/named.rb +14 -0
- data/lib/sober_swag/types/comma_array.rb +4 -0
- data/lib/sober_swag/version.rb +1 -1
- data/lib/sober_swag.rb +7 -1
- metadata +74 -2
@@ -1,8 +1,16 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
module Nodes
|
3
3
|
##
|
4
|
-
#
|
4
|
+
# This is a node for one attribute of an object.
|
5
|
+
# An object type is represented by a `SoberSwag::Nodes::Object` full of these keys.
|
6
|
+
#
|
7
|
+
#
|
5
8
|
class Attribute < Base
|
9
|
+
##
|
10
|
+
# @param key [Symbol] the key of this attribute
|
11
|
+
# @param required [Boolean] if this attribute must be set or not
|
12
|
+
# @param value [Class] the type of this attribute
|
13
|
+
# @param meta [Hash] the metadata associated with this attribute
|
6
14
|
def initialize(key, required, value, meta = {})
|
7
15
|
@key = key
|
8
16
|
@required = required
|
@@ -10,22 +18,55 @@ module SoberSwag
|
|
10
18
|
@meta = meta
|
11
19
|
end
|
12
20
|
|
21
|
+
##
|
22
|
+
# Deconstruct into the {#key}, {#required}, {#value}, and {#meta} attributes
|
23
|
+
# of this {Attribute} object.
|
24
|
+
#
|
25
|
+
# @return [Array(Symbol, Boolean, Class, Hash)] the attributes of this object
|
13
26
|
def deconstruct
|
14
27
|
[key, required, value, meta]
|
15
28
|
end
|
16
29
|
|
17
|
-
|
30
|
+
##
|
31
|
+
# Deconstructs into {#key}, {#required}, {#value}, and {#meta} attributes, as a
|
32
|
+
# hash with the attribute names as the keys.
|
33
|
+
#
|
34
|
+
# @param _keys [void] ignored
|
35
|
+
# @return [Hash] the attributes as keys.
|
36
|
+
def deconstruct_keys(_keys)
|
18
37
|
{ key: key, required: required, value: value, meta: meta }
|
19
38
|
end
|
20
39
|
|
21
|
-
|
40
|
+
##
|
41
|
+
# @return [Symbol]
|
42
|
+
attr_reader :key
|
22
43
|
|
44
|
+
##
|
45
|
+
# @return [Boolean] true if this attribute must be set, false otherwise.
|
46
|
+
attr_reader :required
|
47
|
+
|
48
|
+
##
|
49
|
+
# @return [Class] the type of this attribute
|
50
|
+
attr_reader :value
|
51
|
+
|
52
|
+
##
|
53
|
+
# @return [Hash] the metadata for this attribute.
|
54
|
+
attr_reader :meta
|
55
|
+
|
56
|
+
##
|
57
|
+
# @see SoberSwag::Nodes::Base#map
|
23
58
|
def map(&block)
|
24
59
|
self.class.new(key, required, value.map(&block), meta)
|
25
60
|
end
|
26
61
|
|
62
|
+
##
|
63
|
+
# @see SoberSwag::Nodes::Base#cata
|
27
64
|
def cata(&block)
|
28
|
-
block.call(
|
65
|
+
block.call(
|
66
|
+
self.class.new(
|
67
|
+
key, required, value.cata(&block), meta
|
68
|
+
)
|
69
|
+
)
|
29
70
|
end
|
30
71
|
end
|
31
72
|
end
|
@@ -1,28 +1,44 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
module Nodes
|
3
3
|
##
|
4
|
+
# @abstract
|
4
5
|
# Base Node that all other nodes inherit from.
|
5
6
|
# All nodes should define the following:
|
6
7
|
#
|
7
|
-
#
|
8
|
+
#
|
9
|
+
# - `#deconstruct`, which returns an array of *everything needed to identify the node.*
|
8
10
|
# We base comparisons on the result of deconstruction.
|
9
|
-
# -
|
11
|
+
# - `#deconstruct_keys`, which returns a hash of *everything needed to identify the node*.
|
10
12
|
# We use this later.
|
11
13
|
class Base
|
12
14
|
include Comparable
|
13
15
|
|
14
16
|
##
|
15
17
|
# Value-level comparison.
|
18
|
+
# Returns `-1`, `0`, or `+1`
|
19
|
+
# if this object is less than, equal to, or greater than `other`.
|
20
|
+
#
|
21
|
+
# @param other [Object] the other object
|
22
|
+
#
|
23
|
+
# @return [Integer]
|
24
|
+
# comparison result
|
16
25
|
def <=>(other)
|
17
|
-
return other.class.name <=> self.class.name unless other.
|
26
|
+
return other.class.name <=> self.class.name unless other.instance_of?(self.class)
|
18
27
|
|
19
28
|
deconstruct <=> other.deconstruct
|
20
29
|
end
|
21
30
|
|
31
|
+
##
|
32
|
+
# Is this object equal to the other object?
|
33
|
+
# @param other [Object] the object to compare
|
34
|
+
# @return [Boolean] yes or no
|
22
35
|
def eql?(other)
|
23
36
|
deconstruct == other.deconstruct
|
24
37
|
end
|
25
38
|
|
39
|
+
##
|
40
|
+
# Standard hash key.
|
41
|
+
# @return [Integer]
|
26
42
|
def hash
|
27
43
|
deconstruct.hash
|
28
44
|
end
|
@@ -37,17 +53,21 @@ module SoberSwag
|
|
37
53
|
#
|
38
54
|
# When working with these definition nodes, we very often want to transform something recursively.
|
39
55
|
# This method allows us to do so by focusing on a single level at a time, keeping the actual recursion *abstract*.
|
56
|
+
#
|
57
|
+
# @yieldparam node nodes contained within this node, from the bottom-up.
|
58
|
+
# This block will first transform the innermost node, then the second layer, and so on, until we get to the node you originally called `#cata` on.
|
59
|
+
# @yieldreturn [Object] the object you wish to transform into.
|
40
60
|
def cata
|
41
61
|
raise ArgumentError, 'Base is abstract'
|
42
62
|
end
|
43
63
|
|
64
|
+
##
|
65
|
+
# Map over the inner, contained type of this node.
|
66
|
+
# This will *only* map over values wrapped in a `SoberSwag::Nodes::Primitive` object.
|
67
|
+
# Unlike {#cata}, it does not transform the entire node tree.
|
44
68
|
def map
|
45
69
|
raise ArgumentError, 'Base is abstract'
|
46
70
|
end
|
47
|
-
|
48
|
-
def flatten_one_ofs
|
49
|
-
raise ArgumentError, 'Base is abstract'
|
50
|
-
end
|
51
71
|
end
|
52
72
|
end
|
53
73
|
end
|
@@ -1,37 +1,43 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
module Nodes
|
3
3
|
##
|
4
|
-
# A
|
5
|
-
#
|
6
|
-
# It's cool I promise.
|
4
|
+
# A binary node: has a left and right hand side.
|
5
|
+
# Basically a node of a binary tree.
|
7
6
|
class Binary < Base
|
7
|
+
##
|
8
|
+
# @param lhs [SoberSwag::Nodes::Base] the left-hand node.
|
9
|
+
# @param rhs [SoberSwag::Nodes::Base] the right-hand node.
|
8
10
|
def initialize(lhs, rhs)
|
9
11
|
@lhs = lhs
|
10
12
|
@rhs = rhs
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
##
|
16
|
+
# @return [SoberSwag::Nodes::Base] the left-hand node
|
17
|
+
attr_reader :lhs
|
14
18
|
|
15
19
|
##
|
16
|
-
#
|
17
|
-
|
18
|
-
def map(&block)
|
19
|
-
self.class.new(
|
20
|
-
lhs.map(&block),
|
21
|
-
rhs.map(&block)
|
22
|
-
)
|
23
|
-
end
|
20
|
+
# @return [SoberSwag::Nodes::Base] the right-hand node
|
21
|
+
attr_reader :rhs
|
24
22
|
|
23
|
+
##
|
24
|
+
# Deconstructs into an array of `[lhs, rhs]`
|
25
|
+
#
|
26
|
+
# @return [Array(SoberSwag::Nodes::Base, SoberSwag::Nodes::Base)]
|
25
27
|
def deconstruct
|
26
28
|
[lhs, rhs]
|
27
29
|
end
|
28
30
|
|
31
|
+
##
|
32
|
+
# Deconstruct into a hash of attributes.
|
29
33
|
def deconstruct_keys(_keys)
|
30
34
|
{ lhs: lhs, rhs: rhs }
|
31
35
|
end
|
32
36
|
|
33
37
|
##
|
34
|
-
#
|
38
|
+
# @see SoberSwag::Nodes::Base#cata
|
39
|
+
#
|
40
|
+
# Maps over the LHS side first, then the RHS side, then the root.
|
35
41
|
def cata(&block)
|
36
42
|
block.call(
|
37
43
|
self.class.new(
|
@@ -40,6 +46,17 @@ module SoberSwag
|
|
40
46
|
)
|
41
47
|
)
|
42
48
|
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# @see SoberSwag::Nodes::Base#map
|
52
|
+
#
|
53
|
+
# Maps over the LHS side first, then the RHS side, then the root.
|
54
|
+
def map(&block)
|
55
|
+
self.class.new(
|
56
|
+
lhs.map(&block),
|
57
|
+
rhs.map(&block)
|
58
|
+
)
|
59
|
+
end
|
43
60
|
end
|
44
61
|
end
|
45
62
|
end
|
@@ -2,26 +2,41 @@ module SoberSwag
|
|
2
2
|
module Nodes
|
3
3
|
##
|
4
4
|
# Compiler node to represent an enum value.
|
5
|
-
# Enums are special enough to have their own node.
|
5
|
+
# Enums are special enough to have their own node, as they are basically a constant list of always-string values.
|
6
6
|
class Enum < Base
|
7
7
|
def initialize(values)
|
8
8
|
@values = values
|
9
9
|
end
|
10
10
|
|
11
|
+
##
|
12
|
+
# @return [Array<Symbol,String>] values of the enum.
|
11
13
|
attr_reader :values
|
12
14
|
|
15
|
+
##
|
16
|
+
# Since there is nothing to map over, this node will never actually call the block given.
|
17
|
+
#
|
18
|
+
# @see SoberSwag::Nodes::Base#map
|
13
19
|
def map
|
14
20
|
dup
|
15
21
|
end
|
16
22
|
|
23
|
+
##
|
24
|
+
# Deconstructs into the enum values.
|
25
|
+
#
|
26
|
+
# @return [Array(Array<Symbol,String>)] the cases of the enum.
|
17
27
|
def deconstruct
|
18
28
|
[values]
|
19
29
|
end
|
20
30
|
|
31
|
+
##
|
32
|
+
# @return [Hash{Symbol => Array<Symbol,String>}]
|
33
|
+
# the values, wrapped in a `values:` key.
|
21
34
|
def deconstruct_keys(_keys)
|
22
35
|
{ values: values }
|
23
36
|
end
|
24
37
|
|
38
|
+
##
|
39
|
+
# @see SoberSwag::Nodes::Base#cata
|
25
40
|
def cata(&block)
|
26
41
|
block.call(dup)
|
27
42
|
end
|
@@ -6,21 +6,37 @@ module SoberSwag
|
|
6
6
|
# Unlike {SoberSwag::Nodes::Array}, this actually models arrays.
|
7
7
|
# The other one is a node that *is* an array in terms of what it contains.
|
8
8
|
# Kinda confusing, but oh well.
|
9
|
+
#
|
10
|
+
# @todo swap the names of this and {SoberSwag::Nodes::Array} so it's less confusing.
|
9
11
|
class List < Base
|
12
|
+
##
|
13
|
+
# Initialize with a node representing the type of elements in the list.
|
14
|
+
# @param element [SoberSwag::Nodes::Base] the type
|
10
15
|
def initialize(element)
|
11
16
|
@element = element
|
12
17
|
end
|
13
18
|
|
19
|
+
##
|
20
|
+
# @return [SoberSwag::Nodes::Base]
|
14
21
|
attr_reader :element
|
15
22
|
|
23
|
+
##
|
24
|
+
# @return [Array(SoberSwag::Nodes::Base)]
|
16
25
|
def deconstruct
|
17
26
|
[element]
|
18
27
|
end
|
19
28
|
|
29
|
+
##
|
30
|
+
# @return [Hash{Symbol => SoberSwag::Nodes::Base}]
|
31
|
+
# the contained type wrapped in an `element:` key.
|
20
32
|
def deconstruct_keys(_)
|
21
33
|
{ element: element }
|
22
34
|
end
|
23
35
|
|
36
|
+
##
|
37
|
+
# @see SoberSwag::Nodes::Base#cata
|
38
|
+
#
|
39
|
+
# Maps over the element type, then this `List` type.
|
24
40
|
def cata(&block)
|
25
41
|
block.call(
|
26
42
|
self.class.new(
|
@@ -29,6 +45,10 @@ module SoberSwag
|
|
29
45
|
)
|
30
46
|
end
|
31
47
|
|
48
|
+
##
|
49
|
+
# @see SoberSwag::Nodes::Base#map
|
50
|
+
#
|
51
|
+
# Maps over the element type.
|
32
52
|
def map(&block)
|
33
53
|
self.class.new(
|
34
54
|
element.map(&block)
|
@@ -2,8 +2,11 @@ module SoberSwag
|
|
2
2
|
module Nodes
|
3
3
|
##
|
4
4
|
# Objects might have attribute keys, so they're
|
5
|
-
# basically a list of attributes
|
5
|
+
# basically a list of attributes.
|
6
6
|
class Object < SoberSwag::Nodes::Array
|
7
|
+
##
|
8
|
+
# @return [Hash{Symbol => Array<SoberSwag::Nodes::Attribute>}]
|
9
|
+
# the attributes, wrapped in an `attributes:` key.
|
7
10
|
def deconstruct_keys(_)
|
8
11
|
{ attributes: @elements }
|
9
12
|
end
|
@@ -1,11 +1,19 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
module Nodes
|
3
3
|
##
|
4
|
-
#
|
5
|
-
#
|
4
|
+
# OpenAPI v3 represents types that are a "choice" between multiple alternatives as an array.
|
5
|
+
# However, it is easier to model these as a sum type initially: if a type can be either an `A`, a `B`, or a `C`, we can model this as:
|
6
|
+
#
|
7
|
+
# `Sum.new(A, Sum.new(B, C))`.
|
8
|
+
#
|
9
|
+
# This means we only ever need to deal with two types at once.
|
10
|
+
# So, we initially serialize to a sum type, then later transform to this array type for further serialization.
|
6
11
|
class OneOf < ::SoberSwag::Nodes::Array
|
12
|
+
##
|
13
|
+
# @return [Hash{Symbol => SoberSwag::Nodes::Base}]
|
14
|
+
# the alternatives, wrapped in an `alternatives:` key.
|
7
15
|
def deconstruct_keys(_)
|
8
|
-
{ alternatives: @
|
16
|
+
{ alternatives: @elements }
|
9
17
|
end
|
10
18
|
end
|
11
19
|
end
|
@@ -1,27 +1,59 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
module Nodes
|
3
3
|
##
|
4
|
-
# Root node of the tree
|
4
|
+
# Root node of the tree.
|
5
|
+
# This contains "primitive values."
|
6
|
+
# Initially, this is probably the types of attributes or array elements or whatever.
|
7
|
+
# As we use {#cata} to transform this, it will start containing swagger-compatible type objects.
|
8
|
+
#
|
9
|
+
# This node can contain metadata as well.
|
5
10
|
class Primitive < Base
|
11
|
+
##
|
12
|
+
# @param value [Object] the primitive value to store
|
13
|
+
# @param metadata [Hash] the metadata
|
6
14
|
def initialize(value, metadata = {})
|
7
15
|
@value = value
|
8
16
|
@metadata = metadata
|
9
17
|
end
|
10
18
|
|
11
|
-
|
19
|
+
##
|
20
|
+
# @return [Object] the contained value
|
21
|
+
attr_reader :value
|
12
22
|
|
23
|
+
##
|
24
|
+
# @return [Hash] metadata associated with the contained value.
|
25
|
+
attr_reader :metadata
|
26
|
+
|
27
|
+
##
|
28
|
+
# @see SoberSwag::Nodes::Base#map
|
29
|
+
#
|
30
|
+
# This will actually map over {#value}.
|
13
31
|
def map(&block)
|
14
32
|
self.class.new(block.call(value), metadata.dup)
|
15
33
|
end
|
16
34
|
|
35
|
+
##
|
36
|
+
# Deconstructs to the value and the metadata.
|
37
|
+
#
|
38
|
+
# @return [Array(Object, Hash)] containing value and metadata.
|
17
39
|
def deconstruct
|
18
40
|
[value, metadata]
|
19
41
|
end
|
20
42
|
|
43
|
+
##
|
44
|
+
# Wraps the attributes in a hash.
|
45
|
+
#
|
46
|
+
# @return [Hash{Symbol => Object, Hash}]
|
47
|
+
# {#value} in `value:`, {#metadata} in `metadata:`
|
21
48
|
def deconstruct_keys(_)
|
22
49
|
{ value: value, metadata: metadata }
|
23
50
|
end
|
24
51
|
|
52
|
+
##
|
53
|
+
# @see SoberSwag::Nodes::Base#cata
|
54
|
+
# As this is a root node, we actually call the block directly.
|
55
|
+
# @yieldparam node [SoberSwag::Nodes::Primitive] this node.
|
56
|
+
# @return whatever the block returns.
|
25
57
|
def cata(&block)
|
26
58
|
block.call(self.class.new(value, metadata))
|
27
59
|
end
|
data/lib/sober_swag/nodes/sum.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
module SoberSwag
|
2
2
|
module Nodes
|
3
|
+
##
|
4
|
+
# A "Sum" type represents either one type or the other.
|
5
|
+
#
|
6
|
+
# It is called "Sum" because, if a type can be either type `A` or type `B`,
|
7
|
+
# the number of possible values for the type of `number_of_values(A) + number_of_values(B)`.
|
8
|
+
#
|
9
|
+
# Internally, this is primarily used when an object can be either one type or another.
|
10
|
+
# It will latter be flattened into {SoberSwag::Nodes::OneOf}
|
3
11
|
class Sum < Binary
|
4
12
|
end
|
5
13
|
end
|
@@ -9,14 +9,56 @@ module SoberSwag
|
|
9
9
|
@views = []
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
##
|
13
|
+
# @return [Array<SoberSwag::OutputObject::Field>]
|
14
|
+
attr_reader :fields
|
15
|
+
|
16
|
+
##
|
17
|
+
# @return [Array<SoberSwag::OutputObject::View>]
|
18
|
+
attr_reader :views
|
13
19
|
|
14
20
|
include FieldSyntax
|
15
21
|
|
22
|
+
##
|
23
|
+
# Adds a "type key", which is basically a field that will be
|
24
|
+
# serialized out to a constant value.
|
25
|
+
#
|
26
|
+
# This is useful if you have multiple types you may want to serialize out, and want consumers of your API to distinguish between them.
|
27
|
+
#
|
28
|
+
# By default this will have the key "type" but you can set it with the keyword arg.
|
29
|
+
# ```ruby
|
30
|
+
# type_key('MyObject')
|
31
|
+
# # is equivalent to
|
32
|
+
# field :type, SoberSwag::Serializer::Primitive.new(SoberSwag::Types::String.enum('MyObject')) do |_, _|
|
33
|
+
# 'MyObject'
|
34
|
+
# end
|
35
|
+
# ```
|
36
|
+
# @param str [String, Symbol] the value to serialize (will be converted to a string)
|
37
|
+
# @param
|
38
|
+
def type_key(str, field_name: :type)
|
39
|
+
str = str.to_s
|
40
|
+
field(
|
41
|
+
field_name,
|
42
|
+
SoberSwag::Serializer::Primitive.new(
|
43
|
+
SoberSwag::Types::String.enum(str)
|
44
|
+
)
|
45
|
+
) { |_, _| str }
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Adds a new field to the fields array
|
50
|
+
# @param field [SoberSwag::OutputObject::Field]
|
16
51
|
def add_field!(field)
|
17
52
|
@fields << field
|
18
53
|
end
|
19
54
|
|
55
|
+
##
|
56
|
+
# Define a new view, with the view DSL
|
57
|
+
# @param name [Symbol] the name of the view
|
58
|
+
# @param inherits [Symbol] the name of another view this
|
59
|
+
# view will "inherit" from
|
60
|
+
# @yieldself [SoberSwag::OutputObject::View]
|
61
|
+
# @return [nil] nothing interesting.
|
20
62
|
def view(name, inherits: nil, &block)
|
21
63
|
initial_fields =
|
22
64
|
if inherits.nil? || inherits == :base
|
@@ -31,6 +73,14 @@ module SoberSwag
|
|
31
73
|
@views << view
|
32
74
|
end
|
33
75
|
|
76
|
+
##
|
77
|
+
# @overload identifier()
|
78
|
+
# Get the identifier of this output object.
|
79
|
+
# @return [String] the identifier
|
80
|
+
# @overload identifier(arg)
|
81
|
+
# Set the identifier of this output object.
|
82
|
+
# @param arg [String] the external identifier to use for this OutputObject
|
83
|
+
# @return [String] `arg`
|
34
84
|
def identifier(arg = nil)
|
35
85
|
@identifier = arg if arg
|
36
86
|
@identifier
|
@@ -38,6 +88,12 @@ module SoberSwag
|
|
38
88
|
|
39
89
|
private
|
40
90
|
|
91
|
+
##
|
92
|
+
# Get the already-defined view with a specific name.
|
93
|
+
#
|
94
|
+
# @param name [Symbol] name of view to look up
|
95
|
+
# @return [SoberSwag::OutputObject::View] the view found
|
96
|
+
# @raise [ArgumentError] if no view with that name found
|
41
97
|
def find_view(name)
|
42
98
|
@views.find { |view| view.name == name } || (raise ArgumentError, "no view #{name.inspect} defined!")
|
43
99
|
end
|
@@ -4,6 +4,18 @@ module SoberSwag
|
|
4
4
|
# A single field in an output object.
|
5
5
|
# Later used to make an actual serializer from this.
|
6
6
|
class Field
|
7
|
+
##
|
8
|
+
# @param name [Symbol] the name of this field
|
9
|
+
# @param serializer [SoberSwag::Serializer::Base, Proc, Lambda] how to serialize
|
10
|
+
# the value in this field.
|
11
|
+
# If given a `Proc` or `Lambda`, the `Proc` or `Lambda` should return
|
12
|
+
# an instance of SoberSwag::Serializer::Base when called.
|
13
|
+
# @param from [Symbol] an optional parameter specifying
|
14
|
+
# that this field should be plucked "from" another
|
15
|
+
# attribute of a ruby object
|
16
|
+
# @param block [Proc] a proc to get this field from a serialized
|
17
|
+
# object. If not given, will try to grab an attribute
|
18
|
+
# with the same name, *or* with the name of `from:` if that was sent.
|
7
19
|
def initialize(name, serializer, from: nil, &block)
|
8
20
|
@name = name
|
9
21
|
@root_serializer = serializer
|
@@ -11,12 +23,18 @@ module SoberSwag
|
|
11
23
|
@block = block
|
12
24
|
end
|
13
25
|
|
26
|
+
##
|
27
|
+
# @return [Symbol] name of this field.
|
14
28
|
attr_reader :name
|
15
29
|
|
30
|
+
##
|
31
|
+
# @return [SoberSwag::Serializer::Base]
|
16
32
|
def serializer
|
17
33
|
@serializer ||= resolved_serializer.serializer.via_map(&transform_proc)
|
18
34
|
end
|
19
35
|
|
36
|
+
##
|
37
|
+
# @return [SoberSwag::Serializer::Base]
|
20
38
|
def resolved_serializer
|
21
39
|
if @root_serializer.is_a?(Proc)
|
22
40
|
@root_serializer.call
|
@@ -27,17 +45,19 @@ module SoberSwag
|
|
27
45
|
|
28
46
|
private
|
29
47
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
48
|
+
##
|
49
|
+
# @return [Proc]
|
50
|
+
def transform_proc
|
51
|
+
return @transform_proc if defined?(@transform_proc)
|
52
|
+
|
53
|
+
return @transform_proc = @block if @block
|
54
|
+
|
55
|
+
key = @from || @name
|
56
|
+
@transform_proc = proc do |object, _|
|
57
|
+
if object.respond_to?(key)
|
58
|
+
object.public_send(key)
|
59
|
+
else
|
60
|
+
object[key]
|
41
61
|
end
|
42
62
|
end
|
43
63
|
end
|
@@ -3,6 +3,13 @@ module SoberSwag
|
|
3
3
|
##
|
4
4
|
# Syntax for definitions that can add fields.
|
5
5
|
module FieldSyntax
|
6
|
+
##
|
7
|
+
# Defines a new field.
|
8
|
+
# @see SoberSwag::OutputObject::Field#initialize
|
9
|
+
# @param name [Symbol] name of this field
|
10
|
+
# @param serializer [SoberSwag::Serializer::Base] serializer to use for this field.
|
11
|
+
# @param from [Symbol] method name to extract this field from, for convenience.
|
12
|
+
# @param block [Proc] optional way to extract this field.
|
6
13
|
def field(name, serializer, from: nil, &block)
|
7
14
|
add_field!(Field.new(name, serializer, from: from, &block))
|
8
15
|
end
|
@@ -10,22 +17,31 @@ module SoberSwag
|
|
10
17
|
##
|
11
18
|
# Similar to #field, but adds multiple at once.
|
12
19
|
# Named #multi because #fields was already taken.
|
20
|
+
#
|
21
|
+
# @param names [Array<Symbol>] the field names to add.
|
22
|
+
# @param serializer [SoberSwag::Serializer::Base] the serializer to use for all fields.
|
13
23
|
def multi(names, serializer)
|
14
24
|
names.each { |name| field(name, serializer) }
|
15
25
|
end
|
16
26
|
|
17
27
|
##
|
18
28
|
# Given a symbol to this, we will use a primitive name
|
29
|
+
# @param name [Symbol] symbol to look up.
|
30
|
+
# @return [SoberSwag::Serializer::Base] serializer to use.
|
19
31
|
def primitive(name)
|
20
32
|
SoberSwag::Serializer.primitive(SoberSwag::Types.const_get(name))
|
21
33
|
end
|
22
34
|
|
23
35
|
##
|
24
36
|
# Merge in anything that has a list of fields, and use it.
|
25
|
-
# Note that merging in a full
|
26
|
-
|
37
|
+
# Note that merging in a full output object *will not* also merge in views, just fields defined on the base.
|
38
|
+
#
|
39
|
+
# @param other [#fields] a field container, like a {SoberSwag::OutputObject} or something
|
40
|
+
# @param except [Array<Symbol>] optionally exclude a field from the output object being merged
|
41
|
+
# @return [void]
|
42
|
+
def merge(other, except: [])
|
27
43
|
other.fields.each do |field|
|
28
|
-
add_field!(field)
|
44
|
+
add_field!(field) unless except.include?(field.name)
|
29
45
|
end
|
30
46
|
end
|
31
47
|
end
|