This is the core library that handles the field parsing and masking. You can use this library standalone.
Requirements:
- Java >= 1.8
Gradle:
repositories {
mavenCentral()
}
dependencies {
// If using the core library only
implementation 'ma.ju.fieldmask:fieldmask-core:1.0.14'
}
Maven:
<dependency>
<groupId>ma.ju.fieldmask</groupId>
<artifactId>fieldmask-core</artifactId>
<version>1.0.14</version>
</dependency>
Parsing a string for Paths and checking if a given path matches the PathList
/* To parse a field query into a path collection */
val parser = FieldQueryParser()
val paths = parser.parse(query)
/* Checking for matches for the given mask */
val matcher = PathList.matcherFor("name,songs(title,albumId),albums/*")
/* Matches all of these paths */
matcher.matches("name")
matcher.matches("songs/title")
matcher.matches("songs/albumId")
matcher.matches("albums/name")
matcher.matches("albums/artist/name")
/* Doesn't match any of these */
matcher.matches("address/postCode")
matcher.matches("unknown")
Creating a mask for the specified object
val entity = Artist(id = 1001, name = "Avril Lavigne", songs = mutableListOf(
Song(id = 2001, title = "Complicate", track = 3)
), albums = mutableListOf(
Album(id = 3001, title = "Let Go", songCount = 12, year = 2002)
))
val model = BeanMask.mask(entity, "name,songs(title)")
/**
* Returns:
* Map<String, *>{
* "name": "Avril Lavigne",
* "songs" [
* {"title": "Complicated",
* ]
* }
*/
Copying data from one entity to another with a field mask
val modelI = Artist(id = 1001, name = "Avril Lavigne")
val modelII = Artist(id = 1002, name = "Taylor Swift")
BeanMask.apply(modelI, modelII, "name")
/**
* Returns ModelII {
* name = "Avril Lavigne"
* }
*/