lib/snmp/open/parser.rb in snmp-open-0.7.0 vs lib/snmp/open/parser.rb in snmp-open-0.7.1

- old
+ new

@@ -61,39 +61,38 @@ end private def align(columns) - indexes = columns.first.map { |value| index_using_first_oid(value) } + indexes = indexes_from_columns(columns) + bases = bases_from_columns(columns) hash = columns.flat_map { |row| row.map { |v| [v.oid, v] } }.to_h indexes.map do |index| - @oids.map do |base| + bases.map do |base, _| oid = [base, *index].join('.') hash.fetch(oid) { Value.new(oid, 'absent', nil) } end end end + def bases_from_columns(columns) + @oids + .zip(columns.map { |c| c&.first&.oid }) + .map { |base, oid| base && oid && split_oid(base, oid) } + end + def clean_input_text(text) text .gsub(/\r\n|\n\r|\r/, "\n") .gsub(EMPTY_STRING_RE, %(\\1 = \\2: ""\n)) .gsub(STRING_RE, %(\\1 = \\2: "\\3"\n)) .gsub(Static::ANY_MESSAGE, Static::QUOTED_MESSAGES) end - def index_using_first_oid(value) - base = @oids.first - - if base == value.oid - nil - elsif value.oid.start_with?(base) - value.oid.gsub(/\A#{base}\.?/, '') - else - raise "Received ID doesn't start with the given ID" - end + def indexes_from_columns(columns) + columns.first.map { |value| split_oid(@oids.first, value.oid)[1] } end def parse_tokens(tokens) tokens = tokens.each objects = [] @@ -120,9 +119,24 @@ def parse_type(tokens) token = tokens.next type = token.match(/\A([-A-Za-z]+[0-9]*):\z/) { |md| md[1] } ValueParser.find(type, token).parse(tokens) + end + + # split a complete OID into a base and index, given an expected base + # raises if the base isn't present + def split_oid(base, oid) + if base == oid + [base, nil] + elsif oid.start_with?(base) + [base, oid.sub(/\A#{base}\.?/, '')] + elsif base.include?('::') && !oid.include?('::') + alternate_base = base.sub(/\A[^:]+::/, '') + split_oid(alternate_base, oid) + else + raise "Received ID doesn't start with the given ID" + end end def table(columns) if columns.size == 1 && columns.all? { |column| column.size == 1 } columns