mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
102 lines
3.4 KiB
Swift
102 lines
3.4 KiB
Swift
// Generated code. Do not modify.
|
|
|
|
import Foundation
|
|
import Serialization
|
|
import VGSL
|
|
|
|
public final class StringValueTemplate: TemplateValue, Sendable {
|
|
public static let type: String = "string"
|
|
public let parent: String?
|
|
public let value: Field<Expression<String>>?
|
|
|
|
public convenience init(dictionary: [String: Any], templateToType: [TemplateName: String]) throws {
|
|
self.init(
|
|
parent: dictionary["type"] as? String,
|
|
value: dictionary.getOptionalExpressionField("value")
|
|
)
|
|
}
|
|
|
|
init(
|
|
parent: String?,
|
|
value: Field<Expression<String>>? = nil
|
|
) {
|
|
self.parent = parent
|
|
self.value = value
|
|
}
|
|
|
|
private static func resolveOnlyLinks(context: TemplatesContext, parent: StringValueTemplate?) -> DeserializationResult<StringValue> {
|
|
let valueValue = { parent?.value?.resolveValue(context: context) ?? .noValue }()
|
|
var errors = mergeErrors(
|
|
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 = StringValue(
|
|
value: { valueNonNil }()
|
|
)
|
|
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
|
|
}
|
|
|
|
public static func resolveValue(context: TemplatesContext, parent: StringValueTemplate?, useOnlyLinks: Bool) -> DeserializationResult<StringValue> {
|
|
if useOnlyLinks {
|
|
return resolveOnlyLinks(context: context, parent: parent)
|
|
}
|
|
var valueValue: DeserializationResult<Expression<String>> = { parent?.value?.value() ?? .noValue }()
|
|
_ = {
|
|
// Each field is parsed in its own lambda to keep the stack size managable
|
|
// Otherwise the compiler will allocate stack for each intermediate variable
|
|
// upfront even when we don't actually visit a relevant branch
|
|
for (key, __dictValue) in context.templateData {
|
|
_ = {
|
|
if key == "value" {
|
|
valueValue = deserialize(__dictValue).merged(with: valueValue)
|
|
}
|
|
}()
|
|
_ = {
|
|
if key == parent?.value?.link {
|
|
valueValue = valueValue.merged(with: { deserialize(__dictValue) })
|
|
}
|
|
}()
|
|
}
|
|
}()
|
|
var errors = mergeErrors(
|
|
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 = StringValue(
|
|
value: { valueNonNil }()
|
|
)
|
|
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
|
|
}
|
|
|
|
private func mergedWithParent(templates: [TemplateName: Any]) throws -> StringValueTemplate {
|
|
guard let parent = parent, parent != Self.type else { return self }
|
|
guard let parentTemplate = templates[parent] as? StringValueTemplate else {
|
|
throw DeserializationError.unknownType(type: parent)
|
|
}
|
|
let mergedParent = try parentTemplate.mergedWithParent(templates: templates)
|
|
|
|
return StringValueTemplate(
|
|
parent: nil,
|
|
value: value ?? mergedParent.value
|
|
)
|
|
}
|
|
|
|
public func resolveParent(templates: [TemplateName: Any]) throws -> StringValueTemplate {
|
|
return try mergedWithParent(templates: templates)
|
|
}
|
|
}
|