mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
103 lines
3.9 KiB
Swift
103 lines
3.9 KiB
Swift
// Generated code. Do not modify.
|
|
|
|
import CommonCore
|
|
import Foundation
|
|
import Serialization
|
|
|
|
public final class EntityWithArrayOfEnumsTemplate: TemplateValue, TemplateDeserializable {
|
|
public typealias Item = EntityWithArrayOfEnums.Item
|
|
|
|
public static let type: String = "entity_with_array_of_enums"
|
|
public let parent: String? // at least 1 char
|
|
public let items: Field<[Item]>? // at least 1 elements
|
|
|
|
static let parentValidator: AnyValueValidator<String> =
|
|
makeStringValidator(minLength: 1)
|
|
|
|
public convenience init(dictionary: [String: Any], templateToType: TemplateToType) throws {
|
|
do {
|
|
self.init(
|
|
parent: try dictionary.getOptionalField("type", validator: Self.parentValidator),
|
|
items: try dictionary.getOptionalArray("items")
|
|
)
|
|
} catch let DeserializationError.invalidFieldRepresentation(field: field, representation: representation) {
|
|
throw DeserializationError.invalidFieldRepresentation(field: "entity_with_array_of_enums_template." + field, representation: representation)
|
|
}
|
|
}
|
|
|
|
init(
|
|
parent: String?,
|
|
items: Field<[Item]>? = nil
|
|
) {
|
|
self.parent = parent
|
|
self.items = items
|
|
}
|
|
|
|
private static func resolveOnlyLinks(context: Context, parent: EntityWithArrayOfEnumsTemplate?) -> DeserializationResult<EntityWithArrayOfEnums> {
|
|
let itemsValue = parent?.items?.resolveValue(context: context, validator: ResolvedValue.itemsValidator) ?? .noValue
|
|
var errors = mergeErrors(
|
|
itemsValue.errorsOrWarnings?.map { .nestedObjectError(field: "items", error: $0) }
|
|
)
|
|
if case .noValue = itemsValue {
|
|
errors.append(.requiredFieldIsMissing(field: "items"))
|
|
}
|
|
guard
|
|
let itemsNonNil = itemsValue.value
|
|
else {
|
|
return .failure(NonEmptyArray(errors)!)
|
|
}
|
|
let result = EntityWithArrayOfEnums(
|
|
items: itemsNonNil
|
|
)
|
|
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
|
|
}
|
|
|
|
public static func resolveValue(context: Context, parent: EntityWithArrayOfEnumsTemplate?, useOnlyLinks: Bool) -> DeserializationResult<EntityWithArrayOfEnums> {
|
|
if useOnlyLinks {
|
|
return resolveOnlyLinks(context: context, parent: parent)
|
|
}
|
|
var itemsValue: DeserializationResult<[EntityWithArrayOfEnums.Item]> = parent?.items?.value(validatedBy: ResolvedValue.itemsValidator) ?? .noValue
|
|
context.templateData.forEach { key, __dictValue in
|
|
switch key {
|
|
case "items":
|
|
itemsValue = deserialize(__dictValue, validator: ResolvedValue.itemsValidator).merged(with: itemsValue)
|
|
case parent?.items?.link:
|
|
itemsValue = itemsValue.merged(with: deserialize(__dictValue, validator: ResolvedValue.itemsValidator))
|
|
default: break
|
|
}
|
|
}
|
|
var errors = mergeErrors(
|
|
itemsValue.errorsOrWarnings?.map { .nestedObjectError(field: "items", error: $0) }
|
|
)
|
|
if case .noValue = itemsValue {
|
|
errors.append(.requiredFieldIsMissing(field: "items"))
|
|
}
|
|
guard
|
|
let itemsNonNil = itemsValue.value
|
|
else {
|
|
return .failure(NonEmptyArray(errors)!)
|
|
}
|
|
let result = EntityWithArrayOfEnums(
|
|
items: itemsNonNil
|
|
)
|
|
return errors.isEmpty ? .success(result) : .partialSuccess(result, warnings: NonEmptyArray(errors)!)
|
|
}
|
|
|
|
private func mergedWithParent(templates: Templates) throws -> EntityWithArrayOfEnumsTemplate {
|
|
guard let parent = parent, parent != Self.type else { return self }
|
|
guard let parentTemplate = templates[parent] as? EntityWithArrayOfEnumsTemplate else {
|
|
throw DeserializationError.unknownType(type: parent)
|
|
}
|
|
let mergedParent = try parentTemplate.mergedWithParent(templates: templates)
|
|
|
|
return EntityWithArrayOfEnumsTemplate(
|
|
parent: nil,
|
|
items: items ?? mergedParent.items
|
|
)
|
|
}
|
|
|
|
public func resolveParent(templates: Templates) throws -> EntityWithArrayOfEnumsTemplate {
|
|
return try mergedWithParent(templates: templates)
|
|
}
|
|
}
|