Class: StrongJSON::ErrorReporter
- Inherits:
-
Object
- Object
- StrongJSON::ErrorReporter
- Defined in:
- lib/strong_json/error_reporter.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #format ⇒ Object
- #format_aliases(path:, where:) ⇒ Object
- #format_trace(path:, index: 1) ⇒ Object
-
#initialize(path:) ⇒ ErrorReporter
constructor
A new instance of ErrorReporter.
- #pretty(type, pp, expand_alias: false) ⇒ Object
- #pretty_str(type, expand_alias: false) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path:) ⇒ ErrorReporter
Returns a new instance of ErrorReporter.
6 7 8 |
# File 'lib/strong_json/error_reporter.rb', line 6 def initialize(path:) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/strong_json/error_reporter.rb', line 4 def path @path end |
Instance Method Details
#format ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/strong_json/error_reporter.rb', line 15 def format @string = "" format_trace(path: path) where = format_aliases(path: path, where: []) unless where.empty? @string << "\nWhere:\n" @string << where.map {|x| x.gsub(/^/, " ") }.join("\n") end end |
#format_aliases(path:, where:) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/strong_json/error_reporter.rb', line 45 def format_aliases(path:, where:) ty = path.type if ty.alias # @type const PrettyPrint: any where << PrettyPrint.format do |pp| pp.text(ty.alias.to_s) pp.text(" = ") pp.group do pretty(ty, pp, expand_alias: true) end end end if parent = path.parent format_aliases(path: parent[1], where: where) end where end |
#format_trace(path:, index: 1) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/strong_json/error_reporter.rb', line 26 def format_trace(path:, index: 1) @string << (" " * index) type_string = pretty_str(path.type) if parent = path.parent case parent[0] when Symbol @string << "\"#{parent[0]}\" expected to be #{type_string}\n" when Integer @string << "#{parent[0]} expected to be #{type_string}\n" else @string << "Expected to be #{type_string}\n" end format_trace(path: parent[1], index: index + 1) else @string << "$ expected to be #{type_string}\n" end end |
#pretty(type, pp, expand_alias: false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/strong_json/error_reporter.rb', line 73 def pretty(type, pp, expand_alias: false) if ! && type.alias pp.text type.alias.to_s else case type when Type::Object pp.group 0, "{", "}" do pp.nest(2) do pp.breakable(" ") type.fields.each.with_index do |pair, index| key, ty = pair pp.text "#{key.to_s.inspect}: " pretty(ty, pp) if index < type.fields.size-1 pp.text "," pp.breakable(" ") end end end pp.breakable(" ") end when Type::Enum pp.group 0, "enum(", ")" do pp.nest(2) do pp.breakable("") type.types.each.with_index do |ty, index| pretty(ty, pp) if index < type.types.size - 1 pp.text "," pp.breakable " " end end end pp.breakable("") end when Type::Optional pp.group 0, "optional(", ")" do pp.nest(2) do pp.breakable "" pretty(type.type, pp) end pp.breakable "" end when Type::Array pp.group 0, "array(", ")" do pp.nest(2) do pp.breakable "" pretty(type.type, pp) end pp.breakable "" end else pp.text type.to_s end end end |
#pretty_str(type, expand_alias: false) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/strong_json/error_reporter.rb', line 66 def pretty_str(type, expand_alias: false) # @type const PrettyPrint: any PrettyPrint.singleline_format do |pp| pretty(type, pp, expand_alias: ) end end |
#to_s ⇒ Object
10 11 12 13 |
# File 'lib/strong_json/error_reporter.rb', line 10 def to_s format() unless @string @string end |