Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove xcdata extractor #65

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,13 @@ sealed trait XmlExtractor {
}
object XmlExtractor {
val types: Map[String, String] = Map(
nameOfType[XMLCDataExtractor] -> "xcdata",
nameOfType[JsonCDataExtractor] -> "jcdata",
).withDefault(identity)

implicit val customConfiguration: TapirConfig =
TapirConfig.default.withDiscriminator("type").copy(toEncodedName = types)
}

/**
* @param prefix
* Path to CDATA
* @param path
* Path inside CDATA
*/
@derive(decoder, encoder)
final case class XMLCDataExtractor(prefix: Xpath, path: Xpath) extends XmlExtractor {
def apply(node: Node): Either[XPathError, Json] =
node
.evalXPath[String](prefix.toXPathExpr)
.flatMap(_.trim().asNode)
.flatMap(_.evalXPath[String](path.toXPathExpr))
.map(Json.fromString)
}

/**
* @param prefix
* Path to CDATA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ object Keyword
case object AllIn extends Keyword("&[_]", "$all") with JsonKeyword with FormKeyword
case object Cdata extends Keyword("cdata", "") with XmlKeyword
case object JCdata extends Keyword("jcdata", "") with XmlKeyword
case object XCdata extends Keyword("xcdata", "") with XmlKeyword

val values = findValues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,6 @@ object XmlPredicate {
} yield jpred)
.leftMap(NonEmptyList.one)
.map(jpred => (op: Option[Node]) => op.exists(_.evalXPath[Json](xp"self::node()").map(jpred).getOrElse(false)))
case (XCdata, spec) =>
Validated
.fromEither(for {
xspec <- spec
.as[Map[Xpath, Map[Keyword.Xml, Json]]]
.leftMap(_ => kwd.asInstanceOf[Keyword] -> spec)
xpred <- XmlPredicate(xspec).toEither.leftMap(errs =>
kwd.asInstanceOf[Keyword] -> Json.fromString(errs.toList.mkString(", "))
)
} yield xpred)
.leftMap(NonEmptyList.one)
.map(xpred =>
(op: Option[Node]) =>
op.exists(
_.evalXPath[String](xp"self::node()")
.flatMap(_.trim().asNode)
.map(xpred)
.getOrElse(false)
)
)
case (kwd, j) => Validated.invalidNel(kwd -> j)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,4 @@ class XmlPredicateSpec extends AnyFunSuite with Matchers with EitherValues {
sut.map(_(xml("""<json> <![CDATA[{"f": 43}]]> </json>"""))) shouldBe Some(false)
sut.map(_(xml("""<json> <![CDATA[{"f": 42]]> </json>"""))) shouldBe Some(false)
}

test("Check CDATA with XML") {
val spec = Json.obj(
"/xml" := Json.obj(
"xcdata" := Json.obj(
"/f" := Json.obj(
"==" := 42
)
)
)
)

val sut = XmlPredicate(spec.as[Map[Xpath, Map[Keyword.Xml, Json]]].value).toOption

sut.map(_(xml("""<xml> <![CDATA[<f>42</f>]]> </xml>"""))) shouldBe Some(true)
sut.map(_(xml("""<xml> <![CDATA[<f>43</f>]]> </xml>"""))) shouldBe Some(false)
sut.map(_(xml("""<xml> <![CDATA[<f>42</f]]> </xml>"""))) shouldBe Some(false)
}
}
13 changes: 0 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,6 @@ Anything that needs to be extracted from the path should be done with a _named_
### Extractors
In some cases, it's necessary to insert into the response data that cannot be extracted by simple means. For these purposes, extractors have been added.

#### xcdata Extractor

Extracts values from XML located within CDATA.

Configuration:
```javascript
{
"type": "xcdata",
"prefix": "/root/inner/tag", // Path to the tag with CDATA
"path": "/path/to" // Path to the desired tag
}
```

#### jcdata Extractor

Extracts values from JSON located within CDATA.
Expand Down
13 changes: 0 additions & 13 deletions readme_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,6 @@ HTTP заголовки валидируются на полное соотве
### Экстракторы
В некоторых случаях нужно подставить в ответ данные, которые невозможно извлечь простыми средствами. Для этих целей были добавлены экстракторы

#### Экстрактор xcdata

Достаёт значения из XML, лежащего в CDATA

конфигурация:
```javascript
{
"type": "xcdata",
"prefix": "/root/inner/tag", //Путь до тэга с CDATA
"path": "/path/to" //Путь до нужного тэга
}
```

#### Экстрактор jcdata

Достаёт значения из JSON, лежащего в CDATA
Expand Down