Sha256: cd1e0e7add0ed9632fbeb7ad8739c147e890222ae1c11d9509427d4e35b51aaa
Contents?: true
Size: 1.55 KB
Versions: 1
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.map { |typ| [typ, Type.new(typ)] }.to_h TYPES[:type] = Type.new(:type) TYPES[:any] = Type.new(:any) end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cel-0.1.0 | lib/cel/ast/types.rb |