Class: Cel::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/cel/context.rb

Constant Summary collapse

NORMALIZE_KEYS =
lambda do |k, v|
  if v.is_a?(Hash) || v.is_a?(Map)
    v.flat_map do |k2, v2|
      NORMALIZE_KEYS.call(:"#{k}.#{String(k2)}", v2)
    end
  else
    [k, v]
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, bindings) ⇒ Context

Returns a new instance of Context.



7
8
9
10
# File 'lib/cel/context.rb', line 7

def initialize(environment, bindings)
  @environment = environment
  @bindings = bindings.dup
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



5
6
7
# File 'lib/cel/context.rb', line 5

def bindings
  @bindings
end

Instance Method Details

#declarationsObject



12
13
14
# File 'lib/cel/context.rb', line 12

def declarations
  @environment.declarations
end

#lookup(identifier) ⇒ Object

Raises:



16
17
18
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cel/context.rb', line 16

def lookup(identifier)
  raise EvaluateError, "no value in context for #{identifier}" unless @bindings

  top_level_key = nil
  id = identifier.to_s

  lookup_keys = id.split(".")

  val = @bindings

  if lookup_keys.first.empty?
    # key starts with ".", i.e. ".y"
    val = val.dup
    lookup_keys.shift
    top_level_key = key = lookup_keys.first

    if val.respond_to?(:key?)
      val = val.dup
      val.keys.select { |k| k.start_with?(".#{key}") }.each do |k|
        val[k.to_s[1..-1].to_sym] = val.delete(k) # rubocop:disable Style/SlicingWithRange
      end
      # else do nothing
    end
  end

  if (container = @environment.container)
    # ex move data from "example.com.y" to "y"
    container_keys = val.keys.select { |k| k.start_with?(container) }

    unless container_keys.empty?
      val = val.dup
      container_keys.each do |key|
        skey = key.to_s.delete_prefix("#{container}.")

        # forego editing if this is a top level lookup and the key being
        # overwritten would be one
        next if top_level_key && skey.start_with?(top_level_key)

        val[skey.to_sym] = val.delete(key)
      end
    end
  end

  loop do
    fetched = false
    (0...lookup_keys.size).reverse_each do |idx|
      key = lookup_keys[0..idx].join(".").to_sym
      is_binding = val == @bindings

      val = if val.respond_to?(:key?)
        # hash
        next unless val.key?(key)

        val[key]
      else
        next unless val.respond_to?(key)

        case val
        when Protobuf.base_class
          Protobuf.lookup(val, key)
        else
          val.__send__(key)
        end
      end

      val = @bindings[key] = to_cel_primitive(key, val) if is_binding
      lookup_keys = lookup_keys[(idx + 1)..]
      fetched = true
      break
    end

    break unless fetched

    break if lookup_keys.empty?
  end

  raise EvaluateError, "no value in context for #{id}" if val == @bindings

  # lookup_keys.each do |key|
  #   raise EvaluateError, "no value in context for #{id}" unless val.key?(key)

  #   val = val[key]
  # end

  [val, lookup_keys.empty? ? nil : lookup_keys.join(".")]
end

#merge(bindings) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cel/context.rb', line 113

def merge(bindings)
  bindings = bindings.to_h { |k, v| NORMALIZE_KEYS[k, v] }
  if (old_bindings = @bindings)
    if @environment.container
      old_bindings = old_bindings.dup

      bindings.each_key do |key|
        container_key = :"#{@environment.container}.#{key}"

        old_bindings.delete(container_key) if old_bindings.key?(container_key)
      end
    end
    conflicts = []
    bindings = old_bindings.merge(bindings) do |k, old, new|
      conflicts << [k, old]
      new
    end
    conflicts.each do |k, v|
      bindings[:".#{k}"] = v
    end
  end
  Context.new(@environment, bindings)
end