Skip to content

Commit

Permalink
Do not set the valueType of Attribute to optionals
Browse files Browse the repository at this point in the history
Even if it is optional, the value type is still
the base type!
Will this break anything?
  • Loading branch information
helje5 committed Nov 7, 2024
1 parent 316d332 commit 4caeec6
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions Sources/ZeeQL/Access/ModelLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,12 @@ open class CoreDataModelLoader : ModelLoader {

// Note: we don't care about usesScalarValueType
let attribute = ModelAttribute(name: name)
let allowsNull = boolValue(attrs["optional"] ?? attrs["null"])
attribute.allowsNull = allowsNull
if let v = attrs["optional"] ?? attrs["null"] {
attribute.allowsNull = boolValue(v)
}
else {
assert(attribute.allowsNull == nil)
}

if name == attrs["skip"] { // <attribute skip="is_person">
assert(attribute.patternType == .none)
Expand All @@ -696,41 +700,26 @@ open class CoreDataModelLoader : ModelLoader {
if let attrType = attrs["attributeType"], !attrType.isEmpty {
let lc = attrType.lowercased()

// Note: The valueType is NOT the Optional<X> if the attribute is
// optional!
if lc == "string" {
attribute.valueType = allowsNull ? Optional<String>.self : String.self
attribute.valueType = String.self
}
else if lc.hasPrefix("int") { // funny, ?: doesn't work here
if attrType.hasSuffix("16") {
if allowsNull { attribute.valueType = Optional<Int16>.self }
else { attribute.valueType = Int16.self }
}
else if attrType.hasSuffix("32") {
if allowsNull { attribute.valueType = Optional<Int32>.self }
else { attribute.valueType = Int32.self }
}
else if attrType.hasSuffix("64") {
if allowsNull { attribute.valueType = Optional<Int64>.self }
else { attribute.valueType = Int64.self }
}
else {
if allowsNull { attribute.valueType = Optional<Int>.self }
else { attribute.valueType = Int.self }
}
}
else if lc == "date" {
attribute.valueType = allowsNull ? Optional<Date>.self : Date.self
if attrType.hasSuffix("16") { attribute.valueType = Int16.self }
else if attrType.hasSuffix("32") { attribute.valueType = Int32.self }
else if attrType.hasSuffix("64") { attribute.valueType = Int64.self }
else { attribute.valueType = Int .self }
}
else if lc == "date" { attribute.valueType = Date.self }
else if lc == "binary" || lc == "data" {
attribute.valueType = allowsNull ? Optional<Data>.self : Data.self
attribute.valueType = Data.self
}
else if lc == "decimal" {
// TODO: 'usesScalarValueType=YES'
if allowsNull { attribute.valueType = Optional<Decimal>.self }
else { attribute.valueType = Decimal.self }
}
else if lc.hasPrefix("bool") {
attribute.valueType = allowsNull ? Optional<Bool>.self : Bool.self
attribute.valueType = Decimal.self
}
else if lc.hasPrefix("bool") { attribute.valueType = Bool.self }
else if lc.hasPrefix("transformable") {
// valueTransformerName="xx"
// customClassName="xx"
Expand Down Expand Up @@ -825,8 +814,9 @@ open class CoreDataModelLoader : ModelLoader {

if let v = attrs["elementID"], !v.isEmpty { attribute.elementID = v }

let allowsNull = boolValue(attrs["optional"])
attribute.allowsNull = allowsNull
if let v = attrs["optional"] ?? attrs["null"] {
attribute.allowsNull = boolValue(v)
}

relship.joins = [ Join(source : attributeName,
destination : primaryKeyAttributeName) ]
Expand Down

0 comments on commit 4caeec6

Please sign in to comment.