All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticmq.rest.sqs.AttributesModule.scala Maven / Gradle / Ivy

The newest version!
package org.elasticmq.rest.sqs

trait AttributesModule {
  val attributeNamesReader = new AttributeNamesReader
  val attributesToXmlConverter = new AttributesToXmlConverter
  val attributeValuesCalculator = new AttributeValuesCalculator
  val attributeNameAndValuesReader = new AttributeNameAndValuesReader

  class AttributeNamesReader {
    def read(parameters: Map[String, String], allAttributeNames: List[String]) = {
      def collect(suffix: Int, acc: List[String]): List[String] = {
        parameters.get("AttributeName." + suffix) match {
          case None => acc
          case Some(an) => collect(suffix+1, an :: acc)
        }
      }

      def unfoldAllAttributeIfRequested(attributeNames: List[String]): List[String] = {
        if (attributeNames.contains("All")) {
          allAttributeNames
        } else {
          attributeNames
        }
      }

      val rawAttributeNames = collect(1, parameters.get("AttributeName").toList)
      val attributeNames = unfoldAllAttributeIfRequested(rawAttributeNames)

      attributeNames
    }
  }

  class AttributesToXmlConverter {
    def convert(attributes: List[(String, String)]) = {
      attributes.map(a =>
        
          {a._1}
          {a._2}
        )
    }
  }

  class AttributeValuesCalculator {
    import AttributeValuesCalculator.Rule

    def calculate(attributeNames: List[String], rules: Rule*): List[(String, String)] = {
      attributeNames.flatMap(attribute => {
        rules.find(rule => rule.attributeName == attribute).map(rule => (rule.attributeName, rule.calculateValue()))
      })
    }
  }
  
  object AttributeValuesCalculator {
    case class Rule(attributeName: String, calculateValue: () => String)
  }
  
  class AttributeNameAndValuesReader {
    def read(parameters: Map[String, String]): Map[String, String] = {
      def collect(suffix: Int, acc: Map[String, String]): Map[String, String] = {
        parameters.get("Attribute."+suffix+".Name") match {
          case None => acc
          case Some(an) => collect(suffix+1, acc + (an -> parameters("Attribute."+suffix+".Value")))
        }
      }
      
      collect(1, Map())
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy