fun parse()

in alerting/src/main/kotlin/org/opensearch/alerting/model/action/ActionExecutionScope.kt [34:88]


        fun parse(xcp: XContentParser): ActionExecutionScope {
            var type: Type? = null
            var actionExecutionScope: ActionExecutionScope? = null
            val alertFilter = mutableSetOf<AlertCategory>()

            ensureExpectedToken(Token.START_OBJECT, xcp.currentToken(), xcp)
            while (xcp.nextToken() != Token.END_OBJECT) {
                val fieldName = xcp.currentName()
                xcp.nextToken()

                // If the type field has already been set, the user has provided more than one type of schedule
                if (type != null) {
                    throw IllegalArgumentException("You can only specify one type of action execution scope.")
                }

                when (fieldName) {
                    PER_ALERT_FIELD -> {
                        type = Type.PER_ALERT
                        while (xcp.nextToken() != Token.END_OBJECT) {
                            val perAlertFieldName = xcp.currentName()
                            xcp.nextToken()
                            when (perAlertFieldName) {
                                ACTIONABLE_ALERTS_FIELD -> {
                                    ensureExpectedToken(Token.START_ARRAY, xcp.currentToken(), xcp)
                                    val allowedCategories = AlertCategory.values().map { it.toString() }
                                    while (xcp.nextToken() != Token.END_ARRAY) {
                                        val alertCategory = xcp.text()
                                        if (!allowedCategories.contains(alertCategory)) {
                                            throw IllegalStateException("Actionable alerts should be one of $allowedCategories")
                                        }
                                        alertFilter.add(AlertCategory.valueOf(alertCategory))
                                    }
                                }
                                else -> throw IllegalArgumentException(
                                    "Invalid field [$perAlertFieldName] found in per alert action execution scope."
                                )
                            }
                        }
                    }
                    PER_EXECUTION_FIELD -> {
                        type = Type.PER_EXECUTION
                        while (xcp.nextToken() != Token.END_OBJECT) {}
                    }
                    else -> throw IllegalArgumentException("Invalid field [$fieldName] found in action execution scope.")
                }
            }

            if (type == Type.PER_ALERT) {
                actionExecutionScope = PerAlertActionScope(alertFilter)
            } else if (type == Type.PER_EXECUTION) {
                actionExecutionScope = PerExecutionActionScope()
            }

            return requireNotNull(actionExecutionScope) { "Action execution scope is null." }
        }