@@ -157,31 +157,29 @@ extension UserAttribute {
157157 return try value!. isLessOrEqual ( than: rawAttributeValue!, condition: stringRepresentation, name: nameFinal)
158158 // semantic versioning seems unique. the comarison is to compare verion but the passed in version is the target version.
159159 case . semver_eq:
160- guard let targetValue = AttributeValue ( value: rawAttributeValue) else {
161- throw OptimizelyError . evaluateAttributeInvalidCondition ( " attribute value \( nameFinal) invalid type " )
162- }
160+ let targetValue = try targetAsAttributeValue ( value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
163161 return try targetValue. isSemanticVersionEqual ( than: value!. stringValue)
164162 case . semver_lt:
165- guard let targetValue = AttributeValue ( value: rawAttributeValue) else {
166- throw OptimizelyError . evaluateAttributeInvalidCondition ( " attribute value \( nameFinal) invalid type " )
167- }
163+ let targetValue = try targetAsAttributeValue ( value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
168164 return try targetValue. isSemanticVersionLess ( than: value!. stringValue)
169165 case . semver_le:
170- guard let targetValue = AttributeValue ( value: rawAttributeValue) else {
171- throw OptimizelyError . evaluateAttributeInvalidCondition ( " attribute value \( nameFinal) invalid type " )
172- }
166+ let targetValue = try targetAsAttributeValue ( value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
173167 return try targetValue. isSemanticVersionLessOrEqual ( than: value!. stringValue)
174168 case . semver_gt:
175- guard let targetValue = AttributeValue ( value: rawAttributeValue) else {
176- throw OptimizelyError . evaluateAttributeInvalidCondition ( " attribute value \( nameFinal) invalid type " )
177- }
169+ let targetValue = try targetAsAttributeValue ( value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
178170 return try targetValue. isSemanticVersionGreater ( than: value!. stringValue)
179171 case . semver_ge:
180- guard let targetValue = AttributeValue ( value: rawAttributeValue) else {
181- throw OptimizelyError . evaluateAttributeInvalidCondition ( " attribute value \( nameFinal) invalid type " )
182- }
172+ let targetValue = try targetAsAttributeValue ( value: rawAttributeValue, attribute: value, nameFinal: nameFinal)
183173 return try targetValue. isSemanticVersionGreaterOrEqual ( than: value!. stringValue)
184174 }
185175 }
186176
177+ private func targetAsAttributeValue( value: Any ? , attribute: AttributeValue ? , nameFinal: String ) throws -> AttributeValue {
178+ guard let targetValue = AttributeValue ( value: value) , targetValue. isComparable ( with: attribute!) else {
179+ throw OptimizelyError . evaluateAttributeInvalidCondition ( " attribute value \( nameFinal) invalid type " )
180+ }
181+
182+ return targetValue
183+ }
184+
187185}
0 commit comments