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.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/benchmark.yml +39 -0
  3. data/.github/workflows/lint.yml +2 -4
  4. data/.github/workflows/ruby.yml +1 -1
  5. data/.gitignore +3 -0
  6. data/.rubocop.yml +6 -1
  7. data/.yardopts +7 -0
  8. data/CHANGELOG.md +18 -0
  9. data/Gemfile +12 -0
  10. data/README.md +1 -1
  11. data/bench/benchmark.rb +34 -0
  12. data/bench/benchmarks/basic_field_serializer.rb +21 -0
  13. data/bench/benchmarks/view_selection.rb +47 -0
  14. data/bin/console +30 -10
  15. data/docs/reporting.md +190 -0
  16. data/docs/serializers.md +4 -1
  17. data/example/Gemfile +2 -2
  18. data/example/Gemfile.lock +94 -101
  19. data/example/app/controllers/application_controller.rb +4 -0
  20. data/example/app/controllers/people_controller.rb +44 -28
  21. data/example/app/output_objects/identified_output.rb +7 -0
  22. data/example/app/output_objects/person_output_object.rb +37 -11
  23. data/example/app/output_objects/post_output_object.rb +0 -4
  24. data/example/app/output_objects/reporting_post_output.rb +18 -0
  25. data/example/bin/rspec +29 -0
  26. data/example/config/environments/production.rb +1 -1
  27. data/example/spec/requests/people/create_spec.rb +3 -2
  28. data/example/spec/requests/people/index_spec.rb +1 -1
  29. data/lib/sober_swag/compiler/path.rb +45 -4
  30. data/lib/sober_swag/compiler/paths.rb +20 -0
  31. data/lib/sober_swag/compiler/primitive.rb +17 -0
  32. data/lib/sober_swag/compiler/type.rb +105 -22
  33. data/lib/sober_swag/compiler.rb +87 -15
  34. data/lib/sober_swag/controller/route.rb +147 -28
  35. data/lib/sober_swag/controller.rb +57 -17
  36. data/lib/sober_swag/input_object.rb +124 -7
  37. data/lib/sober_swag/nodes/array.rb +19 -0
  38. data/lib/sober_swag/nodes/attribute.rb +45 -4
  39. data/lib/sober_swag/nodes/base.rb +27 -7
  40. data/lib/sober_swag/nodes/binary.rb +30 -13
  41. data/lib/sober_swag/nodes/enum.rb +16 -1
  42. data/lib/sober_swag/nodes/list.rb +20 -0
  43. data/lib/sober_swag/nodes/nullable_primitive.rb +3 -0
  44. data/lib/sober_swag/nodes/object.rb +4 -1
  45. data/lib/sober_swag/nodes/one_of.rb +11 -3
  46. data/lib/sober_swag/nodes/primitive.rb +34 -2
  47. data/lib/sober_swag/nodes/sum.rb +8 -0
  48. data/lib/sober_swag/output_object/definition.rb +57 -1
  49. data/lib/sober_swag/output_object/field.rb +31 -11
  50. data/lib/sober_swag/output_object/field_syntax.rb +19 -3
  51. data/lib/sober_swag/output_object/view.rb +46 -1
  52. data/lib/sober_swag/output_object.rb +40 -19
  53. data/lib/sober_swag/parser.rb +7 -1
  54. data/lib/sober_swag/reporting/compiler.rb +39 -0
  55. data/lib/sober_swag/reporting/input/base.rb +11 -0
  56. data/lib/sober_swag/reporting/input/bool.rb +19 -0
  57. data/lib/sober_swag/reporting/input/converting/bool.rb +24 -0
  58. data/lib/sober_swag/reporting/input/converting/date.rb +30 -0
  59. data/lib/sober_swag/reporting/input/converting/date_time.rb +28 -0
  60. data/lib/sober_swag/reporting/input/converting/decimal.rb +24 -0
  61. data/lib/sober_swag/reporting/input/converting/integer.rb +19 -0
  62. data/lib/sober_swag/reporting/input/converting.rb +16 -0
  63. data/lib/sober_swag/reporting/input/defer.rb +29 -0
  64. data/lib/sober_swag/reporting/input/described.rb +38 -0
  65. data/lib/sober_swag/reporting/input/dictionary.rb +37 -0
  66. data/lib/sober_swag/reporting/input/either.rb +51 -0
  67. data/lib/sober_swag/reporting/input/enum.rb +44 -0
  68. data/lib/sober_swag/reporting/input/format.rb +39 -0
  69. data/lib/sober_swag/reporting/input/in_range.rb +61 -0
  70. data/lib/sober_swag/reporting/input/interface.rb +113 -0
  71. data/lib/sober_swag/reporting/input/list.rb +44 -0
  72. data/lib/sober_swag/reporting/input/mapped.rb +36 -0
  73. data/lib/sober_swag/reporting/input/merge_objects.rb +72 -0
  74. data/lib/sober_swag/reporting/input/multiple_of.rb +36 -0
  75. data/lib/sober_swag/reporting/input/null.rb +34 -0
  76. data/lib/sober_swag/reporting/input/number.rb +19 -0
  77. data/lib/sober_swag/reporting/input/object/property.rb +53 -0
  78. data/lib/sober_swag/reporting/input/object.rb +100 -0
  79. data/lib/sober_swag/reporting/input/pattern.rb +46 -0
  80. data/lib/sober_swag/reporting/input/referenced.rb +38 -0
  81. data/lib/sober_swag/reporting/input/struct.rb +271 -0
  82. data/lib/sober_swag/reporting/input/text.rb +42 -0
  83. data/lib/sober_swag/reporting/input.rb +56 -0
  84. data/lib/sober_swag/reporting/invalid_schema_error.rb +21 -0
  85. data/lib/sober_swag/reporting/output/base.rb +25 -0
  86. data/lib/sober_swag/reporting/output/bool.rb +25 -0
  87. data/lib/sober_swag/reporting/output/defer.rb +69 -0
  88. data/lib/sober_swag/reporting/output/described.rb +42 -0
  89. data/lib/sober_swag/reporting/output/dictionary.rb +46 -0
  90. data/lib/sober_swag/reporting/output/enum.rb +47 -0
  91. data/lib/sober_swag/reporting/output/interface.rb +89 -0
  92. data/lib/sober_swag/reporting/output/list.rb +54 -0
  93. data/lib/sober_swag/reporting/output/merge_objects.rb +97 -0
  94. data/lib/sober_swag/reporting/output/null.rb +25 -0
  95. data/lib/sober_swag/reporting/output/number.rb +25 -0
  96. data/lib/sober_swag/reporting/output/object/property.rb +45 -0
  97. data/lib/sober_swag/reporting/output/object.rb +54 -0
  98. data/lib/sober_swag/reporting/output/partitioned.rb +77 -0
  99. data/lib/sober_swag/reporting/output/pattern.rb +50 -0
  100. data/lib/sober_swag/reporting/output/referenced.rb +42 -0
  101. data/lib/sober_swag/reporting/output/struct.rb +262 -0
  102. data/lib/sober_swag/reporting/output/text.rb +25 -0
  103. data/lib/sober_swag/reporting/output/via_map.rb +67 -0
  104. data/lib/sober_swag/reporting/output/viewed.rb +72 -0
  105. data/lib/sober_swag/reporting/output.rb +55 -0
  106. data/lib/sober_swag/reporting/report/base.rb +57 -0
  107. data/lib/sober_swag/reporting/report/either.rb +36 -0
  108. data/lib/sober_swag/reporting/report/error.rb +15 -0
  109. data/lib/sober_swag/reporting/report/list.rb +28 -0
  110. data/lib/sober_swag/reporting/report/merged_object.rb +25 -0
  111. data/lib/sober_swag/reporting/report/object.rb +29 -0
  112. data/lib/sober_swag/reporting/report/output.rb +14 -0
  113. data/lib/sober_swag/reporting/report/value.rb +28 -0
  114. data/lib/sober_swag/reporting/report.rb +16 -0
  115. data/lib/sober_swag/reporting.rb +11 -0
  116. data/lib/sober_swag/serializer/array.rb +27 -3
  117. data/lib/sober_swag/serializer/base.rb +75 -25
  118. data/lib/sober_swag/serializer/conditional.rb +33 -1
  119. data/lib/sober_swag/serializer/field_list.rb +23 -5
  120. data/lib/sober_swag/serializer/hash.rb +53 -0
  121. data/lib/sober_swag/serializer/mapped.rb +10 -1
  122. data/lib/sober_swag/serializer/optional.rb +18 -1
  123. data/lib/sober_swag/serializer/primitive.rb +3 -0
  124. data/lib/sober_swag/serializer.rb +1 -0
  125. data/lib/sober_swag/server.rb +5 -1
  126. data/lib/sober_swag/type/named.rb +14 -0
  127. data/lib/sober_swag/types/comma_array.rb +4 -0
  128. data/lib/sober_swag/version.rb +1 -1
  129. data/lib/sober_swag.rb +7 -1
  130. metadata +74 -2
@@ -3,44 +3,87 @@ module SoberSwag
3
3
  ##
4
4
  # DSL for defining a view.
5
5
  # Used in `view` blocks within {SoberSwag::OutputObject.define}.
6
+ #
7
+ # Views are "variants" of {SoberSwag::OutputObject}s that contain
8
+ # different fields.
6
9
  class View < SoberSwag::Serializer::Base
10
+ ##
11
+ # Define a new view with the given base fields.
12
+ # @param name [Symbol] name for this view
13
+ # @param base_fields [Array<SoberSwag::OutputObject::Field>] fields already defined
14
+ # @yieldself [SoberSwag::OutputObject::View]
15
+ #
16
+ # @return [SoberSwag::OutputObject::View]
7
17
  def self.define(name, base_fields, &block)
8
18
  new(name, base_fields).tap do |view|
9
19
  view.instance_eval(&block)
10
20
  end
11
21
  end
12
22
 
23
+ ##
24
+ # An error thrown when you try to nest views inside views.
13
25
  class NestingError < Error; end
14
26
 
15
27
  include FieldSyntax
16
28
 
29
+ ##
30
+ # @param name [Sybmol] name for this view.
31
+ # @param base_fields [Array<SoberSwag::OutputObject::Field>] already-defined fields.
17
32
  def initialize(name, base_fields = [])
18
33
  @name = name
19
34
  @fields = base_fields.dup
20
35
  end
21
36
 
22
- attr_reader :name, :fields
37
+ ##
38
+ # @return [Symbol] the name of this view
39
+ attr_reader :name
40
+
41
+ ##
42
+ # @return [Array<SoberSwag::OutputObject::Fields>] the fields defined in this view.
43
+ attr_reader :fields
23
44
 
45
+ ##
46
+ # Serialize an object according to this view.
47
+ # @param object what to serialize
48
+ # @param opts [Hash] arbitrary options
49
+ # @return [Hash] the serialized result
24
50
  def serialize(obj, opts = {})
25
51
  serializer.serialize(obj, opts)
26
52
  end
27
53
 
54
+ ##
55
+ # Get the type of this view.
56
+ # @return [Class] the type, a subclass of {Dry::Struct}
28
57
  def type
29
58
  serializer.type
30
59
  end
31
60
 
61
+ ##
62
+ # Excludes a field with the given name from this view.
63
+ # @param name [Symbol] field to exclude.
64
+ # @return [nil] nothing interesting
32
65
  def except!(name)
33
66
  @fields.reject! { |f| f.name == name }
34
67
  end
35
68
 
69
+ ##
70
+ # Always raises {NestingError}
71
+ # @raise {NestingError} always
36
72
  def view(*)
37
73
  raise NestingError, 'no views in views'
38
74
  end
39
75
 
76
+ ##
77
+ # Adds a field do this view.
78
+ # @param field [SoberSwag::OutputObject::Field]
79
+ # @return [nil] nothing interesting
40
80
  def add_field!(field)
41
81
  @fields << field
42
82
  end
43
83
 
84
+ ##
85
+ # Pretty show for humans
86
+ # @return [String]
44
87
  def to_s
45
88
  "<SoberSwag::OutputObject::View(#{identifier})>"
46
89
  end
@@ -48,6 +91,8 @@ module SoberSwag
48
91
  ##
49
92
  # Get the serializer defined by this view.
50
93
  # WARNING: Don't add more fields after you call this.
94
+ #
95
+ # @return [SoberSwag::Serializer::FieldList]
51
96
  def serializer
52
97
  @serializer ||=
53
98
  SoberSwag::Serializer::FieldList.new(fields).tap { |s| s.identifier(identifier) }
@@ -5,7 +5,7 @@ module SoberSwag
5
5
  # Create a serializer that is heavily inspired by the "Blueprinter" library.
6
6
  # This allows you to make "views" and such inside.
7
7
  #
8
- # Under the hood, this is actually all based on {SoberSwag::Serialzier::Base}.
8
+ # Under the hood, this is actually all based on {SoberSwag::Serializer::Base}.
9
9
  class OutputObject < SoberSwag::Serializer::Base
10
10
  autoload(:Field, 'sober_swag/output_object/field')
11
11
  autoload(:Definition, 'sober_swag/output_object/definition')
@@ -20,7 +20,7 @@ module SoberSwag
20
20
  #
21
21
  # PersonSerializer = SoberSwag::OutputObject.define do
22
22
  # field :id, primitive(:Integer)
23
- # field :name, primtive(:String).optional
23
+ # field :name, primitive(:String).optional
24
24
  #
25
25
  # view :complex do
26
26
  # field :age, primitive(:Integer)
@@ -32,9 +32,12 @@ module SoberSwag
32
32
  # However, this is only a hack to get rid of the weird naming issue when
33
33
  # generating swagger from dry structs: their section of the schema area
34
34
  # is defined by their *Ruby Class Name*. In the future, if we get rid of this,
35
- # we might be able to keep this on the value-level, in which case {#define}
35
+ # we might be able to keep this on the value-level, in which case {.define}
36
36
  # can simply return an *instance* of SoberSwag::Serializer that does
37
37
  # the correct thing, with the name you give it. This works for now, though.
38
+ #
39
+ # @return [Class] the serializer generated.
40
+ # @yieldself [SoberSwag::OutputObject::Definition]
38
41
  def self.define(&block)
39
42
  d = Definition.new.tap do |o|
40
43
  o.instance_eval(&block)
@@ -42,28 +45,51 @@ module SoberSwag
42
45
  new(d.fields, d.views, d.identifier)
43
46
  end
44
47
 
48
+ ##
49
+ # @param fields [Array<SoberSwag::OutputObject::Field>] the fields for this OutputObject
50
+ # @param views [Array<SoberSwag::OutputObject::View>] the views for this OutputObject
51
+ # @param identifier [String] the external identifier for this OutputObject
45
52
  def initialize(fields, views, identifier)
46
53
  @fields = fields
47
54
  @views = views
48
55
  @identifier = identifier
49
56
  end
50
57
 
51
- attr_reader :fields, :views, :identifier
58
+ ##
59
+ # @return [Array<SoberSwag::OutputObject::Field>]
60
+ attr_reader :fields
61
+ ##
62
+ # @return [Array<SoberSwag::OutputObject::View>]
63
+ attr_reader :views
64
+ ##
65
+ # @return [String] the external ID to use for this object
66
+ attr_reader :identifier
52
67
 
68
+ ##
69
+ # Perform serialization.
53
70
  def serialize(obj, opts = {})
54
71
  serializer.serialize(obj, opts)
55
72
  end
56
73
 
74
+ ##
75
+ # Get a Dry::Struct of the type this OutputObject will serialize to.
57
76
  def type
58
77
  serializer.type
59
78
  end
60
79
 
80
+ ##
81
+ # Get a serializer for a single view contained in this output object.
82
+ # Note: given `:base`, it will return a serializer for the base OutputObject
83
+ # @param name [Symbol] the name of the view
84
+ # @return [SoberSwag::Serializer::Base] the serializer
61
85
  def view(name)
62
86
  return base_serializer if name == :base
63
87
 
64
88
  @views.find { |v| v.name == name }
65
89
  end
66
90
 
91
+ ##
92
+ # A serializer for the "base type" of this OutputObject, with no views.
67
93
  def base
68
94
  base_serializer
69
95
  end
@@ -72,30 +98,25 @@ module SoberSwag
72
98
  # Compile down this to an appropriate serializer.
73
99
  # It uses {SoberSwag::Serializer::Conditional} to do view-parsing,
74
100
  # and {SoberSwag::Serializer::FieldList} to do the actual serialization.
75
- def serializer # rubocop:disable Metrics/MethodLength
101
+ #
102
+ # @todo: optimize view selection to use binary instead of linear search
103
+ def serializer
76
104
  @serializer ||=
77
105
  begin
78
- views.reduce(base_serializer) do |base, view|
79
- view_serializer = view.serializer
80
- SoberSwag::Serializer::Conditional.new(
81
- proc do |object, options|
82
- if options[:view].to_s == view.name.to_s
83
- [:left, object]
84
- else
85
- [:right, object]
86
- end
87
- end,
88
- view_serializer,
89
- base
90
- )
91
- end
106
+ view_choices = views.map { |view| [view.name.to_s, view.serializer] }.to_h
107
+ view_choices['base'] = base_serializer
108
+ SoberSwag::Serializer::Hash.new(view_choices, base, proc { |_, options| options[:view]&.to_s })
92
109
  end
93
110
  end
94
111
 
112
+ ##
113
+ # @return [String]
95
114
  def to_s
96
115
  "<SoberSwag::OutputObject(#{identifier})>"
97
116
  end
98
117
 
118
+ ##
119
+ # @return [SoberSwag::Serializer::FieldList] serializer for this output object.
99
120
  def base_serializer
100
121
  @base_serializer ||= SoberSwag::Serializer::FieldList.new(fields).tap do |s|
101
122
  s.identifier(identifier)
@@ -1,13 +1,19 @@
1
1
  module SoberSwag
2
2
  ##
3
3
  # Parses a *Dry-Types Schema* into a set of nodes we can use to compile.
4
- # This is mostly because the vistior pattern sucks and catamorphisms are nice.
4
+ # This is mostly because the visitor pattern sucks and catamorphisms are nice.
5
+ #
6
+ # Do not use this class directly, as it is not part of the public api.
7
+ # Instead, use classes from the {SoberSwag::Compiler} namespace.
5
8
  class Parser
6
9
  def initialize(node)
7
10
  @node = node
8
11
  @found = Set.new
9
12
  end
10
13
 
14
+ ##
15
+ # Compile to one of our internal nodes.
16
+ # @return [SoberSwag::Nodes::Base] the node that describes this type.
11
17
  def to_syntax # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
12
18
  case @node
13
19
  when Dry::Types::Default
@@ -0,0 +1,39 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Compiler
4
+ ##
5
+ # Compile component schemas.
6
+ class Schema
7
+ def initialize
8
+ @references = {}
9
+ @referenced_schemas = Set.new
10
+ end
11
+
12
+ ##
13
+ # Hash of references to type definitions.
14
+ # Suitable for us as the components hash.
15
+ attr_reader :references
16
+
17
+ def compile(value)
18
+ compile_inner(value.swagger_schema)
19
+ end
20
+
21
+ def compile_inner(value)
22
+ initial, found = value
23
+
24
+ merge_found(found)
25
+
26
+ initial
27
+ end
28
+
29
+ def merge_found(found)
30
+ found.each do |k, v|
31
+ next unless @referenced_schemas.add?(k)
32
+
33
+ @references[k] = compile_inner(v.call)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Base class for all inputs in the cool new schema style.
6
+ class Base
7
+ include Interface
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Convert booleans.
6
+ class Bool < Base
7
+ def call(val)
8
+ return Report::Value.new(['was not a bool']) unless [true, false].include?(val)
9
+
10
+ val
11
+ end
12
+
13
+ def swagger_schema
14
+ [{ type: 'boolean' }, {}]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ module Converting
5
+ ##
6
+ # Try to convert a boolean-like value to an actual boolean.
7
+ Bool = # rubocop:disable Naming/ConstantName
8
+ (SoberSwag::Reporting::Input::Bool.new |
9
+ (
10
+ SoberSwag::Reporting::Input::Text
11
+ .new
12
+ .enum(*(%w[y yes true t].flat_map { |x| [x, x.upcase] } + ['1'])) |
13
+ SoberSwag::Reporting::Input::Number.new.enum(1)).mapped { |_| true } |
14
+ (
15
+ SoberSwag::Reporting::Input::Text
16
+ .new
17
+ .enum(*(%w[false no n f].flat_map { |x| [x, x.upcase] } + ['0'])) |
18
+ SoberSwag::Reporting::Input::Number.new.enum(0)
19
+ ).mapped { |_| false }
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ module Converting
5
+ ##
6
+ # Convert via a date.
7
+ #
8
+ # Note: unlike the swagger spec, we first try to convert
9
+ # rfc8601, then try rfc3339.
10
+ Date = (
11
+ SoberSwag::Reporting::Input::Text
12
+ .new
13
+ .mapped { |str|
14
+ begin
15
+ ::Date.rfc3339(str)
16
+ rescue ArgumentError
17
+ Report::Value.new(['was not an RFC 3339 date string'])
18
+ end
19
+ } |
20
+ SoberSwag::Reporting::Input::Text
21
+ .new
22
+ .mapped do |str|
23
+ ::Date.iso8601(str)
24
+ rescue ArgumentError
25
+ Report::Value.new(['was not an ISO8601 date string'])
26
+ end).format('date')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ module Converting
5
+ ##
6
+ # Convert via a date.
7
+ DateTime =
8
+ SoberSwag::Reporting::Input::Text
9
+ .new
10
+ .mapped { |str|
11
+ begin
12
+ ::DateTime.rfc3339(str)
13
+ rescue ArgumentError
14
+ Report::Value.new(['was not an RFC 3339 date-time string'])
15
+ end
16
+ }.or(
17
+ SoberSwag::Reporting::Input::Text
18
+ .new
19
+ .mapped do |str|
20
+ ::DateTime.iso8601(str)
21
+ rescue ArgumentError
22
+ Report::Value.new(['was not an ISO8601 date-time string'])
23
+ end
24
+ ).format('date-time')
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ module Converting
5
+ ##
6
+ # Parse a decimal.
7
+ Decimal =
8
+ (SoberSwag::Reporting::Input::Number.new.mapped(&:to_d).format(:decimal) |
9
+ SoberSwag::Reporting::Input::Text
10
+ .new
11
+ .format('decimal')
12
+ .mapped do |v|
13
+ BigDecimal(v)
14
+ rescue ArgumentError
15
+ Report::Value.new('was not a decimal')
16
+ end).described(<<~MARKDOWN).referenced('SoberSwag.Converting.Decimal')
17
+ Decimal formatted input.
18
+ Will either convert a JSON number to a decimal, or accept a string representation.
19
+ The string representation allows for greater precision.
20
+ MARKDOWN
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ module Converting
5
+ Integer =
6
+ (SoberSwag::Reporting::Input.number.format('integer').mapped(&:to_i)) |
7
+ (SoberSwag::Reporting::Input.text.format('integer').mapped do |v|
8
+ Integer(v)
9
+ rescue ArgumentError
10
+ Report::Value.new(['was not an integer string'])
11
+ end).described(<<~MARKDOWN).referenced('SoberSwag.Converting.Integer')
12
+ Integer formatted input.
13
+
14
+ With either convert a JSON number to an integer, or accept a string representation of an integer.
15
+ MARKDOWN
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Namespace for things that can do conversion.
6
+ # These are really just compound types that kinda look nice.
7
+ module Converting
8
+ autoload(:Decimal, 'sober_swag/reporting/input/converting/decimal')
9
+ autoload(:Date, 'sober_swag/reporting/input/converting/date')
10
+ autoload(:DateTime, 'sober_swag/reporting/input/converting/date_time')
11
+ autoload(:Bool, 'sober_swag/reporting/input/converting/bool')
12
+ autoload(:Integer, 'sober_swag/reporting/input/converting/integer')
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Resolve circular references by deferring the loading of an input.
6
+ class Defer < Base
7
+ def initialize(other_lazy)
8
+ @other_lazy = other_lazy
9
+ end
10
+
11
+ attr_reader :other_lazy
12
+
13
+ def other
14
+ return @other if defined?(@other)
15
+
16
+ @other = other_lazy.call
17
+ end
18
+
19
+ def call(input)
20
+ other.call(input)
21
+ end
22
+
23
+ def swagger_schema
24
+ other.swagger_schema
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Node for things with descriptions.
6
+ # This describes the *type*, not the *object key*.
7
+ class Described < Base
8
+ def initialize(input, description)
9
+ @input = input
10
+ @description = description
11
+ end
12
+
13
+ ##
14
+ # @return [Interface] base input
15
+ attr_reader :input
16
+
17
+ ##
18
+ # @return [String] description of input
19
+ attr_reader :description
20
+
21
+ def call(value)
22
+ input.call(value)
23
+ end
24
+
25
+ def swagger_schema
26
+ val, other = input.swagger_schema
27
+ merged =
28
+ if val.key?(:$ref)
29
+ { allOf: [val] }
30
+ else
31
+ val
32
+ end.merge(description: description)
33
+ [merged, other]
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Dictionary types: string keys, something else as a value.
6
+ class Dictionary < Base
7
+ def self.of(input_type)
8
+ new(input_type)
9
+ end
10
+
11
+ def initialize(value_input)
12
+ @value_input = value_input
13
+ end
14
+
15
+ attr_reader :value_input
16
+
17
+ def call(value)
18
+ return Report::Base.new(['was not an object']) unless value.is_a?(Hash)
19
+
20
+ bad, good = value.map { |k, v|
21
+ [k, value_input.call(v)]
22
+ }.compact.partition { |(_, v)| v.is_a?(Report::Base) }
23
+
24
+ return Report::Object.new(bad.to_h) if bad.any?
25
+
26
+ good.to_h
27
+ end
28
+
29
+ def swagger_schema
30
+ schema, found = value_input.swagger_schema
31
+
32
+ [{ type: :object, additionalProperties: schema }, found]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,51 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Parses either one input, or another.
6
+ # Left-biased.
7
+ class Either < Base
8
+ ##
9
+ # @param lhs [Base] an input we will try first
10
+ # @param rhs [Base] an input we will try second
11
+ def initialize(lhs, rhs)
12
+ @lhs = lhs
13
+ @rhs = rhs
14
+ end
15
+
16
+ ##
17
+ # @return [Base] parser for LHS
18
+ attr_reader :lhs
19
+ ##
20
+ # @return [Base] parser for RHS
21
+ attr_reader :rhs
22
+
23
+ def call(value)
24
+ maybe_lhs = lhs.call(value)
25
+
26
+ return maybe_lhs unless maybe_lhs.is_a?(Report::Base)
27
+
28
+ maybe_rhs = rhs.call(value)
29
+
30
+ return maybe_rhs unless maybe_rhs.is_a?(Report::Base)
31
+
32
+ Report::Either.new(maybe_lhs, maybe_rhs)
33
+ end
34
+
35
+ def swagger_schema
36
+ lhs_val, lhs_set = lhs.swagger_schema
37
+ rhs_val, rhs_set = rhs.swagger_schema
38
+
39
+ val = { oneOf: defs_for(lhs_val) + defs_for(rhs_val) }
40
+ [val, lhs_set.merge(rhs_set)]
41
+ end
42
+
43
+ private
44
+
45
+ def defs_for(schema)
46
+ schema[:oneOf] || [schema]
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ module SoberSwag
2
+ module Reporting
3
+ module Input
4
+ ##
5
+ # Specify that a value must be included in a list of possible values.
6
+ class Enum < Base
7
+ def initialize(input, values)
8
+ @input = input
9
+ @values = values
10
+ end
11
+
12
+ ##
13
+ # @return [Interface] base type
14
+ attr_reader :input
15
+
16
+ ##
17
+ # @return [Array<String>] acceptable types
18
+ attr_reader :values
19
+
20
+ def call(value)
21
+ inner = input.call(value)
22
+
23
+ return inner if inner.is_a?(Report::Base)
24
+
25
+ return Report::Value.new(['was not an acceptable enum member']) unless values.include?(inner)
26
+
27
+ inner
28
+ end
29
+
30
+ def swagger_schema
31
+ schema, found = input.swagger_schema
32
+
33
+ merged =
34
+ if schema.key?(:$ref)
35
+ { allOf: [schema] }
36
+ else
37
+ schema
38
+ end.merge(enum: values)
39
+ [merged, found]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end