Class: StrongJSON::ErrorReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/strong_json/error_reporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

#formatObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/strong_json/error_reporter.rb', line 15

def format
  @string = ""

  format_trace(path: path)
  where = format_aliases(path: path, where: [])

  # @type var ty: Type::Enum<any>
  if (ty = _ = path.type).is_a?(Type::Enum)
    ty.types.each do |t|
      if (a = t.alias)
        where.push format_single_alias(a, t)
      end
    end
  end

  unless where.empty?
    @string << "\nWhere:\n"
    @string << where.map {|x| x.gsub(/^/, "  ") }.join("\n")
  end
end

#format_aliases(path:, where:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strong_json/error_reporter.rb', line 55

def format_aliases(path:, where:)
  ty = path.type

  if (a = ty.alias)
    where << format_single_alias(a, ty)
  end

  if parent = path.parent
    format_aliases(path: parent[1], where: where)
  end

  where
end

#format_single_alias(name, type) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/strong_json/error_reporter.rb', line 69

def format_single_alias(name, type)
  # @type const PrettyPrint: any
  PrettyPrint.format do |pp|
    pp.text(name.to_s)
    pp.text(" = ")
    pp.group do
      pretty(type, pp, expand_alias: true)
    end
  end
end

#format_trace(path:, index: 1) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/strong_json/error_reporter.rb', line 36

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



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/strong_json/error_reporter.rb', line 87

def pretty(type, pp, expand_alias: false)
  if !expand_alias && 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
    when Type::Base
      pp.text type.type.to_s
    else
      pp.text type.to_s
    end
  end
end

#pretty_str(type, expand_alias: false) ⇒ Object



80
81
82
83
84
85
# File 'lib/strong_json/error_reporter.rb', line 80

def pretty_str(type, expand_alias: false)
  # @type const PrettyPrint: any
  PrettyPrint.singleline_format do |pp|
    pretty(type, pp, expand_alias: expand_alias)
  end
end

#to_sObject



10
11
12
13
# File 'lib/strong_json/error_reporter.rb', line 10

def to_s
  format() unless @string
  @string
end