div-pivot-fixed.value is required property

commit_hash:71d1db2fddf9d87815702fb9771e10452c6eba2f
This commit is contained in:
pkurchatov
2026-04-03 01:00:37 +03:00
parent 77f69e0512
commit 172c2aa44d
7 changed files with 35 additions and 14 deletions
@@ -192,7 +192,7 @@ internal fun DivPivot?.equalsToConstant(other: DivPivot?): Boolean {
internal fun DivPivot?.isConstant(): Boolean {
return when (this) {
null -> true
is DivPivot.Fixed -> value.value.isConstantOrNull() && value.value.isConstantOrNull()
is DivPivot.Fixed -> value.value.isConstant() && value.unit.isConstant()
is DivPivot.Percentage -> value.value.isConstant()
}
}
@@ -126,7 +126,7 @@ internal fun ExpressionSubscriber.observePivot(
is DivPivot.Fixed -> {
val fixedPivot = pivot.value
addSubscription(fixedPivot.value?.observe(resolver, callback))
addSubscription(fixedPivot.value.observe(resolver, callback))
addSubscription(fixedPivot.unit.observe(resolver, callback))
}
@@ -147,7 +147,7 @@ internal fun View.applyTransform(
private fun View.getPivotValue(len: Int, divPivot: DivPivot, resolver: ExpressionResolver): Float {
return when (val pivot = divPivot.value()) {
is DivPivotFixed -> {
val offset = pivot.value?.evaluate(resolver)?.toFloat() ?: return len / 2f
val offset = pivot.value.evaluate(resolver).toFloat()
when (pivot.unit.evaluate(resolver)) {
DivSizeUnit.DP -> offset.dpToPxF(resources.displayMetrics)
DivSizeUnit.PX -> offset
@@ -7,7 +7,7 @@ import VGSL
public final class DivPivotFixed: Sendable {
public static let type: String = "pivot-fixed"
public let unit: Expression<DivSizeUnit> // default value: dp
public let value: Expression<Int>?
public let value: Expression<Int>
public func resolveUnit(_ resolver: ExpressionResolver) -> DivSizeUnit {
resolver.resolveEnum(unit) ?? DivSizeUnit.dp
@@ -20,13 +20,13 @@ public final class DivPivotFixed: Sendable {
public convenience init(dictionary: [String: Any], context: ParsingContext) throws {
self.init(
unit: try dictionary.getOptionalExpressionField("unit", context: context),
value: try dictionary.getOptionalExpressionField("value", context: context)
value: try dictionary.getExpressionField("value", context: context)
)
}
init(
unit: Expression<DivSizeUnit>? = nil,
value: Expression<Int>? = nil
value: Expression<Int>
) {
self.unit = unit ?? .value(.dp)
self.value = value
@@ -53,7 +53,7 @@ extension DivPivotFixed: Serializable {
var result: [String: ValidSerializationValue] = [:]
result["type"] = Self.type
result["unit"] = unit.toValidSerializationValue()
result["value"] = value?.toValidSerializationValue()
result["value"] = value.toValidSerializationValue()
return result
}
}
@@ -30,14 +30,22 @@ public final class DivPivotFixedTemplate: TemplateValue, Sendable {
private static func resolveOnlyLinks(context: TemplatesContext, parent: DivPivotFixedTemplate?) -> DeserializationResult<DivPivotFixed> {
let unitValue = { parent?.unit?.resolveOptionalValue(context: context) ?? .noValue }()
let valueValue = { parent?.value?.resolveOptionalValue(context: context) ?? .noValue }()
let errors = mergeErrors(
let valueValue = { parent?.value?.resolveValue(context: context) ?? .noValue }()
var errors = mergeErrors(
unitValue.errorsOrWarnings?.map { .nestedObjectError(field: "unit", error: $0) },
valueValue.errorsOrWarnings?.map { .nestedObjectError(field: "value", error: $0) }
)
if case .noValue = valueValue {
errors.append(.requiredFieldIsMissing(field: "value"))
}
guard
let valueNonNil = valueValue.value
else {
return .failure(NonEmptyArray(errors)!)
}
let result = DivPivotFixed(
unit: { unitValue.value }(),
value: { valueValue.value }()
value: { valueNonNil }()
)
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
}
@@ -75,13 +83,21 @@ public final class DivPivotFixedTemplate: TemplateValue, Sendable {
}()
}
}()
let errors = mergeErrors(
var errors = mergeErrors(
unitValue.errorsOrWarnings?.map { .nestedObjectError(field: "unit", error: $0) },
valueValue.errorsOrWarnings?.map { .nestedObjectError(field: "value", error: $0) }
)
if case .noValue = valueValue {
errors.append(.requiredFieldIsMissing(field: "value"))
}
guard
let valueNonNil = valueValue.value
else {
return .failure(NonEmptyArray(errors)!)
}
let result = DivPivotFixed(
unit: { unitValue.value }(),
value: { valueValue.value }()
value: { valueNonNil }()
)
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
}
@@ -65,8 +65,9 @@ public enum DivPivotTemplate: TemplateValue, Sendable {
}
private static func resolveUnknownValue(context: TemplatesContext, useOnlyLinks: Bool) -> DeserializationResult<DivPivot> {
let raw = context.templateData["type"] as? String ?? DivPivotFixed.type
let type = context.templateToType[raw] ?? raw
guard let type = (context.templateData["type"] as? String).flatMap({ context.templateToType[$0] ?? $0 }) else {
return .failure(NonEmptyArray(.requiredFieldIsMissing(field: "type")))
}
return {
var result: DeserializationResult<DivPivot>?
+4
View File
@@ -22,5 +22,9 @@
"android",
"ios",
"web"
],
"required": [
"type",
"value"
]
}