binary strings encoded binary. Idx::Id instead of Symbol in Idx. parse_items only allowes Symbols as options.

This commit is contained in:
Denis Knauf 2022-09-28 11:01:29 +02:00
parent 6af5922217
commit 10645196d1
2 changed files with 82 additions and 45 deletions

View file

@ -127,22 +127,24 @@ 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
@ -157,7 +159,7 @@ class Knot::Conf
{}
else
raise ArgumentError, "Invalid Item-format"
raise ArgumentError, "Invalid Item-format: #{kv}"
end
end

View file

@ -73,36 +73,71 @@ 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
class <<self
def [] 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}"
Code[k] or raise Knot::Errors::EINVAL, "Unknown Idx: #{k}"
else
raise ArgumentError, "Unknown Idx-Type: #{k}"
end
end
def each &exe
block_given? ? Idx.each( &exe) : Idx.to_enum( :each)
end
end
end
class Knot::Protocol
@ -125,7 +160,7 @@ class Knot::Protocol
def snd sock: nil, **data
rsock = sock || @sock
s = ''
s = ''.b
sock = StringIO.new s
sock.write [1].pack( 'c')
data[:flags] ||= ''
@ -135,7 +170,7 @@ class Knot::Protocol
end
sock.write [3].pack( 'c')
sock.flush
STDERR.puts( {data: data, _: s}.inspect) if @debug
STDERR.puts( {send: data, _: s}.inspect) if @debug
rsock.write s
rsock.flush
end
@ -144,7 +179,7 @@ class Knot::Protocol
attr_reader :str
def initialize sock, str = nil
@str, @sock = str || '', sock
@str, @sock = str || ''.b, sock
end
def unpack pattern
@ -182,7 +217,7 @@ class Knot::Protocol
end
end
ensure
STDERR.puts( {rcvd: ret, read: sock.str}.inspect) if @debug
STDERR.puts( {rcvd: ret, _: sock.str}.inspect) if @debug
ret
end