class SwiftGenerator::SwiftPersistentProperty

Attributes

json_key[RW]

Public Class Methods

new(swift_class, property_name, property_type, mutability=:let, initialization_value=nil, collection_type: nil, json_key: nil, rest_omit:nil ) click to toggle source
Calls superclass method SwiftGenerator::SwiftProperty::new
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1040
def initialize(swift_class, property_name, property_type, mutability=:let, initialization_value=nil, collection_type: nil, json_key: nil, rest_omit:nil  )
        super(swift_class, property_name, property_type, mutability, initialization_value:initialization_value, collection_type: collection_type, rest_omit:rest_omit)
        @is_persistent = true
        @json_key = json_key.nil? ? property_name : json_key
end

Public Instance Methods

all_collection_types() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1337
def all_collection_types
        [
                :array,
                :dictionaryByString
        ]
end
marshal_array(marshal_method) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1108
def marshal_array(marshal_method)
        return if self.rest_omit == :omit
        omit_null = self.rest_omit == :omit_if_null

        case @property_type.swift_kind
                when :primitive
                        if( @mutability_type.mutability_id != :optional )
                                marshal_method << "ERROR Non-optional properties not yet supported"
                        end

                        marshal_template(marshal_method, omit_null) { |m, unwrapped_var, destination|
                                m << "var newArray = NSMutableArray()"
                                m << "for element in #{unwrapped_var} {"
                                m.ii       "newArray.addObject( element as #{@property_type.serialized_json_type.to_s} )"
                                m << "}"
                                m << "#{destination} = newArray"
                        }

                when :class
                        if( @mutability_type.mutability_id != :optional )
                                marshal_method << "ERROR Non-optional properties not yet supported"
                        end

                        marshal_template(marshal_method, omit_null) { |m, unwrapped_var, destination|
                                m.ii "#{destination}  = #{@property_type.swift_type_name}.arrayToJSON(#{unwrapped_var})"
                        }

                when :struct
                        return 'Arrays of structs not yet supported'

                when :enum
                        if( @mutability_type.mutability_id != :optional )
                                marshal_method << "ERROR Non-optional properties not yet supported"
                        end

                        marshal_template(marshal_method, omit_null) { |m, unwrapped_var, destination|
                                m << "var newArray = NSMutableArray()"
                                m << "for element in #{unwrapped_var} {"
                                m.ii       "newArray.addObject( #{@property_type.enum.marshal_expression( "element" )} )"
                                m << "}"
                                m << "#{destination} = newArray"
                        }

                else
                        puts "ERROR: Unknown swift_kind: #{@property_type.swift_kind.to_s}"
        end
end
marshal_code(marshal_method) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1046
def marshal_code(marshal_method)
        if @collection_type.nil?
                marshal_single_element(marshal_method)
        else
                case @collection_type

                        when :array
                                marshal_array(marshal_method)

                        when :dictionaryByString

                        else
                                puts "ERROR: Unknown collection_type: #{@collection_type.to_s}"
                end
        end
end
marshal_single_element(marshal_method) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1063
def marshal_single_element(marshal_method)
        return if self.rest_omit == :omit
        omit_null = self.rest_omit == :omit_if_null

                case @property_type.swift_kind
                when :primitive
                        case @mutability_type.mutability_id
                                when :optional
                                        marshal_template(marshal_method, omit_null){ |m, unwrapped_var, destination|
                                                m.<< "#{destination} = #{unwrapped_var}"
                                        }
                                else
                                        marshal_method << "ERROR Non-optional properties not yet supported"
                        end

                when :enum
                        case @mutability_type.mutability_id
                                when :optional
                                        marshal_template(marshal_method, omit_null){ |m, unwrapped_var, destination|
                                                # TODO: Make this look like the class property method below
                                                m << "#{destination} = #{@property_type.enum.marshal_expression( unwrapped_var)}"
                                        }
                                else
                                        marshal_method << "ERROR Non-optional properties not yet supported"
                        end

                when :class
                        case @mutability_type.mutability_id
                                when :optional
                                        marshal_template(marshal_method, omit_null){ |m, unwrapped_var, destination|
                                                @property_type.swift_class.insert_marshal_expression( m, unwrapped_var, destination)
                                        }
                                else
                                        marshal_method << "ERROR Non-optional properties not yet supported"
                        end

                when :struct
                        return 'Structs not yet supported'

                else
                        puts "ERROR: Unknown swift_kind: #{@property_type.swift_kind.to_s}"
        end
end
marshal_template(marshal_method, omit_null, &detail) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1157
def marshal_template(marshal_method, omit_null, &detail)
        m = marshal_method
        unwrapped_var = @property_name + "Val"
        destination = "jsonObject[ \"#{@json_key}\" ]"
        m << ""
        m << "if let #{unwrapped_var} = #{@property_name} {"

        if self.property_type.custom_marshaling.nil?
                m.indent += 1
                detail.call( m, unwrapped_var, destination)
                m.indent -= 1
        else
                m.ii self.property_type.custom_marshaling.call( destination ,unwrapped_var)
        end
        m << "} else {"
        if( ! omit_null )
                m << "\t#{destination} = NSNull()"
        else
                m << "// Omit nil values"
        end

        m << "}"
end
unmarshal_array(unmarshal_method) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1256
def unmarshal_array(unmarshal_method)
        m = unmarshal_method
        case @property_type.swift_kind
                when :primitive
                        if( @mutability_type.mutability_id == :optional )
                                unwrapped_var = @property_name + "Array"
                                prop_type = @property_type.swift_type_name
                                json_type = @property_type.serialized_json_type.to_s
                                m << ""
                                m << "if let #{unwrapped_var} = jsonObject[ \"#{@property_name}\" ] as? NSArray {"
                                m._i       "var newArray = [#{prop_type}]()"
                                m <<       "for element in #{unwrapped_var} {"
                                m._i               "if let typedElement = element as? #{json_type} {"
                                if @property_type.auto_bridged
                                        m.ii              "newArray.append( typedElement )"
                                else
                                        m._i              "if let unmarshalledValue = typedElement as #{prop_type} {"
                                        m.ii                      "newArray.append( unmarshalledValue )"
                                        m <<              "} else {"
                                        m.ii                      "println( \"Error in converting \\(typedElement) to #{prop_type}\")"
                                        m._o              "}"
                                end
                                m <<               "} else {"
                                m.ii                       "println( \"Unexpected json value: \\(element) for #{prop_type}\")"
                                m._o               "}"
                                m <<       "}"
                                m._o       "#{@property_name} = newArray"
                                m << "} else {"
                                m.ii       "#{@property_name} = nil"
                                m << "}"
                        else
                                m << "ERROR Non-optional properties not yet supported"
                        end
                when :class
                        if( @mutability_type.mutability_id == :optional )
                                unwrapped_var = @property_name + "Array"
                                prop_type = @property_type.swift_type_name
                                m << ""
                                m << "if let #{unwrapped_var} = jsonObject[ \"#{@property_name}\" ] as? NSArray {"
                                m.ii       "#{@property_name} = #{prop_type}.arrayFromJSON( #{unwrapped_var} ) as? [#{prop_type}]"
                                m << "} else {"
                                m.ii       "#{@property_name} = nil"
                                m << "}"
                        else
                                m << "ERROR Non-optional properties not yet supported"
                        end

                when :struct
                        m << 'ERROR Arrays of structs not yet supported'

                when :enum
                        if( @mutability_type.mutability_id == :optional )
                                unwrapped_var = @property_name + "Array"
                                prop_type = @property_type.swift_type_name
                                m << ""
                                m << "if let #{unwrapped_var} = jsonObject[ \"#{@property_name}\" ] as? NSArray {"
                                m._i       "var newArray = [#{prop_type}]()"
                                m <<       "for element in #{unwrapped_var} {"
                                m._i               "if let typedElement = element as? NSString {"
                                m._i                       "if let enumValue = #{@property_type.enum.unmarshal_expression( "typedElement" )} {"
                                m.ii                               "newArray.append( enumValue )"
                                m <<                       "} else {"
                                m.ii                               "println( \"Error in converting \\(typedElement) to #{prop_type}\")"
                                m._o                       "}"
                                m <<               "} else {"
                                m.ii                       "println( \"Unexpected json value: \\(element) for #{prop_type}\")"
                                m._o               "}"
                                m <<       "}"
                                m._o       "#{@property_name} = newArray"
                                m << "} else {"
                                m.ii       "#{@property_name} = nil"
                                m << "}"
                        else
                                m << "ERROR Non-optional properties not yet supported"
                        end
                else
                        puts "ERROR: Unknown swift_kind: #{@property_type.swift_kind.to_s}"
        end
end
unmarshal_code(unmarshal_method) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1182
def unmarshal_code(unmarshal_method)
        if @collection_type.nil?
                unmarshal_single_element(unmarshal_method)
        else
                case @collection_type
                        when :array
                                unmarshal_array(unmarshal_method)

                        when :dictionaryByString

                        else
                                puts "ERROR: Unknown collection_type: #{@collection_type.to_s}"
                end
        end
end
unmarshal_single_element(unmarshal_method) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 1198
def unmarshal_single_element(unmarshal_method)
        m = unmarshal_method
        case @property_type.swift_kind
                when :primitive, :enum
                        case @mutability_type.mutability_id
                                when :optional
                                        unwrapped_var = @property_name + "Val"
                                        m << ""
                                        m << "if let #{unwrapped_var} = jsonObject[ \"#{@json_key}\" ] as? #{@property_type.serialized_json_type} {"

                                        if self.property_type.custom_unmarshaling.nil?
                                                m.ii "#{@property_name} = #{unwrapped_var} "
                                        else
                                                m.ii self.property_type.custom_unmarshaling.call( @property_name, unwrapped_var )
                                        end

                                        m << "} else {"
                                        m.ii      "#{@property_name} = nil"
                                        m << "}"
                                else
                                        m << "ERROR Non-optional properties not yet supported"
                        end

                        # if let aVal = jsonObject[ "a" ] as? Int {
                        #   a = aVal
                        # } else {
                        #   a = nil
                        # }

                        # if @property_type.auto_bridged
                        #   return "#{@property_name} = jsonObject[ \"#{@json_key}\" ]"
                        # else
                        return "#{@property_name} = jsonObject[ \"#{@json_key}\" ] as #{property_declared_type}"
                # end

                when :class
                        case @mutability_type.mutability_id
                                when :optional
                                        unwrapped_var = @property_name + "Val"
                                        # TODO: check for success
                                        # m << "var success = false"
                                        m << "if let #{unwrapped_var} = jsonObject[ \"#{@json_key}\" ] as? #{@property_type.serialized_json_type} {"
                                        self.property_type.swift_class.insert_unmarshal_expression( m, unwrapped_var, @property_name )
                                        m << "} else {"
                                        m.ii      "#{@property_name} = nil"
                                        m << "}"
                                else
                                        m << "ERROR Non-optional properties not yet supported"
                        end

                when :struct
                        return 'Structs not yet supported'

                else
                        puts "ERROR: Unknown swift_kind: #{@property_type.swift_kind.to_s}"
        end
end