Sha256: 555552e3cd3a00e2792b4f81eacbdac5ae4f05aee5700a15af6d127120077df1

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Cel
  class Type
    def initialize(type)
      @type = type
    end

    def ==(other)
      other == @type || super
    end

    def to_str
      @type.to_s
    end

    def type
      TYPES[:type]
    end

    def cast(value)
      case @type
      when :int
        Number.new(:int, Integer(value))
      when :uint
        Number.new(:uint, Integer(value).abs)
      when :double
        Number.new(:double, Float(value))
      when :string
        String.new(String(value))
      when :bytes
        Bytes.new(value.bytes)
      when :bool
        Bool.new(value)
      when :any
        value
      else
        raise Error, "unsupported cast operation to #{@type}"
      end
    end
  end

  class ListType < Type
    attr_accessor :element_type

    def initialize(type_list)
      super(:list)
      @type_list = type_list
      @element_type = @type_list.empty? ? TYPES[:any] : @type_list.sample.type
    end

    def get(idx)
      @type_list[idx]
    end
  end

  class MapType < Type
    attr_accessor :element_type

    def initialize(type_map)
      super(:map)
      @type_map = type_map
      @element_type = @type_map.empty? ? TYPES[:any] : @type_map.keys.sample.type
    end

    def get(attrib)
      _, value = @type_map.find { |k, _| k == attrib.to_s }
      value
    end
  end

  # Primitive Cel Types

  PRIMITIVE_TYPES = %i[int uint double bool string bytes list map null_type type].freeze
  TYPES = PRIMITIVE_TYPES.to_h { |typ| [typ, Type.new(typ)] }
  TYPES[:type] = Type.new(:type)
  TYPES[:any] = Type.new(:any)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cel-0.1.2 lib/cel/ast/types.rb
cel-0.1.1 lib/cel/ast/types.rb