Class: Datadog::DI::EL::Expression Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/el/expression.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents an Expression Language expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dsl_expr, compiled_expr, regexps: [], redaction_identifier: nil) ⇒ Expression

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Expression.

Parameters:

  • dsl_expr (String) —

    human-readable DSL form, kept for debugging.

  • compiled_expr (String) —

    Ruby source produced by Compiler#compile.

  • regexps (Array<Regexp>) (defaults to: []) —

    precompiled matches regexps (see Compiler#precompile_regexp), the second element returned by Compiler#compile.

  • redaction_identifier (String, nil) (defaults to: nil) —

    the identifier this expression directly references at its top level (see Compiler#redaction_identifier); nil when it is not a direct reference.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datadog/di/el/expression.rb', line 19

def initialize(dsl_expr, compiled_expr, regexps: [], redaction_identifier: nil)
  unless String === compiled_expr
    raise ArgumentError, "compiled_expr must be a string"
  end

  @dsl_expr = dsl_expr
  @redaction_identifier = redaction_identifier

  cls = Class.new(Evaluator)
  cls.class_exec do
    eval(<<-RUBY, Object.new.send(:binding), __FILE__, __LINE__ + 1) # standard:disable Security/Eval
      # Evaluates the compiled expression against +context+ within
      # the cooperative evaluation +evaluation_deadline+.
      #
      # @param context [Context] evaluation context (locals, instance
      #   variables, and per-invocation special variables).
      # @param evaluation_deadline [Float] invocation-local evaluation
      #   deadline, as CLOCK_THREAD_CPUTIME_ID float seconds.
      # @return [Object] the value the expression evaluates to.
      # @raise [DI::Error::ExpressionEvaluationError] the expression
      #   failed to evaluate, including the EvaluationTimeout
      #   subclass raised when the deadline is exceeded.
      def evaluate(context, evaluation_deadline)
        #{compiled_expr}
      end
    RUBY
  end
  @evaluator = cls.new(regexps)
end

Instance Attribute Details

#dsl_expr ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



49
50
51
# File 'lib/datadog/di/el/expression.rb', line 49

def dsl_expr
  @dsl_expr
end

#evaluator ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/datadog/di/el/expression.rb', line 50

def evaluator
  @evaluator
end

#redaction_identifier ⇒ Object (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
# File 'lib/datadog/di/el/expression.rb', line 51

def redaction_identifier
  @redaction_identifier
end

Instance Method Details

#evaluate(context, evaluation_deadline:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates the expression against context.

Parameters:

  • context (Context) —

    evaluation context (locals, instance variables, and per-invocation special variables).

  • evaluation_deadline (Float) —

    cooperative evaluation deadline, as CLOCK_THREAD_CPUTIME_ID float seconds.

Returns:

  • (Object) —

    the value the expression evaluates to.

Raises:



63
64
65
# File 'lib/datadog/di/el/expression.rb', line 63

def evaluate(context, evaluation_deadline:)
  evaluator.evaluate(context, evaluation_deadline)
end

#satisfied?(context, evaluation_deadline:) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether the expression evaluates to a truthy value against context.

Parameters:

  • context (Context) —

    evaluation context.

  • evaluation_deadline (Float) —

    cooperative evaluation deadline, as CLOCK_THREAD_CPUTIME_ID float seconds.

Returns:

  • (Boolean) —

    whether the expression is satisfied.

Raises:



77
78
79
# File 'lib/datadog/di/el/expression.rb', line 77

def satisfied?(context, evaluation_deadline:)
  !!evaluate(context, evaluation_deadline: evaluation_deadline)
end