<commit_before>package com.avsystem.commons
package spring

import java.{util => ju}

import com.avsystem.commons.spring.AttrNames._
import scala.annotation.nowarn
import com.typesafe.config._
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder
import org.springframework.beans.factory.config.{BeanDefinitionHolder, ConstructorArgumentValues, RuntimeBeanNameReference, RuntimeBeanReference}
import org.springframework.beans.factory.support._
import org.springframework.beans.{MutablePropertyValues, PropertyValue}
import org.springframework.core.io.Resource

class HoconBeanDefinitionReader(registry: BeanDefinitionRegistry)
  extends AbstractBeanDefinitionReader(registry) {

  import com.typesafe.config.ConfigValueType._

  private implicit class ConfigValueExtensions(value: ConfigValue) {
    def as[T: HoconType] =
      implicitly[HoconType[T]].get(value)
  }

  private val autowireMapping = AutowireMapping.withDefault {
    v => throw new IllegalArgumentException(s"Invalid value $v for $AutowireAttr attribute")
  }

  private val dependencyCheckMapping = DependencyCheckMapping.withDefault {
    v => throw new IllegalArgumentException(s"Invalid value $v for $DependencyCheckAttr attribute")
  }

  private def setup[T](t: T)(setupFunc: T => Any) = {
    setupFunc(t)
    t
  }

  private def iterate(obj: ConfigObject)
    (attrFun: (String, ConfigValue) => Any)
    (propFun: (String, ConfigValue) => Any) =
    obj.asScala.foreach {
      case (key, _) if key.startsWith("_") =>
      case (key, value) if key.startsWith("%") => attrFun(key, value)
      case (key, value) => propFun(key, value)
    }

  private def validateObj(
    required: Set[String] = Set.empty,
    requiredAny: Set[String] = Set.empty,
    allowed: Set[String] = Set.empty,
    props: Boolean = false
  )(obj: ConfigObject): Unit = {
    require(required.forall(obj.containsKey),
      s"Attributes ${required.mkString(", ")} must be present in object at ${obj.origin.description}")
    require(requiredAny.isEmpty || requiredAny.exists(obj.containsKey),
      s"At least one of ${requiredAny.mkString(", ")} must be present in object at ${obj.origin.description}")
    val allAllowed = required ++ requiredAny ++ allowed
    iterate(obj) { (key, value) =>
      if (!allAllowed.contains(key))
        badAttr(key, value)
    } { (key, value) =>
      if (!props)
        badProp(key, value)
    }
  }

  private def getProps(obj: ConfigObject) =
    obj.asScala.view.filterKeys(k => !k.startsWith("%") && !k.startsWith("_"))

  private def badAttr(key: String, value: ConfigValue) =
    throw new IllegalArgumentException(s"Unexpected attribute $key at ${value.origin.description}")

  private def badProp(key: String, value: ConfigValue) =<commit_msg>compilation fixed<commit_after>package com.avsystem.commons
package spring

import java.{util => ju}

import com.avsystem.commons.spring.AttrNames._
import scala.annotation.nowarn
import com.typesafe.config._
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder
import org.springframework.beans.factory.config.{BeanDefinitionHolder, ConstructorArgumentValues, RuntimeBeanNameReference, RuntimeBeanReference}
import org.springframework.beans.factory.support._
import org.springframework.beans.{MutablePropertyValues, PropertyValue}
import org.springframework.core.io.Resource

class HoconBeanDefinitionReader(registry: BeanDefinitionRegistry)
  extends AbstractBeanDefinitionReader(registry) {

  import com.typesafe.config.ConfigValueType._

  private implicit class ConfigValueExtensions(value: ConfigValue) {
    def as[T: HoconType] =
      implicitly[HoconType[T]].get(value)
  }

  private val autowireMapping = AutowireMapping.withDefault {
    v => throw new IllegalArgumentException(s"Invalid value $v for $AutowireAttr attribute")
  }

  private val dependencyCheckMapping = DependencyCheckMapping.withDefault {
    v => throw new IllegalArgumentException(s"Invalid value $v for $DependencyCheckAttr attribute")
  }

  private def setup[T](t: T)(setupFunc: T => Any) = {
    setupFunc(t)
    t
  }

  private def iterate(obj: ConfigObject)
    (attrFun: (String, ConfigValue) => Any)
    (propFun: (String, ConfigValue) => Any) =
    obj.asScala.foreach {
      case (key, _) if key.startsWith("_") =>
      case (key, value) if key.startsWith("%") => attrFun(key, value)
      case (key, value) => propFun(key, value)
    }

  private def validateObj(
    required: Set[String] = Set.empty,
    requiredAny: Set[String] = Set.empty,
    allowed: Set[String] = Set.empty,
    props: Boolean = false
  )(obj: ConfigObject): Unit = {
    require(required.forall(obj.containsKey),
      s"Attributes ${required.mkString(", ")} must be present in object at ${obj.origin.description}")
    require(requiredAny.isEmpty || requiredAny.exists(obj.containsKey),
      s"At least one of ${requiredAny.mkString(", ")} must be present in object at ${obj.origin.description}")
    val allAllowed = required ++ requiredAny ++ allowed
    iterate(obj) { (key, value) =>
      if (!allAllowed.contains(key))
        badAttr(key, value)
    } { (key, value) =>
      if (!props)
        badProp(key, value)
    }
  }

  @nowarn("msg=deprecated")
  private def getProps(obj: ConfigObject) =
    obj.asScala.filterKeys(k => !k.startsWith("%") && !k.startsWith("_"))

  private def badAttr(key: String, value: ConfigValue) =
    throw new IllegalArgumentException(s"Unexpected attribute $key at ${value.origin.description}")

  private def badProp(key: String, value: ConfigValue) =
{'fraction_non_alphanumeric': 0.11971954425942156, 'fraction_numerical': 0.0, 'mean_word_length': 4.5505836575875485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6965520', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1649, 'n_words': 390}