Files
divkit/api_generator/tests/references/swift/EntityWithComplexPropertyTemplate.swift
T
feldspar ce9af20a5b Revert parsing logic update
commit_hash:c9419f063b7f8c5df41675acc9ed49d862850a2c
2026-01-16 23:06:00 +03:00

176 lines
6.8 KiB
Swift

// Generated code. Do not modify.
import CommonCore
import Foundation
import Serialization
public final class EntityWithComplexPropertyTemplate: TemplateValue, Sendable {
public final class PropertyTemplate: TemplateValue, Sendable {
public let value: Field<Expression<URL>>?
public convenience init(dictionary: [String: Any], templateToType: [TemplateName: String]) throws {
self.init(
value: dictionary.getOptionalExpressionField("value", transform: URL.makeFromNonEncodedString)
)
}
init(
value: Field<Expression<URL>>? = nil
) {
self.value = value
}
private static func resolveOnlyLinks(context: TemplatesContext, parent: PropertyTemplate?) -> DeserializationResult<EntityWithComplexProperty.Property> {
let valueValue = parent?.value?.resolveValue(context: context, transform: URL.makeFromNonEncodedString) ?? .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 = EntityWithComplexProperty.Property(
value: valueNonNil
)
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
}
public static func resolveValue(context: TemplatesContext, parent: PropertyTemplate?, useOnlyLinks: Bool) -> DeserializationResult<EntityWithComplexProperty.Property> {
if useOnlyLinks {
return resolveOnlyLinks(context: context, parent: parent)
}
var valueValue: DeserializationResult<Expression<URL>> = parent?.value?.value() ?? .noValue
context.templateData.forEach { key, __dictValue in
switch key {
case "value":
valueValue = deserialize(__dictValue, transform: URL.makeFromNonEncodedString).merged(with: valueValue)
case parent?.value?.link:
valueValue = valueValue.merged(with: { deserialize(__dictValue, transform: URL.makeFromNonEncodedString) })
default: break
}
}
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 = EntityWithComplexProperty.Property(
value: valueNonNil
)
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
}
private func mergedWithParent(templates: [TemplateName: Any]) throws -> PropertyTemplate {
return self
}
public func resolveParent(templates: [TemplateName: Any]) throws -> PropertyTemplate {
return try mergedWithParent(templates: templates)
}
}
public static let type: String = "entity_with_complex_property"
public let parent: String?
public let property: Field<PropertyTemplate>?
public convenience init(dictionary: [String: Any], templateToType: [TemplateName: String]) throws {
self.init(
parent: dictionary["type"] as? String,
property: dictionary.getOptionalField("property", templateToType: templateToType)
)
}
init(
parent: String?,
property: Field<PropertyTemplate>? = nil
) {
self.parent = parent
self.property = property
}
private static func resolveOnlyLinks(context: TemplatesContext, parent: EntityWithComplexPropertyTemplate?) -> DeserializationResult<EntityWithComplexProperty> {
let propertyValue = parent?.property?.resolveValue(context: context, useOnlyLinks: true) ?? .noValue
var errors = mergeErrors(
propertyValue.errorsOrWarnings?.map { .nestedObjectError(field: "property", error: $0) }
)
if case .noValue = propertyValue {
errors.append(.requiredFieldIsMissing(field: "property"))
}
guard
let propertyNonNil = propertyValue.value
else {
return .failure(NonEmptyArray(errors)!)
}
let result = EntityWithComplexProperty(
property: propertyNonNil
)
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
}
public static func resolveValue(context: TemplatesContext, parent: EntityWithComplexPropertyTemplate?, useOnlyLinks: Bool) -> DeserializationResult<EntityWithComplexProperty> {
if useOnlyLinks {
return resolveOnlyLinks(context: context, parent: parent)
}
var propertyValue: DeserializationResult<EntityWithComplexProperty.Property> = .noValue
context.templateData.forEach { key, __dictValue in
switch key {
case "property":
propertyValue = deserialize(__dictValue, templates: context.templates, templateToType: context.templateToType, type: EntityWithComplexPropertyTemplate.PropertyTemplate.self).merged(with: propertyValue)
case parent?.property?.link:
propertyValue = propertyValue.merged(with: { deserialize(__dictValue, templates: context.templates, templateToType: context.templateToType, type: EntityWithComplexPropertyTemplate.PropertyTemplate.self) })
default: break
}
}
if let parent = parent {
_ = propertyValue = propertyValue.merged(with: { parent.property?.resolveValue(context: context, useOnlyLinks: true) })
}
var errors = mergeErrors(
propertyValue.errorsOrWarnings?.map { .nestedObjectError(field: "property", error: $0) }
)
if case .noValue = propertyValue {
errors.append(.requiredFieldIsMissing(field: "property"))
}
guard
let propertyNonNil = propertyValue.value
else {
return .failure(NonEmptyArray(errors)!)
}
let result = EntityWithComplexProperty(
property: propertyNonNil
)
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
}
private func mergedWithParent(templates: [TemplateName: Any]) throws -> EntityWithComplexPropertyTemplate {
guard let parent = parent, parent != Self.type else { return self }
guard let parentTemplate = templates[parent] as? EntityWithComplexPropertyTemplate else {
throw DeserializationError.unknownType(type: parent)
}
let mergedParent = try parentTemplate.mergedWithParent(templates: templates)
return EntityWithComplexPropertyTemplate(
parent: nil,
property: property ?? mergedParent.property
)
}
public func resolveParent(templates: [TemplateName: Any]) throws -> EntityWithComplexPropertyTemplate {
let merged = try mergedWithParent(templates: templates)
return EntityWithComplexPropertyTemplate(
parent: nil,
property: try merged.property?.resolveParent(templates: templates)
)
}
}