Created
March 13, 2019 14:13
-
-
Save afranzi/603b24c9e431f418b111469501ebdf48 to your computer and use it in GitHub Desktop.
JSON Schema builder with LoadingCache
This file contains hidden or 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
val schemaCache: LoadingCache[String, Schema] = CacheBuilder | |
.newBuilder | |
.maximumSize(MaximumCacheSize) | |
.expireAfterAccess(CacheMinutes, TimeUnit.MINUTES) | |
.build(new CacheLoader[String, Schema]() { | |
override def load(schemaRef: String): Schema = { | |
logger.info(s"Loading schema $schemaRef") | |
loadSchema(schemaRef: String) | |
} | |
}) | |
def buildSchema(schema: JSONObject): Schema = { | |
SchemaLoader.builder() | |
.schemaJson(schema) | |
.schemaClient(new ResourceSchemaClient) | |
.draftV7Support() | |
.useDefaults(true) | |
.build() | |
.load() | |
.build() | |
} | |
def loadSchema(schemaRef: String): Schema = { | |
val schemaObject = Try(readJsonObject(schemaRef)) match { | |
case Success(reader) => reader | |
case Failure(_: NullPointerException) => throw SchemaNotFoundException(schemaRef) | |
case Failure(exception) => throw exception | |
} | |
buildSchema(schemaObject) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment