From 10645196d1f0f11d657e39c6ac2c825f60a15f7a Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 28 Sep 2022 11:01:29 +0200 Subject: [PATCH] binary strings encoded binary. Idx::Id instead of Symbol in Idx. parse_items only allowes Symbols as options. --- lib/knot/interface.rb | 34 ++++++++-------- lib/knot/protocol.rb | 93 +++++++++++++++++++++++++++++-------------- 2 files changed, 82 insertions(+), 45 deletions(-) diff --git a/lib/knot/interface.rb b/lib/knot/interface.rb index 221af51..2825e97 100644 --- a/lib/knot/interface.rb +++ b/lib/knot/interface.rb @@ -127,37 +127,39 @@ class Knot::Conf self.commit end - def parse_item k - case k + def parse_item kv + case kv when Hash - case k.keys.sort - when %w[section], %w[id section], %w[item section], %w[id item section] - k + r = {} + kv.each {|k,v| r[k.to_s.to_sym] = v } + case r.keys.sort + when %i[section], %i[id section], %i[item section], %i[id item section] + r else - raise ArgumentError, "Invalid Item-format" + raise ArgumentError, "Invalid Item-format: #{k}" end when Array - case k.length - when 1 then {section: k[0]} - when 2 then {section: k[0], item: k[1]} - when 3 then {section: k[0], id: k[1], item: k[2]} - else raise ArgumentError, "Invalid Item-format" + case kv.length + when 1 then {section: kv[0]} + when 2 then {section: kv[0], item: kv[1]} + when 3 then {section: kv[0], id: kv[1], item: kv[2]} + else raise ArgumentError, "Invalid Item-format: #{kv}" end when /\A - (?
[a-z0-9_-]+ ) - (?: \[ (? [a-z0-9_.-]+) \] )? - (?: \. (?[a-z0-9_-]+) )? + (?
[a-z0-9_-]+ ) + (?: \[ (? [a-z0-9_.-]+) \] )? + (?: \. (?[a-z0-9_-]+) )? \z/xi - $~.named_captures.delete_if {|_,v| v.nil? } + $~.named_captures.delete_if {|_,v| v.nil? } when nil {} else - raise ArgumentError, "Invalid Item-format" + raise ArgumentError, "Invalid Item-format: #{kv}" end end diff --git a/lib/knot/protocol.rb b/lib/knot/protocol.rb index 247e7d8..21c8463 100644 --- a/lib/knot/protocol.rb +++ b/lib/knot/protocol.rb @@ -73,34 +73,69 @@ end #end module Knot::Protocol::Idx + class Id + include Comparable + attr_reader :name, :code, :cname, :description + + def initialize name, code, cname, description + raise ArgumentError, "Expecting Symbol for Idx::Id instead of: #{name.inspect}" if Symbol === name + raise ArgumentError, "Expecting Integer for Idx::Id instead of: #{code.inspect}" if Integer === code + @name, @code, @cname, @description = name, code, cname, description + freeze + end + + def === x + case x + when self.class then self == x + when Symbol then @name == x + when String then @name == x.to_sym + when Integer then @code == x + else nil + end + end + + def <=>( x) @id <=> x.id end + def to_s() @name.to_s end + def to_sym() @name end + def to_i() @code end + end + Idx = [ - :command, # 10, :CMD, # Control command name. - :flags, # 11, :FLAGS, # Control command flags. - :error, # 12, :ERROR, # Error message. - :section, # 13, :SECTION, # Configuration section name. - :item, # 14, :ITEM, # Configuration item name. - :id, # 15, :ID, # Congiguration item identifier. - :zone, # 16, :ZONE, # Zone name. - :owner, # 17, :OWNER, # Zone record owner - :ttl, # 18, :TTL, # Zone record TTL. - :type, # 19, :TYPE, # Zone record type name. - :data, # 1a, :DATA, # Configuration item/zone record data. - :filter, # 1b, :FILTER, # An option or a filter for output data processing. + Id.new( :command, 0x10, :CMD, 'Control command name.'), + Id.new( :flags, 0x11, :FLAGS, 'Control command flags.'), + Id.new( :error, 0x12, :ERROR, 'Error message.'), + Id.new( :section, 0x13, :SECTION, 'Configuration section name.'), + Id.new( :item, 0x14, :ITEM, 'Configuration item name.'), + Id.new( :id, 0x15, :ID, 'Congiguration item identifier.'), + Id.new( :zone, 0x16, :ZONE, 'Zone name.'), + Id.new( :owner, 0x17, :OWNER, 'Zone record owner'), + Id.new( :ttl, 0x18, :TTL, 'Zone record TTL.'), + Id.new( :type, 0x19, :TYPE, 'Zone record type name.'), + Id.new( :data, 0x1a, :DATA, 'Configuration item/zone record data.'), + Id.new( :filter, 0x1b, :FILTER, 'An option or a filter for output data processing.'), ] Name = {} Code = {} - Idx.each_with_index do |v, i| - Code[0x10+i] = v - Name[v] = i + + Idx.each do |id| + Code[id.to_i] = id + Name[id.to_sym] = id end - def self.[] k - case k - when Symbol - Name[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}" - when Integer - Idx[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}" - else - raise ArgumentError, "Unknown Idx-Type: #{k}" + + class <