Skip to content

Commit

Permalink
Merge pull request #18 from LachlanMcKee/develop
Browse files Browse the repository at this point in the history
Releasing 1.1.0
  • Loading branch information
LachlanMcKee authored Nov 19, 2018
2 parents fd0b217 + 3133b64 commit edeb2bb
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 208 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Change Log
==========

Version 1.1.0 *(2017-02-26)*
Version 1.1.0 *(2018-11-19)*
----------------------------

* Added support for Gsonpath 2.3.0.
* Added support for Gsonpath 3.0.0

Version 1.0.0 *(2017-02-26)*
----------------------------
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,5 @@ if (nonNullMap.size() == 0) {
This library is available on Maven, you can add it to your project using the following gradle dependencies:

```gradle
apt 'net.lachlanmckee:gsonpath-extensions:1.0.0'
apt 'net.lachlanmckee:gsonpath-extensions:1.1.0'
```
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
buildscript {
ext.kotlin_version = '1.2.31'
ext.kotlin_version = '1.3.0'
ext.gson_version = '2.8.1'
ext.gsonpath_version = '2.4.0'
ext.gsonpath_compiler_version = '1.2.0'
ext.gsonpath_version = '3.0.0'
ext.gsonpath_compiler_version = '1.2.1'
ext.junit_version = '4.12'

repositories {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
2 changes: 1 addition & 1 deletion gsonpath-extensions-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'maven'
apply plugin: 'signing'

group = 'net.lachlanmckee'
version = '1.0.0'
version = '1.1.0'

targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package gsonpath.extension.def.intdef
import com.squareup.javapoet.CodeBlock
import gsonpath.compiler.ExtensionFieldMetadata
import gsonpath.compiler.GsonPathExtension
import gsonpath.compiler.addNewLine
import gsonpath.compiler.addWithNewLine
import gsonpath.extension.addException
import gsonpath.extension.def.DefAnnotationMirrors
import gsonpath.extension.def.getDefAnnotationMirrors
import gsonpath.extension.getAnnotationValueObject
import gsonpath.util.addWithNewLine
import gsonpath.util.case
import gsonpath.util.codeBlock
import gsonpath.util.switch
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.element.AnnotationValue

Expand All @@ -25,38 +27,26 @@ class IntDefGsonPathFieldValidator : GsonPathExtension {
val (fieldInfo, variableName) = extensionFieldMetadata

val defAnnotationMirrors: DefAnnotationMirrors = getDefAnnotationMirrors(fieldInfo.element,
"android.support.annotation", "IntDef") ?: return null

val validationBuilder = CodeBlock.builder()
validationBuilder.beginControlFlow("switch ($variableName)")
"android.support.annotation", "IntDef") ?: return null

// The integer constants within the 'IntDef#values' property.
val intDefValues: List<*> = getAnnotationValueObject(defAnnotationMirrors.defAnnotationMirror, "value")
as List<*>? ?: return null

// Create a 'case' for each valid integer.
intDefValues.forEach { it ->
validationBuilder.addWithNewLine("""case ${(it as AnnotationValue).value}:""")
}

validationBuilder.indent()
.addStatement("break")
.unindent()
.addNewLine()

// Create a 'default' that throws an exception if an unexpected integer is found.
.addWithNewLine("default:")
.indent()
.addException("""Unexpected Int '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""")
.unindent()

validationBuilder.endControlFlow()

val validationCodeBlock = validationBuilder.build()
if (!validationCodeBlock.isEmpty) {
return validationCodeBlock
as List<*>? ?: return null

return codeBlock {
switch(variableName) {
// Create a 'case' for each valid integer.
intDefValues.forEach {
case((it as AnnotationValue).value.toString()) {}
}

// Create a 'default' that throws an exception if an unexpected integer is found.
addWithNewLine("default:")
indent()
addException("""Unexpected Int '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""")
unindent()
}
}
return null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import com.sun.source.util.Trees
import com.sun.tools.javac.tree.JCTree
import gsonpath.compiler.ExtensionFieldMetadata
import gsonpath.compiler.GsonPathExtension
import gsonpath.compiler.addNewLine
import gsonpath.compiler.addWithNewLine
import gsonpath.extension.addException
import gsonpath.extension.def.DefAnnotationMirrors
import gsonpath.extension.def.getDefAnnotationMirrors
import gsonpath.extension.getAnnotationValue
import gsonpath.util.*
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.element.AnnotationValue

Expand All @@ -39,11 +38,11 @@ class StringDefGsonPathFieldValidator : GsonPathExtension {
val (fieldInfo, variableName) = extensionFieldMetadata

val defAnnotationMirrors: DefAnnotationMirrors = getDefAnnotationMirrors(fieldInfo.element,
"android.support.annotation", "StringDef") ?: return null
"android.support.annotation", "StringDef") ?: return null

// The annotation values reference which contains the String constants.
val defAnnotationValues: AnnotationValue = getAnnotationValue(defAnnotationMirrors.defAnnotationMirror,
"value") ?: return null
"value") ?: return null

//
// The standard javax AnnotationMirror does not expose the ability to retrieve the actual constant variable
Expand All @@ -53,53 +52,42 @@ class StringDefGsonPathFieldValidator : GsonPathExtension {
val annotationElement = defAnnotationMirrors.annotationMirror.annotationType.asElement()
val treesInstance = Trees.instance(processingEnvironment)
val stringDefConstants: List<String>? = AnnotationValueConstantsVisitor().scan(
treesInstance.getPath(annotationElement, defAnnotationMirrors.defAnnotationMirror, defAnnotationValues),
null)
treesInstance.getPath(annotationElement, defAnnotationMirrors.defAnnotationMirror, defAnnotationValues),
null)

// If there are no String constants in the 'StringDef' abort.
if (stringDefConstants == null || stringDefConstants.isEmpty()) {
return null
}

val validationBuilder = CodeBlock.builder()
validationBuilder.beginControlFlow("switch ($variableName)")

stringDefConstants.forEach { it ->

val constant =
if (!it.contains(".")) {
// Append the enclosing class name
"$annotationElement.$it"
} else {
it
}

validationBuilder.addWithNewLine("""case $constant:""")
.indent()

//
// The StringDef annotation requires the String constant to be referenced, therefore we must
// reassign the String value to the constant.
//
.addStatement("$variableName = $constant")
.addStatement("break")
.unindent()
.addNewLine()
}

// Throw an exception if an unexpected String is found.
validationBuilder.addWithNewLine("default:")
.indent()
.addException("""Unexpected String '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""")
.unindent()

validationBuilder.endControlFlow()

val validationCodeBlock = validationBuilder.build()
if (!validationCodeBlock.isEmpty) {
return validationCodeBlock
return codeBlock {
switch(variableName) {
stringDefConstants
.map {
if (!it.contains(".")) {
// Append the enclosing class name
"$annotationElement.$it"
} else {
it
}
}
.forEach {
//
// The StringDef annotation requires the String constant to be referenced, therefore we must
// reassign the String value to the constant.
//
case(it) {
assign(variableName, it)
}
}

// Throw an exception if an unexpected String is found.
addWithNewLine("default:")
indent()
addException("""Unexpected String '" + $variableName + "' for JSON element '${extensionFieldMetadata.jsonPath}'""")
unindent()
}
}
return null
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import com.squareup.javapoet.CodeBlock
import gsonpath.ProcessingException
import gsonpath.compiler.ExtensionFieldMetadata
import gsonpath.compiler.GsonPathExtension
import gsonpath.compiler.isFieldCollectionType
import gsonpath.extension.addException
import gsonpath.extension.annotation.EmptyToNull
import gsonpath.util.ProcessorTypeHandler
import gsonpath.util.`if`
import gsonpath.util.assign
import gsonpath.util.codeBlock
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.type.ArrayType
import javax.lang.model.type.TypeMirror
Expand All @@ -30,50 +33,47 @@ class EmptyToNullGsonPathFieldValidator : GsonPathExtension {
}

val fieldCollectionType: Boolean =
try {
isFieldCollectionType(processingEnvironment, fieldInfo.typeMirror)
} catch (e: Exception) {
false
}
try {
ProcessorTypeHandler(processingEnvironment).isMirrorOfCollectionType(fieldInfo.typeMirror)
} catch (e: Exception) {
false
}
val fieldMapType: Boolean =
try {
isFieldMapType(processingEnvironment, fieldInfo.typeMirror)
} catch (e: Exception) {
false
}
try {
isFieldMapType(processingEnvironment, fieldInfo.typeMirror)
} catch (e: Exception) {
false
}

val fieldType: FieldType =
when {
(fieldInfo.typeMirror is ArrayType) -> FieldType.ARRAY
fieldCollectionType -> FieldType.COLLECTION
fieldMapType -> FieldType.MAP
(fieldInfo.typeName == ClassName.get(String::class.java)) -> FieldType.STRING
when {
(fieldInfo.typeMirror is ArrayType) -> FieldType.ARRAY
fieldCollectionType -> FieldType.COLLECTION
fieldMapType -> FieldType.MAP
(fieldInfo.typeName == ClassName.get(String::class.java)) -> FieldType.STRING

else ->
throw ProcessingException("Unexpected type found for field annotated with 'EmptyToNull', only " +
"string, array, map, or collection classes may be used.", fieldInfo.element)
}
else ->
throw ProcessingException("Unexpected type found for field annotated with 'EmptyToNull', only " +
"string, array, map, or collection classes may be used.", fieldInfo.element)
}

val validationBuilder = CodeBlock.builder()
.beginControlFlow("if ($variableName${fieldType.emptyCheck})")
.apply {
return codeBlock {
`if`("$variableName${fieldType.emptyCheck}") {
if (isRequired) {
addException("JSON element '$jsonPath' cannot be blank")
} else {
addStatement("$variableName = null")
assign(variableName, "null")
}
}
.endControlFlow()

return validationBuilder.build()
}
}

private fun isFieldMapType(processingEnv: ProcessingEnvironment, typeMirror: TypeMirror): Boolean {
val mapTypeElement = processingEnv.elementUtils.getTypeElement(Map::class.java.name)
val typeUtils = processingEnv.typeUtils
val mapWildcardType = typeUtils.getDeclaredType(mapTypeElement,
typeUtils.getWildcardType(null, null),
typeUtils.getWildcardType(null, null))
typeUtils.getWildcardType(null, null),
typeUtils.getWildcardType(null, null))

return typeUtils.isSubtype(typeMirror, mapWildcardType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gsonpath.extension.range

import com.squareup.javapoet.CodeBlock
import gsonpath.extension.addException
import gsonpath.util.`if`

/**
* Creates the 'range' code validation code block.
Expand All @@ -19,23 +20,23 @@ fun CodeBlock.Builder.handleRangeValue(value: String,
variableName: String): CodeBlock.Builder {

val comparisonOperator: String =
if (isFrom) {
if (isInclusive) "<" else "<="
} else {
if (isInclusive) ">" else ">="
}
if (isFrom) {
if (isInclusive) "<" else "<="
} else {
if (isInclusive) ">" else ">="
}

val expectedOperator: String =
if (isFrom) {
if (isInclusive) ">=" else ">"
} else {
if (isInclusive) "<=" else "<"
}
if (isFrom) {
if (isInclusive) ">=" else ">"
} else {
if (isInclusive) "<=" else "<"
}

val label: String = if (isFrom) "from" else "to"

return this.beginControlFlow("if ($variableName $comparisonOperator $value)")
.addException("Invalid '$label' range for JSON element '$jsonPath'. Expected: '$expectedOperator $value', " +
"""Found '" + $variableName + "'""")
.endControlFlow()
return `if`("$variableName $comparisonOperator $value") {
addException("Invalid '$label' range for JSON element '$jsonPath'. Expected: '$expectedOperator $value', " +
"""Found '" + $variableName + "'""")
}
}
Loading

0 comments on commit edeb2bb

Please sign in to comment.