Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename parameter 'assert' as it shadows an existing method. (Linked to Closed issue-#141) #142

Closed
vinayak3qilabs opened this issue Oct 17, 2022 · 2 comments

Comments

@vinayak3qilabs
Copy link

@dmendel this issue still not resolved.

NameError: Rename parameter 'assert' as it shadows an existing method.
ensure_valid_names at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/params.rb:111
each at org/jruby/RubyArray.java:1800
ensure_valid_names at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/params.rb:109
to_syms at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/params.rb:103
optional at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/params.rb:66
optional_parameters at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/params.rb:12
class:BasePrimitive at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/base_primitive.rb:52
module:BinData at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/base_primitive.rb:49
at C:/awe-jruby2/jruby-9.2.8.0/lib/ruby/gems/shared/gems/bindata-2.4.12/lib/bindata/base_primitive.rb:3

@vinayak3qilabs
Copy link
Author

I was trying to push a fix where access denied error occurred.please check the following changes in base_primitive.rb file.

i have replaced 'assert' keyword with 'assert_key' only.

class BasePrimitive < BinData::Base
unregister_self

optional_parameters :initial_value, :value, :assert_key, :asserted_value
mutually_exclusive_parameters :initial_value, :value
mutually_exclusive_parameters :asserted_value, :value, :assert_key

def initialize_shared_instance
  extend InitialValuePlugin  if has_parameter?(:initial_value)
  extend ValuePlugin         if has_parameter?(:value)
  extend AssertPlugin        if has_parameter?(:assert_key)
  extend AssertedValuePlugin if has_parameter?(:asserted_value)
  super
end

def initialize_instance
  @value = nil
end

def clear? #:nodoc:
  @value.nil?
end

def assign(val)
  raise ArgumentError, "can't set a nil value for #{debug_name}" if val.nil?

  raw_val = val.respond_to?(:snapshot) ? val.snapshot : val
  @value =
    begin
      raw_val.dup
    rescue TypeError
      # can't dup Fixnums
      raw_val
    end
end

def snapshot
  _value
end

def value
  snapshot
end

def value=(val)
  assign(val)
end

def respond_to?(symbol, include_private = false) #:nodoc:
  child = snapshot
  child.respond_to?(symbol, include_private) || super
end

def method_missing(symbol, *args, &block) #:nodoc:
  child = snapshot
  if child.respond_to?(symbol)
    self.class.class_eval \
      "def #{symbol}(*args, &block);" \
      "  snapshot.#{symbol}(*args, &block);" \
      "end"
    child.__send__(symbol, *args, &block)
  else
    super
  end
end

def <=>(other)
  snapshot <=> other
end

def eql?(other)
  # double dispatch
  other.eql?(snapshot)
end

def hash
  snapshot.hash
end

def do_read(io) #:nodoc:
  @value = read_and_return_value(io)
end

def do_write(io) #:nodoc:
  io.writebytes(value_to_binary_string(_value))
end

def do_num_bytes #:nodoc:
  value_to_binary_string(_value).length
end

#---------------
private

# The unmodified value of this data object.  Note that #snapshot calls this
# method.  This indirection is so that #snapshot can be overridden in
# subclasses to modify the presentation value.
def _value
  @value != nil ? @value : sensible_default
end

# Logic for the :value parameter
module ValuePlugin
  def assign(val)
    # Ignored
  end

  def _value
    reading? ? @value : eval_parameter(:value)
  end
end

# Logic for the :initial_value parameter
module InitialValuePlugin
  def _value
    @value != nil ? @value : eval_parameter(:initial_value)
  end
end

# Logic for the :assert parameter
module AssertPlugin
  def assign(val)
    super(val)
    assert_key!
  end

  def do_read(io) #:nodoc:
    super(io)
    assert_key!
  end

  def assert_key!
    current_value = snapshot
    expected = eval_parameter(:assert_key, value: current_value)

    msg =
      if !expected
        "value '#{current_value}' not as expected"
      elsif expected != true && current_value != expected
        "value is '#{current_value}' but expected '#{expected}'"
      else
        nil
      end

    raise ValidityError, "#{msg} for #{debug_name}" if msg
  end
end

# Logic for the :asserted_value parameter
module AssertedValuePlugin
  def assign(val)
    assert_value(val)
    super(val)
  end

  def _value
    reading? ? @value : eval_parameter(:asserted_value)
  end

  def do_read(io) #:nodoc:
    super(io)
    assert_key!
  end

  def assert_key!
    assert_value(snapshot)
  end

  def assert_value(current_value)
    expected = eval_parameter(:asserted_value, value: current_value)
    if current_value != expected
      raise ValidityError,
            "value is '#{current_value}' but " \
            "expected '#{expected}' for #{debug_name}"
    end
  end
end

###########################################################################
# To be implemented by subclasses

# Return the string representation that +val+ will take when written.
def value_to_binary_string(val)
  raise NotImplementedError
end

# Read a number of bytes from +io+ and return the value they represent.
def read_and_return_value(io)
  raise NotImplementedError
end

# Return a sensible default for this data.
def sensible_default
  raise NotImplementedError
end

# To be implemented by subclasses
###########################################################################

end
end
`

@vinayak3qilabs
Copy link
Author

Hey @dmendel thanks for the pointer in issue #141 , This resolved our issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant