# NSString, NSNumber, NSArray, NSDictionary or NSNull $number_lambda = lambda{|num| return "#{num}" } $string_lambda = lambda{|num| return "\"STR_#{num}\"" } $date_lambda = lambda{|num| return "NSDate( timeIntervalSince1970:NSDate( timeIntervalSinceNow:-#{num}).timeIntervalSince1970 )" } class SwiftPropertyType attr_accessor :swift_type_symbol attr_accessor :swift_type_name attr_accessor :serialized_json_type attr_accessor :auto_bridged attr_accessor :swift_kind attr_accessor :test_value_lambda attr_accessor :custom_marshaling attr_accessor :custom_unmarshaling attr_accessor :custom_equality_test attr_accessor :hashable_value_lambda def initialize( swift_type_symbol, serialized_json_type, auto_bridged:false, swift_kind: :primitive, test_value:lambda{|num| 'undefined' }, unmarshal_method: nil) @swift_type_symbol = swift_type_symbol @swift_type_name = swift_type_symbol.to_s @serialized_json_type = serialized_json_type @auto_bridged = auto_bridged @swift_kind = swift_kind @test_value_lambda = test_value @custom_equality_test = nil @hashable_value_lambda = nil @unmarshal_method_lambda = unmarshal_method end def self.all_swift_kinds { primitive: 'Swift primitive, e.g. Int, String', class: 'Swift Class', struct: 'Swift Struct', enum: 'Swift Enum' } end def make_test_value( index ) @test_value_lambda.call( index ) end def hashable_value( var_name, is_optional) if @hashable_value_lambda.nil? return var_name else return @hashable_value_lambda.call( var_name, is_optional ) end end end class StringEnumPropertyType < SwiftPropertyType attr_accessor :enum def initialize( enum, serialized_json_type, test_value:lambda{|num| 'undefined' } ) super( enum.swift_type_symbol, serialized_json_type, swift_kind: :enum, test_value:test_value ) @enum = enum end end class SwiftObjectPropertyType < SwiftPropertyType attr_accessor :swift_class def initialize( swift_class, serialized_json_type, test_value:lambda{|num| 'undefined' } ) super( swift_class.swift_type_symbol, serialized_json_type, swift_kind: :class, test_value:test_value ) @swift_class = swift_class end end def initial_property_types iso_date = SwiftPropertyType.new( :NSDate, :String, auto_bridged:false, test_value: $date_lambda ) iso_date.custom_marshaling = lambda{|dest, value| "#{dest} = DateFormatting.isoStringFromDate( #{value} )" } iso_date.custom_unmarshaling = lambda{|dest, value| "#{dest} = DateFormatting.dateFromISOString( #{value} )" } iso_date.custom_equality_test = lambda{|name, other| "optionalDatesEqual( #{name}, #{other})" } int_type = SwiftPropertyType.new( :Int, :NSNumber, auto_bridged:true, test_value: $number_lambda ) uint_type = SwiftPropertyType.new( :UInt, :NSNumber, auto_bridged:true, test_value: $number_lambda ) float_type = SwiftPropertyType.new( :Float, :NSNumber, auto_bridged:true, test_value: $number_lambda ) double_type = SwiftPropertyType.new( :Double, :NSNumber, auto_bridged:true, test_value: $number_lambda ) bool_type = SwiftPropertyType.new( :Bool, :NSNumber, auto_bridged:true, test_value: lambda{|num| return num.even? ? 'true' : 'false' }) int_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.integerValue" } uint_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.unsignedIntegerValue" } float_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.floatValue" } double_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.doubleValue" } bool_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.boolValue" } # Specified-Precision numeric types int8_type = SwiftPropertyType.new( :Int8, :NSNumber, auto_bridged:true, test_value: $number_lambda ) int16_type = SwiftPropertyType.new( :Int16, :NSNumber, auto_bridged:true, test_value: $number_lambda ) int32_type = SwiftPropertyType.new( :Int32, :NSNumber, auto_bridged:true, test_value: $number_lambda ) int64_type = SwiftPropertyType.new( :Int64, :NSNumber, auto_bridged:true, test_value: $number_lambda ) uint8_type = SwiftPropertyType.new( :UInt8, :NSNumber, auto_bridged:true, test_value: $number_lambda ) uint16_type = SwiftPropertyType.new( :UInt16, :NSNumber, auto_bridged:true, test_value: $number_lambda ) uint32_type = SwiftPropertyType.new( :UInt32, :NSNumber, auto_bridged:true, test_value: $number_lambda ) uint64_type = SwiftPropertyType.new( :UInt64, :NSNumber, auto_bridged:true, test_value: $number_lambda ) float80_type = SwiftPropertyType.new( :Float80, :NSNumber, auto_bridged:true, test_value: $number_lambda ) # TODO: unmarshaling for specified-precision numeric types # int8_type.custom_unmarshaling # int16_type.custom_unmarshaling # int32_type.custom_unmarshaling # int64_type.custom_unmarshaling # uint8_type.custom_unmarshaling # uint16_type.custom_unmarshaling # uint32_type.custom_unmarshaling # uint64_type.custom_unmarshaling # float80_type.custom_unmarshaling return [ int_type, uint_type, float_type, double_type, bool_type, int8_type, int16_type, int32_type, int64_type, uint8_type, uint16_type, uint32_type, uint64_type, SwiftPropertyType.new( :String, :NSString, auto_bridged:true, test_value: $string_lambda ), SwiftPropertyType.new( :NSString, :NSString, auto_bridged:true, test_value: $string_lambda ), SwiftPropertyType.new( :USIObjectID, :NSString, auto_bridged:true, test_value: $string_lambda ), SwiftPropertyType.new( :AnvilEndpoint, :AnvilEndpoint, auto_bridged:true, test_value: nil ), iso_date ] end