Skip to content

Commit

Permalink
#1454 Improve customfields parser
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Nov 13, 2020
1 parent 1bd0297 commit 9b3364e
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions dto/src/main/scala/org/thp/thehive/dto/v1/CustomFieldValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,37 @@ case class InputCustomFieldValue(name: String, value: Option[Any], order: Option

object InputCustomFieldValue {

val parser: FieldsParser[Seq[InputCustomFieldValue]] = FieldsParser("customFieldValue") {
val valueParser: FieldsParser[Option[Any]] = FieldsParser("customFieldValue") {
case (_, FString(value)) => Good(Some(value))
case (_, FNumber(value)) => Good(Some(value))
case (_, FBoolean(value)) => Good(Some(value))
case (_, FAny(value :: _)) => Good(Some(value))
case (_, FUndefined | FNull) => Good(None)
}

val parser: FieldsParser[Seq[InputCustomFieldValue]] = FieldsParser("customFieldValues") {
case (_, FObject(fields)) =>
fields
.toSeq
.validatedBy {
case (name, FString(value)) => Good(InputCustomFieldValue(name, Some(value), None))
case (name, FNumber(value)) => Good(InputCustomFieldValue(name, Some(value), None))
case (name, FBoolean(value)) => Good(InputCustomFieldValue(name, Some(value), None))
case (name, FAny(value :: _)) => Good(InputCustomFieldValue(name, Some(value), None))
case (name, FNull) => Good(InputCustomFieldValue(name, None, None))
case (name, other) =>
Bad(
One(
InvalidFormatAttributeError(name, "CustomFieldValue", Set("field: string", "field: number", "field: boolean", "field: date"), other)
)
)
case (name, valueField) => valueParser(valueField).map(v => InputCustomFieldValue(name, v, None))
}
.map(_.toSeq)
case (_, FSeq(list)) =>
list.zipWithIndex.validatedBy {
case (cf: FObject, i) =>
val order = FieldsParser.int(cf.get("order")).getOrElse(i)
for {
name <- FieldsParser.string(cf.get("name"))
value <- valueParser(cf.get("value"))
} yield InputCustomFieldValue(name, value, Some(order))
case (other, i) =>
Bad(
One(
InvalidFormatAttributeError(s"customFild[$i]", "CustomFieldValue", Set.empty, other)
)
)
}
case _ => Good(Nil)
}
implicit val writes: Writes[Seq[InputCustomFieldValue]] = Writes[Seq[InputCustomFieldValue]] { icfv =>
Expand Down

0 comments on commit 9b3364e

Please sign in to comment.