Giter Club home page Giter Club logo

Comments (2)

vinayak3qilabs avatar vinayak3qilabs commented on July 30, 2024

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
`

from bindata.

vinayak3qilabs avatar vinayak3qilabs commented on July 30, 2024

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

from bindata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.