Skip to content

Commit

Permalink
#80 Add raw attribute format
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Mar 18, 2019
1 parent b1ad6c8 commit 7cbff29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/org/elastic4play/models/Attributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ object AttributeFormat {
val uuidFmt = UUIDAttributeFormat
val hashFmt = HashAttributeFormat
val binaryFmt = BinaryAttributeFormat
val rawFmt = RawAttributeFormat

def enumFmt[T <: Enumeration](e: T)(implicit format: Format[T#Value]): EnumerationAttributeFormat[T] = EnumerationAttributeFormat[T](e)

Expand Down Expand Up @@ -95,8 +96,8 @@ case class Attribute[T](
}

def elasticMapping: FieldDefinition = format.elasticType(attributeName) match {
case a: BasicFieldDefinition if isSensitive a.index("no")
case a a
case a: BasicFieldDefinition if isSensitive && a.`type` == "String" a.index("no")
case a a
}

def elasticTemplate(attributePath: Seq[String] = Nil): Seq[DynamicTemplateDefinition] =
Expand Down
25 changes: 25 additions & 0 deletions app/org/elastic4play/models/RawAttributeFormat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.elastic4play.models

import play.api.libs.json.{ JsString, JsValue }

import com.sksamuel.elastic4s.ElasticDsl.binaryField
import com.sksamuel.elastic4s.mappings.BasicFieldDefinition
import org.scalactic._

import org.elastic4play.controllers.{ InputValue, JsonInputValue }
import org.elastic4play.{ AttributeError, InvalidFormatAttributeError }

class RawAttributeFormat extends AttributeFormat[String]("raw") {
override def checkJson(subNames: Seq[String], value: JsValue): Or[JsValue, One[InvalidFormatAttributeError]] = value match {
case _: JsString if subNames.isEmpty Good(value)
case _ formatError(JsonInputValue(value))
}
override def fromInputValue(subNames: Seq[String], value: InputValue): String Or Every[AttributeError] = TextAttributeFormat.fromInputValue(subNames, value) match {
case Bad(One(ifae: InvalidFormatAttributeError)) Bad(One(ifae.copy(format = name)))
case other other
}

override def elasticType(attributeName: String): BasicFieldDefinition = binaryField(attributeName)
}

object RawAttributeFormat extends RawAttributeFormat

0 comments on commit 7cbff29

Please sign in to comment.