|
| 1 | +@file:Suppress("UNCHECKED_CAST") |
| 2 | + |
| 3 | +package ru.hh.plugins.geminio.sdk.recipe.parsers.widgets |
| 4 | + |
| 5 | +import ru.hh.plugins.geminio.sdk.recipe.parsers.ParsersErrorsFactory.rootSectionErrorMessage |
| 6 | +import ru.hh.plugins.geminio.sdk.recipe.parsers.ParsersErrorsFactory.sectionErrorMessage |
| 7 | +import ru.hh.plugins.geminio.sdk.recipe.parsers.ParsersErrorsFactory.sectionRequiredParameterErrorMessage |
| 8 | +import ru.hh.plugins.utils.yaml.YamlUtils |
| 9 | +import java.io.File |
| 10 | + |
| 11 | +internal fun Map<String, Any>.resolveRawWidgetDefinitions( |
| 12 | + recipeFilePath: String? = null, |
| 13 | +): List<RawWidgetDefinition> { |
| 14 | + val normalizedRecipeFilePath = recipeFilePath?.let(::normalizePath) |
| 15 | + |
| 16 | + return resolveRawWidgetDefinitions( |
| 17 | + sourceFilePath = normalizedRecipeFilePath, |
| 18 | + includeChain = normalizedRecipeFilePath?.let(::listOf).orEmpty(), |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +private fun Map<String, Any>.resolveRawWidgetDefinitions( |
| 23 | + sourceFilePath: String?, |
| 24 | + includeChain: List<String>, |
| 25 | +): List<RawWidgetDefinition> { |
| 26 | + val widgetsList = requireNotNull(this[KEY_WIDGETS_SECTION] as? List<*>) { |
| 27 | + rootSectionErrorMessage(KEY_WIDGETS_SECTION) |
| 28 | + } |
| 29 | + |
| 30 | + return widgetsList.flatMap { rawWidget -> |
| 31 | + val widgetMap = requireNotNull(rawWidget as? Map<String, Any>) { |
| 32 | + sectionErrorMessage( |
| 33 | + KEY_WIDGETS_SECTION, |
| 34 | + "Every widget entry should be an object.", |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + val includeMap = widgetMap[KEY_INCLUDE_PARAMETER_TYPE] as? Map<String, Any> |
| 39 | + if (includeMap == null) { |
| 40 | + listOf( |
| 41 | + RawWidgetDefinition( |
| 42 | + definition = widgetMap, |
| 43 | + sourceFilePath = sourceFilePath, |
| 44 | + ) |
| 45 | + ) |
| 46 | + } else { |
| 47 | + require(widgetMap.keys == setOf(KEY_INCLUDE_PARAMETER_TYPE)) { |
| 48 | + sectionErrorMessage( |
| 49 | + KEY_WIDGETS_SECTION, |
| 50 | + "Include widget entry should declare only '$KEY_INCLUDE_PARAMETER_TYPE' " + |
| 51 | + "[keys: ${widgetMap.keys}].", |
| 52 | + ) |
| 53 | + } |
| 54 | + includeMap.resolveIncludedWidgetDefinitions( |
| 55 | + parentSourceFilePath = sourceFilePath, |
| 56 | + includeChain = includeChain, |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +private fun Map<String, Any>.resolveIncludedWidgetDefinitions( |
| 63 | + parentSourceFilePath: String?, |
| 64 | + includeChain: List<String>, |
| 65 | +): List<RawWidgetDefinition> { |
| 66 | + val sectionName = "$KEY_WIDGETS_SECTION:$KEY_INCLUDE_PARAMETER_TYPE" |
| 67 | + val includeFilePath = requireNotNull(this[KEY_INCLUDE_FILE] as? String) { |
| 68 | + sectionRequiredParameterErrorMessage( |
| 69 | + sectionName = sectionName, |
| 70 | + key = KEY_INCLUDE_FILE, |
| 71 | + ) |
| 72 | + } |
| 73 | + require(this.keys == setOf(KEY_INCLUDE_FILE)) { |
| 74 | + sectionErrorMessage( |
| 75 | + sectionName, |
| 76 | + "Include entry should declare only '$KEY_INCLUDE_FILE'.", |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + val parentFile = requireNotNull(parentSourceFilePath) { |
| 81 | + sectionErrorMessage( |
| 82 | + sectionName, |
| 83 | + "Include resolution requires source file context.", |
| 84 | + ) |
| 85 | + } |
| 86 | + val resolvedIncludeFile = File(File(parentFile).parentFile, includeFilePath) |
| 87 | + val normalizedIncludeFilePath = normalizePath(resolvedIncludeFile.path) |
| 88 | + |
| 89 | + require(normalizedIncludeFilePath !in includeChain) { |
| 90 | + val includeChainText = (includeChain + normalizedIncludeFilePath).joinToString(" -> ") |
| 91 | + sectionErrorMessage( |
| 92 | + sectionName, |
| 93 | + "Circular widgets include detected [chain: $includeChainText].", |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + val includedConfigMap = YamlUtils.loadFromConfigFile(normalizedIncludeFilePath) { throwable -> |
| 98 | + throwable.printStackTrace() |
| 99 | + } ?: throw IllegalArgumentException( |
| 100 | + sectionErrorMessage( |
| 101 | + sectionName, |
| 102 | + "Cannot load include file [path: $normalizedIncludeFilePath].", |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + require(includedConfigMap.keys == setOf(KEY_WIDGETS_SECTION)) { |
| 107 | + sectionErrorMessage( |
| 108 | + sectionName, |
| 109 | + "Included widgets file should contain only '$KEY_WIDGETS_SECTION' top-level section " + |
| 110 | + "[path: $normalizedIncludeFilePath, keys: ${includedConfigMap.keys}].", |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + return includedConfigMap.resolveRawWidgetDefinitions( |
| 115 | + sourceFilePath = normalizedIncludeFilePath, |
| 116 | + includeChain = includeChain + normalizedIncludeFilePath, |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +private fun normalizePath(path: String): String { |
| 121 | + return File(path).canonicalFile.path |
| 122 | +} |
| 123 | + |
| 124 | +private const val KEY_WIDGETS_SECTION = "widgets" |
| 125 | +private const val KEY_INCLUDE_PARAMETER_TYPE = "include" |
| 126 | +private const val KEY_INCLUDE_FILE = "file" |
0 commit comments