Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util-jackson: Add c.t.util.jackson.YAML
Problem We'd like a YAML corollary to `c.t.util.jackson.JSON`. Solution Introduce `c.t.util.jackson.YAML` and add methods for parity to `c.t.util.jackson.JSON`. Depends on D687324 Differential Revision: https://phabricator.twitter.biz/D687327
- Loading branch information
Showing
12 changed files
with
420 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
util-jackson/src/main/scala/com/twitter/util/jackson/YAML.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.twitter.util.jackson | ||
|
||
import com.twitter.io.{Buf, ClasspathResource} | ||
import com.twitter.util.Try | ||
import java.io.{File, InputStream} | ||
|
||
/** | ||
* Uses an instance of a [[ScalaObjectMapper]] configured to not perform any type of validation. | ||
* Inspired by [[https://www.scala-lang.org/api/2.12.6/scala-parser-combinators/scala/util/parsing/json/JSON$.html]]. | ||
* | ||
* @note This is only intended for use from Scala (not Java). | ||
* | ||
* @see [[ScalaObjectMapper]] | ||
*/ | ||
object YAML { | ||
private final val Mapper: ScalaObjectMapper = | ||
ScalaObjectMapper.builder.withNoValidation.yamlObjectMapper | ||
|
||
/** Simple utility to parse a YAML string into an Option[T] type. */ | ||
def parse[T: Manifest](input: String): Option[T] = | ||
Try(Mapper.parse[T](input)).toOption | ||
|
||
/** Simple utility to parse a YAML [[Buf]] into an Option[T] type. */ | ||
def parse[T: Manifest](input: Buf): Option[T] = | ||
Try(Mapper.parse[T](input)).toOption | ||
|
||
/** | ||
* Simple utility to parse a YAML [[InputStream]] into an Option[T] type. | ||
* @note the caller is responsible for managing the lifecycle of the given [[InputStream]]. | ||
*/ | ||
def parse[T: Manifest](input: InputStream): Option[T] = | ||
Try(Mapper.parse[T](input)).toOption | ||
|
||
/** | ||
* Simple utility to load a YAML file and parse contents into an Option[T] type. | ||
* | ||
* @note the caller is responsible for managing the lifecycle of the given [[File]]. | ||
*/ | ||
def parse[T: Manifest](f: File): Option[T] = | ||
Try(Mapper.underlying.readValue[T](f)).toOption | ||
|
||
/** Simple utility to write a value as a YAML encoded String. */ | ||
def write(any: Any): String = | ||
Mapper.writeValueAsString(any) | ||
|
||
object Resource { | ||
|
||
/** | ||
* Simple utility to load a YAML resource from the classpath and parse contents into an Option[T] type. | ||
* | ||
* @note `name` resolution to locate the resource is governed by [[java.lang.Class#getResourceAsStream]] | ||
*/ | ||
def parse[T: Manifest](name: String): Option[T] = { | ||
ClasspathResource.load(name) match { | ||
case Some(inputStream) => | ||
try { | ||
YAML.parse[T](inputStream) | ||
} finally { | ||
inputStream.close() | ||
} | ||
case _ => None | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resources( | ||
sources = ["**/*"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"id": "55555555" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
id: 55555555 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
util-jackson/src/test/scala/com/twitter/util/jackson/FileResources.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.twitter.util.jackson | ||
|
||
import com.twitter.io.TempFolder | ||
import java.io.{FileOutputStream, OutputStream, File => JFile} | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.{FileSystems, Files} | ||
|
||
trait FileResources extends TempFolder { | ||
|
||
protected def writeStringToFile( | ||
directory: String, | ||
name: String, | ||
ext: String, | ||
data: String | ||
): JFile = { | ||
val file = Files.createTempFile(FileSystems.getDefault.getPath(directory), name, ext).toFile | ||
try { | ||
val out: OutputStream = new FileOutputStream(file, false) | ||
out.write(data.getBytes(StandardCharsets.UTF_8)) | ||
file | ||
} finally { | ||
file.deleteOnExit() | ||
} | ||
} | ||
|
||
protected def addSlash(directory: String): String = { | ||
if (directory.endsWith("/")) directory else s"$directory/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.